@@ -319,15 +319,23 @@ def get_type_hints(
319319 Whether to pull type hints from the signature of the object if
320320 none can be found via [`typing.get_type_hints`][]. (defaults True)
321321 """
322+ tvar_to_type = {}
322323 try :
323- hints = tp .get_type_hints (obj )
324+ t = obj
325+ if issubscriptedgeneric (t ) and not isforwardref (t ):
326+ t = obj .__origin__ # type: ignore[attr-defined,union-attr]
327+ tvar_to_type = dict (zip (obj .__parameters__ , obj .__args__ , strict = False )) # type: ignore[attr-defined,union-attr]
328+ hints = tp .get_type_hints (t )
324329 except (NameError , TypeError ):
325330 hints = {}
326331 # KW_ONLY is a special sentinel to denote kw-only params in a dataclass.
327332 # We don't want to do anything with this hint/field. It's not real.
328333 hints = {f : t for f , t in hints .items () if t is not compat .KW_ONLY }
329334 if not hints and exhaustive :
330335 hints = _hints_from_signature (obj )
336+
337+ for name , annotation in hints .items ():
338+ hints [name ] = tvar_to_type .get (annotation , annotation )
331339 return hints
332340
333341
@@ -1489,6 +1497,22 @@ def istypealiastype(t: tp.Any) -> compat.TypeIs[compat.TypeAliasType]:
14891497 return isinstance (t , compat .TypeAliasType )
14901498
14911499
1500+ @compat .cache
1501+ def istypevartype (t : tp .Any ) -> compat .TypeIs [compat .TypeVar ]:
1502+ """Detect if the given object is a [`typing.TypeAliasType`][].
1503+
1504+ Examples:
1505+ >>> type IntList = list[int]
1506+ >>> istypealiastype(IntList)
1507+ True
1508+ >>> IntList = compat.TypeAliasType("IntList", list[int])
1509+ >>> istypealiastype(IntList)
1510+ True
1511+
1512+ """
1513+ return isinstance (t , tp .TypeVar )
1514+
1515+
14921516@compat .cache
14931517def unwrap (t : tp .Any ) -> tp .Any :
14941518 lt = None
@@ -1505,6 +1529,16 @@ def unwrap(t: tp.Any) -> tp.Any:
15051529 t = tv
15061530 continue
15071531
1532+ if istypevartype (t ):
1533+ lt = t
1534+ if t .__bound__ :
1535+ t = t .__bound__
1536+ elif t .__constraints__ :
1537+ t = tp .Union [t .__constraints__ ]
1538+ else :
1539+ t = tp .Any
1540+ continue
1541+
15081542 if hasattr (t , "__supertype__" ):
15091543 lt = t
15101544 t = t .__supertype__
0 commit comments