Skip to content

Commit

Permalink
Merge pull request #90 from altescy/fix-union-typevar-map
Browse files Browse the repository at this point in the history
Fix get_typevar_map for union type
  • Loading branch information
altescy authored Dec 3, 2024
2 parents 9341252 + 70bf3c7 commit 8a1a299
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions colt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class GenericAlias:
__origin__: Any


if sys.version_info >= (3, 10):
from types import UnionType
else:

class UnionType: ...


def import_submodules(package_name: str) -> None:
"""
original code is here:
Expand Down Expand Up @@ -207,6 +214,10 @@ def get_typevar_map(annotation: Any) -> Dict[TypeVar, Any]:
if not hasattr(origin, "__parameters__"):
return {}
typevar_map: Dict[TypeVar, Any] = {}
if origin in (Union, UnionType):
for arg in typing.get_args(annotation):
typevar_map.update(get_typevar_map(arg))
return typevar_map
for cls in trace_bases(origin):
if cls is origin:
continue
Expand Down

0 comments on commit 8a1a299

Please sign in to comment.