Skip to content

Commit 9294bbd

Browse files
Escape the index note when structuring heterogeneous tuples
`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.
1 parent bc34a46 commit 9294bbd

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎HISTORY.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
1313

1414
## NEXT (UNRELEASED)
1515

16+
- Fix heterogeneous tuples and `NamedTuple`s with a member type containing a quote in its `repr`, like `tuple[Literal["a"], int]`, crashing structuring code generation with `SyntaxError`; the index note is now embedded with `repr`.
17+
([#777](https://github.com/python-attrs/cattrs/pull/777))
1618
- Fix {func}`transform_error <cattrs.transform_error>` listing the extra keys of a `ForbiddenExtraKeysError` in set iteration order, which made the message differ between runs; the keys are now sorted, like the error's own `__str__` already sorts them.
1719
([#776](https://github.com/python-attrs/cattrs/pull/776))
1820

‎src/cattrs/gen/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ def make_hetero_tuple_structure_fn(
892892
invocation = f"{struct_handler_name}(o[{ix}])"
893893
else:
894894
invocation = f"{struct_handler_name}(o[{ix}], {type_name})"
895+
note = f"Structuring {cl} @ index {ix}"
895896
lines.extend(
896897
[
897898
f" if len(o) > {ix}:",
@@ -900,7 +901,7 @@ def make_hetero_tuple_structure_fn(
900901
" except Exception as e:",
901902
(
902903
f" e.__notes__ = [*getattr(e, '__notes__', []), "
903-
f"__c_ivn('Structuring {cl} @ index {ix}', {ix}, {type_name})]"
904+
f"__c_ivn({note!r}, {ix}, {type_name})]"
904905
),
905906
" errors.append(e)",
906907
]

‎tests/test_tuples.py‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for tuples of all kinds."""
22

3-
from typing import List, NamedTuple, Tuple
3+
from typing import Annotated, List, Literal, NamedTuple, Tuple
44

55
from attrs import Factory, define
66
from pytest import raises
@@ -32,6 +32,33 @@ def test_structuring_invalid_tuples(converter: BaseConverter):
3232
converter.structure(["1", 2, "c"], tuple[int, int, int])
3333

3434

35+
def test_type_names_with_quotes():
36+
"""Types with quote characters in their reprs should work.
37+
38+
The index note is baked into the generated source, and it used to be
39+
interpolated inside single quotes, so `Literal["a"]` produced invalid
40+
source and raised `SyntaxError` before anything was structured.
41+
"""
42+
43+
class NT(NamedTuple):
44+
a: Literal["a"]
45+
b: int
46+
47+
c = Converter()
48+
49+
assert c.structure(["a", 1], Tuple[Literal["a"], int]) == ("a", 1)
50+
assert c.structure([["a"], 1], Tuple[List[Literal["a"]], int]) == (["a"], 1)
51+
assert c.structure([1, 2], Tuple[Annotated[int, "it's"], int]) == (1, 2)
52+
assert c.structure(["a", 1], NT) == NT("a", 1)
53+
54+
with raises(IterableValidationError) as exc_info:
55+
c.structure(["b", 1], Tuple[Literal["a"], int])
56+
57+
assert exc_info.value.exceptions[0].__notes__ == [
58+
f"Structuring {Tuple[Literal['a'], int]} @ index 0"
59+
]
60+
61+
3562
def test_simple_hetero_tuples(genconverter: Converter):
3663
"""Simple heterogenous tuples work.
3764

0 commit comments

Comments
 (0)