Skip to content

Fix overriding an inherited field alias back to the field's own name - #1133

Open
rvaccone wants to merge 1 commit into
msgspec:mainfrom
rvaccone:contrib/654-inherited-alias-override
Open

Fix overriding an inherited field alias back to the field's own name#1133
rvaccone wants to merge 1 commit into
msgspec:mainfrom
rvaccone:contrib/654-inherited-alias-override

Conversation

@rvaccone

Copy link
Copy Markdown
Contributor

Fixes #654.

structmeta_process_rename skips writing to renamed_fields when the requested encode name equals the field's own name, leaving any alias inherited from a base class in place. So field(name="x") on a redeclared field x was silently ignored, and the same held for a rename= policy whose result is the field's own name (e.g. rename="lower" under a parent's rename="upper"):

class Base(Struct):
    x: int = field(name="_x")

class Sub(Base):
    x: int = field(name="x")

Sub.__struct_encode_fields__  # ('_x',), expected ('x',)

The fix clears the inherited entry at both sites instead of skipping the write. Redeclared fields already re-apply the effective naming config, including overriding a base's field(name=...) alias with a policy result; this extends that to identity results.

Two behaviors deliberately unchanged: a redeclaration expressing no naming intent (x: int = 1, no policy in effect) keeps the inherited alias, and so does rename=None, which acts as "no policy in this class" rather than an identity policy. Clearing per field is spelled field(name="x"). Both new tests fail before the fix.

@codspeed-hq

codspeed-hq Bot commented Jul 30, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 139 untouched benchmarks
⏩ 135 skipped benchmarks1


Comparing rvaccone:contrib/654-inherited-alias-override (80359d9) with main (593ec54)

Open in CodSpeed

Footnotes

  1. 135 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Field Alias Overrides in Subclasses Not Reflected in __struct_encode_fields__

1 participant