Skip to content

Commit fb42966

Browse files
committed
more sturdy function to work with annotations
1 parent 45616b9 commit fb42966

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/py_avro_schema/_schemas.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import re
2727
import sys
2828
import types
29+
import typing
2930
import uuid
3031
from enum import StrEnum
3132
from typing import (
@@ -1280,7 +1281,7 @@ def handles_type(cls, py_type: Type) -> bool:
12801281
# If we are subclassing a string, used the "named string" approach
12811282
and (inspect.isclass(py_type) and not issubclass(py_type, str))
12821283
# and any other class with typed annotations
1283-
and bool(get_type_hints(py_type))
1284+
and has_annotations(py_type)
12841285
)
12851286

12861287
def __init__(
@@ -1453,3 +1454,13 @@ def _type_from_annotated(py_type: Type) -> Type:
14531454
return args[0]
14541455
else:
14551456
return py_type
1457+
1458+
1459+
def has_annotations(py_type: Type) -> bool:
1460+
"""Checks if a type has annotations"""
1461+
py_type = _type_from_annotated(py_type)
1462+
try:
1463+
return bool(typing.get_type_hints(py_type))
1464+
except Exception:
1465+
pass
1466+
return hasattr(py_type, "__annotations__")

0 commit comments

Comments
 (0)