Skip to content

Commit 8f3f971

Browse files
committed
Improve TypedDict key quoting
Improves codegen for typeddicts to fix handling keys with single quotes. Signed-off-by: Tin Tvrtkovic <[email protected]>
1 parent 82b02c0 commit 8f3f971

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

‎HISTORY.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
2323
([#737](https://github.com/python-attrs/cattrs/pull/737))
2424
- Fix an `AttributeError` in `cattrs` internals that could be triggered by using the `include_subclasses` strategy in a `structure_hook_factory`
2525
([#721](https://github.com/python-attrs/cattrs/issues/721), [#722](https://github.com/python-attrs/cattrs/pull/722))
26+
- Fix TypedDict codegen when keys contain single quotes.
27+
([#769](https://github.com/python-attrs/cattrs/pull/769))
2628
- Add `CattrsError` exception type: all exceptions raised by `cattrs` inherit from this.
2729
Literal and date-time validation raise this directly, instead of `Exception`.
2830
([#728](https://github.com/python-attrs/cattrs/pull/728))

‎src/cattrs/gen/typeddicts.py‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ def make_dict_unstructure_fn(
154154
kwargs[attr_name] = override
155155

156156
if override.omit:
157-
lines.append(f" res.pop('{attr_name}', None)")
157+
lines.append(f" res.pop({attr_name!r}, None)")
158158
continue
159159

160160
if override.rename is not None:
161161
# We also need to pop when renaming, since we're copying
162162
# the original.
163-
lines.append(f" res.pop('{attr_name}', None)")
163+
lines.append(f" res.pop({attr_name!r}, None)")
164164
kn = attr_name if override.rename is None else override.rename
165165
attr_required = attr_name in req_keys
166166

@@ -192,20 +192,20 @@ def make_dict_unstructure_fn(
192192
unstruct_handler_name = f"__c_unstr_{ix}"
193193
globs[unstruct_handler_name] = handler
194194
internal_arg_parts[unstruct_handler_name] = handler
195-
invoke = f"{unstruct_handler_name}(instance['{attr_name}'])"
195+
invoke = f"{unstruct_handler_name}(instance[{attr_name!r}])"
196196
elif override.rename is None:
197197
# We're not doing anything to this attribute, so
198198
# it'll already be present in the input dict.
199199
continue
200200
else:
201201
# Probably renamed, we just fetch it.
202-
invoke = f"instance['{attr_name}']"
202+
invoke = f"instance[{attr_name!r}]"
203203

204204
if attr_required:
205205
# No default or no override.
206-
lines.append(f" res['{kn}'] = {invoke}")
206+
lines.append(f" res[{kn!r}] = {invoke}")
207207
else:
208-
lines.append(f" if '{attr_name}' in instance: res['{kn}'] = {invoke}")
208+
lines.append(f" if {attr_name!r} in instance: res[{kn!r}] = {invoke}")
209209

210210
internal_arg_line = ", ".join([f"{i}={i}" for i in internal_arg_parts])
211211
if internal_arg_line:
@@ -373,7 +373,7 @@ def make_dict_structure_fn(
373373
allowed_fields.add(kn)
374374
i = " "
375375
if not attr_required:
376-
lines.append(f"{i}if '{kn}' in o:")
376+
lines.append(f"{i}if {kn!r} in o:")
377377
i = f"{i} "
378378
lines.append(f"{i}try:")
379379
i = f"{i} "
@@ -383,16 +383,18 @@ def make_dict_structure_fn(
383383

384384
if handler == converter._structure_call:
385385
internal_arg_parts[struct_handler_name] = t
386-
lines.append(f"{i}res['{an}'] = {struct_handler_name}(o['{kn}'])")
386+
lines.append(f"{i}res[{an!r}] = {struct_handler_name}(o[{kn!r}])")
387387
else:
388-
lines.append(f"{i}res['{an}'] = {struct_handler_name}(o['{kn}'], {tn})")
388+
lines.append(f"{i}res[{an!r}] = {struct_handler_name}(o[{kn!r}], {tn})")
389389
if override.rename is not None:
390-
lines.append(f"{i}del res['{kn}']")
390+
lines.append(f"{i}del res[{kn!r}]")
391391
i = i[:-2]
392392
lines.append(f"{i}except Exception as e:")
393393
i = f"{i} "
394+
note = f"Structuring typeddict {cl.__qualname__} @ attribute {an}"
394395
lines.append(
395-
f'{i}e.__notes__ = [*getattr(e, \'__notes__\', []), __c_avn("Structuring typeddict {cl.__qualname__} @ attribute {an}", "{an}", {tn})]'
396+
f"{i}e.__notes__ = [*getattr(e, '__notes__', []), "
397+
f"__c_avn({note!r}, {an!r}, {tn})]"
396398
)
397399
lines.append(f"{i}errors.append(e)")
398400

@@ -451,17 +453,17 @@ def make_dict_structure_fn(
451453
internal_arg_parts[struct_handler_name] = handler
452454
if handler == converter._structure_call:
453455
internal_arg_parts[struct_handler_name] = t
454-
invocation_line = f" res['{an}'] = {struct_handler_name}(o['{kn}'])"
456+
invocation_line = f" res[{an!r}] = {struct_handler_name}(o[{kn!r}])"
455457
else:
456458
tn = f"__c_type_{ix}"
457459
internal_arg_parts[tn] = t
458460
invocation_line = (
459-
f" res['{an}'] = {struct_handler_name}(o['{kn}'], {tn})"
461+
f" res[{an!r}] = {struct_handler_name}(o[{kn!r}], {tn})"
460462
)
461463

462464
lines.append(invocation_line)
463465
if override.rename is not None:
464-
lines.append(f" del res['{override.rename}']")
466+
lines.append(f" del res[{override.rename!r}]")
465467

466468
# The second loop is for optional args.
467469
if non_required:
@@ -498,20 +500,20 @@ def make_dict_structure_fn(
498500
ian = an
499501
kn = an if override.rename is None else override.rename
500502
allowed_fields.add(kn)
501-
post_lines.append(f" if '{kn}' in o:")
503+
post_lines.append(f" if {kn!r} in o:")
502504
if handler == converter._structure_call:
503505
internal_arg_parts[struct_handler_name] = t
504506
post_lines.append(
505-
f" res['{ian}'] = {struct_handler_name}(o['{kn}'])"
507+
f" res[{ian!r}] = {struct_handler_name}(o[{kn!r}])"
506508
)
507509
else:
508510
tn = f"__c_type_{ix}"
509511
internal_arg_parts[tn] = t
510512
post_lines.append(
511-
f" res['{ian}'] = {struct_handler_name}(o['{kn}'], {tn})"
513+
f" res[{ian!r}] = {struct_handler_name}(o[{kn!r}], {tn})"
512514
)
513515
if override.rename is not None:
514-
lines.append(f" res.pop('{override.rename}', None)")
516+
lines.append(f" res.pop({override.rename!r}, None)")
515517

516518
if _cattrs_forbid_extra_keys:
517519
post_lines += [

‎tests/test_typeddicts.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ def test_simple_roundtrip(cls_and_instance) -> None:
106106
assert restructured == instance
107107

108108

109+
@pytest.mark.parametrize("detailed_validation", [True, False])
110+
def test_key_with_single_quote_roundtrip(detailed_validation: bool) -> None:
111+
"""TypedDict keys containing single quotes can be round-tripped."""
112+
cls = TypedDict("SingleQuoteKey", {"that's": datetime})
113+
instance = {"that's": datetime.now(tz=timezone.utc)}
114+
c = mk_converter(detailed_validation)
115+
116+
unstructured = c.unstructure(instance, unstructure_as=cls)
117+
restructured = c.structure(unstructured, cls)
118+
119+
assert restructured == instance
120+
121+
109122
@given(simple_typeddicts(total=False), booleans())
110123
def test_simple_nontotal(cls_and_instance, detailed_validation: bool) -> None:
111124
"""Non-total dicts work."""

0 commit comments

Comments
 (0)