File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change 1
1
import json
2
2
import re
3
+ import inspect
3
4
from typing import Any , Union , get_args , get_origin
4
5
5
6
import json_repair
@@ -41,9 +42,18 @@ def extract_custom_type_from_annotation(cls, annotation):
41
42
This is used to extract all custom types from the annotation of a field, while the annotation can
42
43
have arbitrary level of nesting. For example, we detect `Tool` is in `list[dict[str, Tool]]`.
43
44
"""
44
- # Direct match
45
- if isinstance (annotation , type ) and issubclass (annotation , cls ):
46
- return [annotation ]
45
+ # Direct match. Some typing constructs (like `typing.Any`, `TypeAlias`,
46
+ # or weird internals) may pass `isinstance(..., type)` but are not
47
+ # valid classes for `issubclass`. We defensively guard against this by
48
+ # using `inspect.isclass` and wrapping the call in a try/except block.
49
+ try :
50
+ if inspect .isclass (annotation ) and issubclass (annotation , cls ):
51
+ return [annotation ]
52
+ except TypeError :
53
+ # `issubclass` can raise `TypeError` if the argument is not actually
54
+ # a class (even if `inspect.isclass` thought otherwise). In these
55
+ # cases we just ignore the annotation.
56
+ return []
47
57
48
58
origin = get_origin (annotation )
49
59
if origin is None :
You can’t perform that action at this time.
0 commit comments