Skip to content
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

Do not add kw_only dataclass fields to __match_args__ #18892

Merged
merged 1 commit into from
Apr 7, 2025
Merged

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Apr 7, 2025

In runtime python does not add fields that have kw_only marker:

See:

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

Closes #18863

Copy link
Contributor

github-actions bot commented Apr 7, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@sterliakov sterliakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Consider also submitting a documentation bug to python/cpython: now the docs fail to mention this rather important aspect (and even seem to suggest the opposite?).

match_args: If true (the default is True), the match_args tuple will be created from the list of parameters to the generated init() method (even if init() is not generated, see above).

kw_only: If true (the default value is False), then all fields will be marked as keyword-only. If a field is marked as keyword-only, then the only effect is that the init() parameter generated from a keyword-only field must be specified with a keyword when init() is called. There is no effect on any other aspect of dataclasses. See the parameter glossary entry for details. Also see the KW_ONLY section.

@sobolevn
Copy link
Member Author

sobolevn commented Apr 7, 2025

Consider also submitting a documentation bug to python/cpython

Agreed, working on it!

Thanks!

@sobolevn sobolevn merged commit 749f258 into master Apr 7, 2025
18 checks passed
@sobolevn sobolevn deleted the issue-18863 branch April 7, 2025 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

__match_args__ variable type differes when generating stubs for keyword only dataclasses
2 participants