Skip to content

fix attrs field order for diamond inheritance #4640 - #4696

Open
shobhitmehro wants to merge 1 commit into
facebook:mainfrom
shobhitmehro:fix-attrs-diamond-field-order-4640
Open

fix attrs field order for diamond inheritance #4640#4696
shobhitmehro wants to merge 1 commit into
facebook:mainfrom
shobhitmehro:fix-attrs-diamond-field-order-4640

Conversation

@shobhitmehro

Copy link
Copy Markdown
Contributor

in attrs classes with diamond inheritance, get_dataclass_fields reordered 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

@meta-cla

meta-cla Bot commented Aug 27, 2026

Copy link
Copy Markdown

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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

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.
@shobhitmehro
shobhitmehro force-pushed the fix-attrs-diamond-field-order-4640 branch from ebb2a2a to 6508b34 Compare August 27, 2026 20:29
@meta-cla meta-cla Bot added the cla signed label Aug 27, 2026
@github-actions github-actions Bot added size/m and removed size/m labels Aug 27, 2026
@github-actions

Copy link
Copy Markdown

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrectly Inferred Attribute Order for Diamond Inheritance and Attrs Classes

2 participants