Skip to content

Commit 42e2d93

Browse files
committed
Add test for type resolution using generics and forward references
1 parent 0c226c2 commit 42e2d93

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

tests/forwardrefs.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Class definitions using forward references."""
2+
3+
from __future__ import annotations
4+
5+
from typing import Generic, TypeVar
6+
7+
from attrs import define
8+
9+
T = TypeVar("T")
10+
11+
12+
@define
13+
class GenericClass(Generic[T]):
14+
t: T

tests/test_generics.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import pytest
55
from attrs import asdict, define
66

7+
import cattrs
78
from cattrs import BaseConverter, Converter
89
from cattrs._compat import Protocol
910
from cattrs._generics import deep_copy_with
1011
from cattrs.errors import StructureHandlerNotFoundError
1112
from cattrs.gen._generics import generate_mapping
1213

1314
from ._compat import Dict_origin, List_origin, is_py310_plus, is_py311_plus
15+
from .forwardrefs import GenericClass
1416

1517
T = TypeVar("T")
1618
T2 = TypeVar("T2")
@@ -161,7 +163,7 @@ class TClass2(Generic[T]):
161163

162164

163165
def test_raises_if_no_generic_params_supplied(
164-
converter: Union[Converter, BaseConverter]
166+
converter: Union[Converter, BaseConverter],
165167
):
166168
data = TClass(1, "a")
167169

@@ -358,3 +360,9 @@ class GenericEntity(GenericProtocol[int]):
358360
assert generate_mapping(GenericEntity) == {"T": int}
359361

360362
assert converter.structure({"a": 1}, GenericEntity) == GenericEntity(1)
363+
364+
365+
def test_generics_with_forward_refs():
366+
"""Type resolution works with forward references."""
367+
converter = cattrs.Converter()
368+
converter.unstructure(GenericClass(42), unstructure_as=GenericClass[int])

0 commit comments

Comments
 (0)