Skip to content

Commit

Permalink
Merge branch 'main' into chore/updates_annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Sep 18, 2022
2 parents 569b7b6 + c3faf86 commit 0248bec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ History
* Expose all error classes in the `cattr.errors` namespace. Note that it is deprecated, just use `cattrs.errors`. (`#252 <https://github.com/python-attrs/cattrs/issues/252>`_)
* ``cattrs.Converter`` and ``cattrs.BaseConverter`` can now copy themselves using the ``copy`` method.
(`#284 <https://github.com/python-attrs/cattrs/pull/284>`_)
* Fix generating structuring functions for types with quotes in the name. (`#291 <https://github.com/python-attrs/cattrs/issues/291>`_ `#277 <https://github.com/python-attrs/cattrs/issues/277>`_)

22.1.0 (2022-04-03)
-------------------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ source = [
]

[build-system]
requires = ["poetry-core>=1.0.0"]
requires = ["poetry-core>=1.1.0"]
build-backend = "poetry.core.masonry.api"
8 changes: 4 additions & 4 deletions src/cattrs/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def make_dict_structure_fn(
lines.append(f"{i}except Exception as e:")
i = f"{i} "
lines.append(
f"{i}e.__note__ = 'Structuring class {cl.__qualname__} @ attribute {an}'"
f"{i}e.__note__ = 'Structuring class ' + {cl.__qualname__!r} + ' @ attribute {an}'"
)
lines.append(f"{i}errors.append(e)")

Expand All @@ -366,15 +366,15 @@ def make_dict_structure_fn(
]

post_lines.append(
f" if errors: raise __c_cve('While structuring {cl.__name__}', errors, __cl)"
f" if errors: raise __c_cve('While structuring ' + {cl.__name__!r}, errors, __cl)"
)
instantiation_lines = (
[" try:"]
+ [" return __cl("]
+ [f" {line}" for line in invocation_lines]
+ [" )"]
+ [
f" except Exception as exc: raise __c_cve('While structuring {cl.__name__}', [exc], __cl)"
f" except Exception as exc: raise __c_cve('While structuring ' + {cl.__name__!r}, [exc], __cl)"
]
)
else:
Expand Down Expand Up @@ -750,7 +750,7 @@ def make_mapping_structure_fn(
lines.append(" errors.append(e)")
lines.append(" if errors:")
lines.append(
f" raise IterableValidationError('While structuring {cl!r}', errors, __cattr_mapping_cl)"
f" raise IterableValidationError('While structuring ' + {repr(cl)!r}, errors, __cattr_mapping_cl)"
)
else:
lines.append(f" res = {{{k_s}: {v_s} for k, v in mapping.items()}}")
Expand Down
21 changes: 19 additions & 2 deletions tests/test_gen_dict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Tests for generated dict functions."""
from typing import Type
from typing import Dict, Type

import pytest
from attr import Factory, define, field
Expand All @@ -8,7 +8,7 @@
from hypothesis.strategies import data, just, one_of, sampled_from

from cattrs import BaseConverter, Converter
from cattrs._compat import adapted_fields, fields
from cattrs._compat import adapted_fields, fields, is_py39_plus
from cattrs.errors import ClassValidationError, ForbiddenExtraKeysError
from cattrs.gen import make_dict_structure_fn, make_dict_unstructure_fn, override

Expand Down Expand Up @@ -287,3 +287,20 @@ class A:
assert structured.a == 1
assert structured.c == 1
assert not hasattr(structured, "b")


@pytest.mark.skipif(not is_py39_plus, reason="literals and annotated are 3.9+")
def test_type_names_with_quotes():
"""Types with quote characters in their reprs should work."""
from typing import Annotated, Literal, Union

converter = Converter()

assert converter.structure({1: 1}, Dict[Annotated[int, "'"], int]) == {1: 1}

converter.register_structure_hook_func(
lambda t: t is Union[Literal["a", 2, 3], Literal[4]], lambda v, _: v
)
assert converter.structure(
{2: "a"}, Dict[Union[Literal["a", 2, 3], Literal[4]], str]
) == {2: "a"}

0 comments on commit 0248bec

Please sign in to comment.