Skip to content

Commit a83d2f5

Browse files
committed
use get_type_hints to handle forward references
1 parent f474bf4 commit a83d2f5

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/py_avro_schema/_schemas.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,8 +1156,13 @@ def __init__(self, py_type: Type, namespace: Optional[str] = None, options: Opti
11561156
super().__init__(py_type, namespace=namespace, options=options)
11571157
py_type = _type_from_annotated(py_type)
11581158

1159+
# Try to get resolved type hints, but fall back to raw annotations if there are unresolved forward refs
1160+
try:
1161+
type_hints = get_type_hints(py_type, include_extras=True)
1162+
except NameError:
1163+
type_hints = py_type.__annotations__
11591164
self.py_fields: list[tuple[str, type]] = []
1160-
for k, v in py_type.__annotations__.items():
1165+
for k, v in type_hints.items():
11611166
self.py_fields.append((k, v))
11621167
# We store __init__ parameters with default values. They can be used as defaults for the record.
11631168
self.signature_fields = {

tests/test_plain_class.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,5 @@ class PyClass:
157157
def test_type_aliases_future():
158158
from py_avro_schema._testing import PyClass
159159

160-
expected = {"fields": [{"name": "name", "type": "string"}], "name": "PyType", "type": "record"}
160+
expected = {"fields": [{"name": "name", "type": "string"}], "name": "PyClass", "type": "record"}
161161
assert_schema(PyClass, expected)

0 commit comments

Comments
 (0)