Skip to content

__match_args__ variable type differes when generating stubs for keyword only dataclasses #18863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dstansby opened this issue Mar 31, 2025 · 1 comment · Fixed by #18892
Closed
Assignees
Labels
bug mypy got something wrong topic-dataclasses topic-match-statement Python 3.10's match statement

Comments

@dstansby
Copy link

Bug Report

Generating and then testing stubs on a dataclass with frozen keyword arguments fails.

To Reproduce

import dataclasses

@dataclasses.dataclass(kw_only=True)
class A:
    a: int

Then generate a stub, and run stubtest. The generated stub is (indentical to the above):

import dataclasses

@dataclasses.dataclass(kw_only=True)
class A:
    a: int

Expected Behavior
stubtest passes

Actual Behavior

% python -m mypy.stubtest test
error: test.A.__match_args__ variable differs from runtime type tuple[()]
Stub: in file /Users/dstansby/software/mpl/matplotlib/test.pyi:-1
tuple[Literal['a']]
Runtime:
()

Found 1 error (checked 1 module)

Your Environment

  • Mypy version used: mypy 1.15.0 (compiled: yes)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.12.9
@dstansby dstansby added the bug mypy got something wrong label Mar 31, 2025
@dstansby
Copy link
Author

Possibly related (but different) to #15748

@brianschubert brianschubert added topic-match-statement Python 3.10's match statement topic-dataclasses and removed topic-match-statement Python 3.10's match statement labels Mar 31, 2025
@sobolevn sobolevn self-assigned this Apr 7, 2025
sobolevn added a commit that referenced this issue Apr 7, 2025
In runtime python does not add fields that have `kw_only` marker:
-
https://github.com/python/cpython/blob/895d983b5c9716aaaab34d14d278084b9b6730d8/Lib/dataclasses.py#L1174-L1177
-
https://github.com/python/cpython/blob/895d983b5c9716aaaab34d14d278084b9b6730d8/Lib/dataclasses.py#L411-L417

See:

```python
>>> import dataclasses
>>> @dataclasses.dataclass(kw_only=True)
... class A:
...     a: int
... 
>>> print(A.__match_args__)
()
```

Closes #18863
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-dataclasses topic-match-statement Python 3.10's match statement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants