Conversation
`create_default_dis_func` computes attribute overrides up front, but the `use_literals` branch built its candidate names from the raw attribute names and looked the discriminator up in the payload under that same raw name. A union whose discriminator field was renamed with `override(rename=...)` therefore failed to structure with a bare `KeyError`, even though each member structured correctly on its own. Build the literal candidate names from the overridden names instead, and keep a per-class map back to the original names so the `Literal` args are still read off the right field. The unique-key branch already did this through `_usable_attribute_names`; this brings the two strategies in line. Fixes python-attrs#779 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #779.
create_default_dis_funccomputes attributeoverridesup front, but theuse_literalsbranch never applied them: it built its candidate discriminator names from the rawat.name, and the generateddis_funclooked the value up asdata[best_discriminator]using that same raw name. A union whoseLiteraldiscriminator field was renamed withoverride(rename=...)therefore failed to structure with a bareKeyError, even though each member structured correctly on its own.The unique-key branch further down already routes names through
_usable_attribute_names, andtest_field_renamingcovers it. This brings the literal strategy in line.Before
After
Both return
A(kind='a', a_val=1).The change
Candidate names are now built from the overridden names, with a per-class
back_mapfrom the payload name to the original attribute name so theLiteralargs are still read off the right field. The discriminator set is still the intersection across all members, so it only matches when every member agrees on the payload name.Testing
test_renamed_literal_discriminatoradded next to the existingtest_field_renaming. It fails onmainwithKeyError: 'kind'across all four converter parametrizations and passes with the fix. Full suite: 994 passed, 15 xfailed, no regressions.ruff format --checkandruff checkare clean.🤖 Generated with Claude Code