Skip to content

Commit 9f506c2

Browse files
committed
use prepare_subst if avail and formatting
1 parent ee810d9 commit 9f506c2

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/typing_extensions.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ def copy_with(self, params):
18251825
"ParamSpec variable or ellipsis.")
18261826
return self.__class__(self.__origin__, params)
18271827

1828-
# 3.9; accessed during GenericAlias.__getitem__when substituting
1828+
# 3.9; accessed during GenericAlias.__getitem__ when substituting
18291829
def __getitem__(self, args):
18301830
if self.__origin__ in (Generic, Protocol):
18311831
# Can't subscript Generic[...] or Protocol[...].
@@ -1838,7 +1838,11 @@ def __getitem__(self, args):
18381838
args = _unpack_args(*(_type_convert(p) for p in args))
18391839
params = self.__parameters__
18401840
for param in params:
1841-
if isinstance(param, ParamSpec):
1841+
prepare = getattr(param, "__typing_prepare_subst__", None)
1842+
if prepare is not None:
1843+
args = prepare(self, args)
1844+
# 3.8 - 3.9 & typing.ParamSpec
1845+
elif isinstance(param, ParamSpec):
18421846
i = params.index(param)
18431847
if (
18441848
i == len(args)
@@ -1851,9 +1855,10 @@ def __getitem__(self, args):
18511855
if len(params) == 1 and not _is_param_expr(args[0]):
18521856
assert i == 0
18531857
args = (args,)
1854-
# This class inherits from list do not convert
18551858
elif (
18561859
isinstance(args[i], list)
1860+
# 3.8 - 3.9
1861+
# This class inherits from list do not convert
18571862
and not isinstance(args[i], _ConcatenateGenericAlias)
18581863
):
18591864
args = (*args[:i], tuple(args[i]), *args[i+1:])
@@ -1886,9 +1891,10 @@ def __getitem__(self, args):
18861891
raise TypeError(f"{arg} is not valid as type argument")
18871892

18881893
elif isinstance(arg,
1889-
typing._GenericAlias
1890-
if not hasattr(_types, "GenericAlias") else
1891-
(typing._GenericAlias, _types.GenericAlias)):
1894+
typing._GenericAlias
1895+
if not hasattr(_types, "GenericAlias") else
1896+
(typing._GenericAlias, _types.GenericAlias)
1897+
):
18921898
subparams = arg.__parameters__
18931899
if subparams:
18941900
subargs = tuple(subst[x] for x in subparams)

0 commit comments

Comments
 (0)