Fix dataclass field collection to only include class body fields#3347
Open
QEDady wants to merge 4 commits intofacebook:mainfacebook/pyrefly:mainfrom
QEDady:try-class-body-fields-onlyQEDady/pyrefly:try-class-body-fields-onlyCopy head branch name to clipboard
Open
Fix dataclass field collection to only include class body fields#3347QEDady wants to merge 4 commits intofacebook:mainfacebook/pyrefly:mainfrom QEDady:try-class-body-fields-onlyQEDady/pyrefly:try-class-body-fields-onlyCopy head branch name to clipboard
QEDady wants to merge 4 commits intofacebook:mainfacebook/pyrefly:mainfrom
QEDady:try-class-body-fields-onlyQEDady/pyrefly:try-class-body-fields-onlyCopy head branch name to clipboard
Conversation
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Restricts dataclass field collection in
get_dataclass_fieldsto useclass_body_fields()instead ofnames().Previously,
pyreflycollected all annotated fields for dataclasses, including those defined via assignments in methods (e.g.,cls.field: Any = Nonein a class method). This caused issues when such an assignment shadowed a declaration in a base class decorated with@dataclass_transform, leading to false positivemissing-argumenterrors at call sites.By restricting collection to class body fields, we ensure that only fields declared in the class body are considered candidates for dataclass fields, aligning with standard dataclass semantics.
Fixes #3346
Test Plan
Added two new unit tests in
dataclass_transform.rsto verify that fields shadowed by method assignments (both class methods and instance methods) are correctly ignored:test_classmethod_shadowing_base_annotation_ignoredtest_method_shadowing_base_annotation_ignored