Fix false positive bad-class-definition for class methods#3261
Fix false positive bad-class-definition for class methods#3261QEDady wants to merge 1 commit intofacebook:mainfrom
Conversation
|
Hi @QEDady! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Welcome to the project and thank you for your contribution @QEDady! The fix correctly identifies the bug (Python's @DataClass ignores cls.x = ... assignments inside class methods, but pyrefly was treating them as fields). The 2 test cases are well-chosen. Tagging @rechen to review this PR. Here are a some key changes to consider while reviewing this PR:
|
Summary
Pyrefly incorrectly extracts dataclass fields from class method assignments (e.g., inside
@classmethodor__init_subclass__). At runtime, these assignments are ignored by Python'sdataclasses.dataclassmechanism.This leads to a false positive
bad-class-definitionerror when a subclass defines a field without a default value.To fix this, I introduced the new variant
ClassMethodto theClassFieldInitializationenum to explicitly track fields initialized inside class methods. These fields are now filtered out during dataclass field extraction, which aligns Pyrefly's behavior with standard Python dataclass semantics.Fixes #3259
Test Plan
I added two new integration tests to
pyrefly/lib/test/dataclass_transform.rs:test_class_method_field_ignored_by_dataclass: Verifies that fields defined in regular class methods are ignored by dataclass synthesis.test_init_subclass_field_ignored_by_dataclass: Verifies that fields defined in__init_subclass__are ignored by dataclass synthesis.I ran
test.pyand it didn't have any failure.