fix Feature: support @field.validator decorator for attrs #2013#2782
fix Feature: support @field.validator decorator for attrs #2013#2782asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
This PR updates Pyrefly’s class-body name-assignment inference so that attrs/dataclass field specifier assignments (e.g. attrs.field()) can be treated as runtime “field objects” inside the class body, enabling decorator surfaces like @x.validator / @a.default to resolve.
Changes:
- Track the owning class for
Binding::NameAssignvia a newclass_keyfield. - Adjust name-assignment inference in class-member contexts to treat dataclass/attrs field specifier assignments as
Any(to avoid the annotation overwriting the field object type in the class body). - Update attrs tests to cover
@x.validatorresolution and refine the existing default-decorator test expectations.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
pyrefly/lib/test/attrs/fields.rs |
Updates attrs testcases to validate @field.validator / @field.default decorator attribute resolution behavior. |
pyrefly/lib/binding/target.rs |
Populates NameAssign.class_key from the current class scope when binding class-body assignments. |
pyrefly/lib/binding/binding.rs |
Adds class_key to the NameAssign binding payload. |
pyrefly/lib/alt/solve.rs |
Uses class_key during NameAssign inference to detect field specifier assignments and model class-body reads as Any for decorator resolution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D97669799. |
migeed-z
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
Diff from mypy_primer, showing the effect of this PR on open source code: core (https://github.com/home-assistant/core)
- ERROR homeassistant/helpers/entity_registry.py:252:6-20: Object of class `str` has no attribute `default` [missing-attribute]
- ERROR homeassistant/helpers/entity_registry.py:667:6-20: Object of class `str` has no attribute `default` [missing-attribute]
attrs (https://github.com/python-attrs/attrs)
- ERROR tests/test_next_gen.py:142:14-25: Object of class `list` has no attribute `validator` [missing-attribute]
- ERROR tests/test_next_gen.py:380:14-25: Object of class `int` has no attribute `validator` [missing-attribute]
|
Primer Diff Classification✅ 2 improvement(s) | 2 project(s) total | -4 errors 2 improvement(s) across core, attrs.
Detailed analysis✅ Improvement (2)core (-2)
attrs (-2)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (2 LLM) |
Summary
Fixes #2013
patched class-body name assignment handling so attrs/dataclass field specifiers are recognized before the annotated field type overwrites them.
Test Plan
update test