|
1 | 1 | """Tests for generated dict functions."""
|
2 |
| -from typing import Type |
| 2 | +from typing import Dict, Type |
3 | 3 |
|
4 | 4 | import pytest
|
5 | 5 | from attr import Factory, define, field
|
|
8 | 8 | from hypothesis.strategies import data, just, one_of, sampled_from
|
9 | 9 |
|
10 | 10 | from cattrs import BaseConverter, Converter
|
11 |
| -from cattrs._compat import adapted_fields, fields |
| 11 | +from cattrs._compat import adapted_fields, fields, is_py39_plus |
12 | 12 | from cattrs.errors import ClassValidationError, ForbiddenExtraKeysError
|
13 | 13 | from cattrs.gen import make_dict_structure_fn, make_dict_unstructure_fn, override
|
14 | 14 |
|
@@ -287,3 +287,20 @@ class A:
|
287 | 287 | assert structured.a == 1
|
288 | 288 | assert structured.c == 1
|
289 | 289 | assert not hasattr(structured, "b")
|
| 290 | + |
| 291 | + |
| 292 | +@pytest.mark.skipif(not is_py39_plus, reason="literals and annotated are 3.9+") |
| 293 | +def test_type_names_with_quotes(): |
| 294 | + """Types with quote characters in their reprs should work.""" |
| 295 | + from typing import Annotated, Literal, Union |
| 296 | + |
| 297 | + converter = Converter() |
| 298 | + |
| 299 | + assert converter.structure({1: 1}, Dict[Annotated[int, "'"], int]) == {1: 1} |
| 300 | + |
| 301 | + converter.register_structure_hook_func( |
| 302 | + lambda t: t is Union[Literal["a", 2, 3], Literal[4]], lambda v, _: v |
| 303 | + ) |
| 304 | + assert converter.structure( |
| 305 | + {2: "a"}, Dict[Union[Literal["a", 2, 3], Literal[4]], str] |
| 306 | + ) == {2: "a"} |
0 commit comments