fix attrs field order for diamond inheritance #4640 - #4696
Conversation
|
Hi @shobhitmehro! Thank you for your pull request. We require contributors to sign our Contributor License Agreement, and yours needs attention. You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted. 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! |
When an attrs class reaches the same field through two bases, get_dataclass_fields relocated every field it saw from a base to the end (attrs' behavior for a redefined field), including one the base merely inherited from a shared ancestor, so the merged order depended on the order the subclass listed its bases and could place a required field after a defaulted one, wrongly reporting "field without a default may not follow" on a class that is valid at runtime. Now a field is relocated only when the base declares it in its own class body, so purely inherited fields keep their position and D(C, B) and D(B, C) infer the same __init__, matching attrs at runtime.
ebb2a2a to
6508b34
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
in attrs classes with diamond inheritance,
get_dataclass_fieldsreordered inherited fields based on base-class order, sometimes raising a spurious "field without a default may not follow" error. It now reorders a field only when a base declares it directly, so inherited fields keep their position.Fixes #4640
Test Plan
python3 test.py