Escape rename keys when generating (un)structuring code - #771
Merged
Tinche merged 3 commits intoSep 1, 2026
Merged
Conversation
The generated (un)structuring code interpolated a field's rename target into
the source inside single quotes (o['{kn}'], res['{kn}'], etc). A rename key is
an arbitrary dict key, so one containing a quote (for example "it's") produced
invalid source and raised SyntaxError. Interpolate the key with repr ({kn!r})
so any key string is embedded safely.
Tinche
requested changes
Aug 31, 2026
Tinche
left a comment
Member
There was a problem hiding this comment.
Looks good, left a comment. Thanks
Contributor
Author
|
Done, parametrized in |
Tinche
approved these changes
Sep 1, 2026
Member
|
Great, thanks! |
Tinche
pushed a commit
that referenced
this pull request
Sep 14, 2026
`make_hetero_tuple_structure_fn` builds the per-index note by interpolating
`str(cl)` into a single-quoted string literal in the generated source, at
`src/cattrs/gen/__init__.py` line 903 on main:
`f"__c_ivn('Structuring {cl} @ index {ix}', {ix}, {type_name})]"`. When a type
argument has a quote in its `repr`, such as `Literal["a"]`, the quote closes
the literal early and compiling the hook raises `SyntaxError` before any data
is looked at. This only affects the generated path, so `Converter()` fails
where `BaseConverter()` and `Converter(detailed_validation=False)` succeed.
`NamedTuple`s structure through the same factory, so they crash the same way.
This is the same class of bug as #769 and #771, and the fix is the same shape
already used in `src/cattrs/gen/typeddicts.py` line 394: build the note text in
Python and embed it with `repr`. The note text is byte-identical to before, so
nothing that reads `IterableValidationNote` changes.
The test covers a quote from `Literal`, the same quote nested inside a `List`,
an `Annotated` whose metadata repr contains both quote characters, and a
`NamedTuple` field, then triggers a real validation failure and checks the note
reads exactly as it does for a tuple whose arguments have no quotes. The
expected note is built from the same type expression so the assertion does not
depend on how a given Python version renders typing objects.
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.
Summary
A field's
renametarget is a dictionary key, so it can legitimately contain any character. But the generated (un)structuring code interpolates it into the source inside single quotes:So a rename to a key containing a quote produces invalid source and blows up when it's compiled:
Fix
Interpolate the key with
repr({kn!r}) instead of hand-written single quotes, so any key string is embedded as a valid, correctly-escaped literal. This only affects therenamekey (kn); attrs already rejects aliases that aren't valid identifiers, so the alias-based keys can't hit this.After the change,
it's, keys with double quotes, and keys that look like code all round-trip as plain string keys.Tests
Added
test_renaming_to_key_with_special_characters(structure + unstructure round-trip for a rename containing a quote and a couple of other awkward keys). It raisesSyntaxErroronmainand passes here. Fulltest_gen_dict.py/test_gen.pystay green (63 passed).