Skip to content

Commit 2814a85

Browse files
committed
Better arangement and style
1 parent 9f506c2 commit 2814a85

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

src/test_typing_extensions.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5307,6 +5307,7 @@ class ProtoZ(Protocol[P]):
53075307
G6 = klass[int, str, T]
53085308
G6args = G6.__args__[0] if sys.version_info >= (3, 10) else G6.__args__
53095309
self.assertEqual(G6args, (int, str, T))
5310+
self.assertEqual(G6.__parameters__, (T,))
53105311

53115312
# P = [int]
53125313
G7 = klass[int]
@@ -5340,61 +5341,54 @@ class ProtoZ(Protocol[P]):
53405341
things = "arguments" if sys.version_info >= (3, 10) else "parameters"
53415342
for klass in Z, ProtoZ:
53425343
with self.subTest(klass=klass.__name__):
5343-
G6 = klass[int, str, T]
53445344
G8 = klass[Concatenate[T, ...]]
5345+
5346+
H8_1 = G8[int]
5347+
self.assertEqual(H8_1.__parameters__, ())
5348+
with self.assertRaisesRegex(TypeError, "not a generic class"):
5349+
H8_1[str]
5350+
5351+
H8_2 = G8[T][int]
5352+
self.assertEqual(H8_2.__parameters__, ())
5353+
with self.assertRaisesRegex(TypeError, "not a generic class"):
5354+
H8_2[str]
5355+
53455356
G9 = klass[Concatenate[T, P_2]]
5357+
self.assertEqual(G9.__parameters__, (T, P_2))
53465358

53475359
with self.assertRaisesRegex(TypeError,
5348-
(
5349-
"The last parameter to Concatenate should be a ParamSpec variable or ellipsis."
5350-
if sys.version_info < (3, 10) else
5351-
# from __typing_subst__
5352-
"Expected a list of types, an ellipsis, ParamSpec, or Concatenate"
5353-
)
5360+
"The last parameter to Concatenate should be a ParamSpec variable or ellipsis."
5361+
if sys.version_info < (3, 10) else
5362+
# from __typing_subst__
5363+
"Expected a list of types, an ellipsis, ParamSpec, or Concatenate"
53545364
):
53555365
G9[int, int]
53565366

5357-
self.assertEqual(G9.__parameters__, (T, P_2))
53585367
with self.assertRaisesRegex(TypeError, f"Too few {things}"):
53595368
G9[int]
53605369

53615370
with self.subTest("Check list as parameter expression", klass=klass.__name__):
53625371
if sys.version_info < (3, 10):
53635372
self.skipTest("Cannot pass non-types")
53645373
G5 = klass[[int, str, T]]
5374+
self.assertEqual(G5.__parameters__, (T,))
53655375
self.assertEqual(G5.__args__, ((int, str, T),))
5366-
H9 = G9[int, [T]]
5367-
5368-
5369-
with self.subTest("Check parametrization", klass=klass.__name__):
5370-
if sys.version_info >= (3, 10): # only availiable for 3.10+
5371-
self.assertEqual(H9.__parameters__, (T,))
5372-
self.assertEqual(G5.__parameters__, (T,))
5373-
self.assertEqual(G6.__parameters__, (T,))
53745376

5375-
with self.subTest("Check further substitution", klass=klass.__name__):
5376-
H1 = G8[int]
5377-
self.assertEqual(H1.__parameters__, ())
5378-
with self.assertRaisesRegex(TypeError, "not a generic class"):
5379-
H1[str]
5380-
5381-
H2 = G8[T][int]
5382-
self.assertEqual(H2.__parameters__, ())
5383-
with self.assertRaisesRegex(TypeError, "not a generic class"):
5384-
H2[str]
5377+
H9 = G9[int, [T]]
5378+
self.assertEqual(H9.__parameters__, (T,))
53855379

53865380
# This is an invalid parameter expression but useful for testing correct subsitution
53875381
G10 = klass[int, Concatenate[str, P]]
53885382
with self.subTest("Check invalid form substitution"):
53895383
self.assertEqual(G10.__parameters__, (P, ))
53905384
if sys.version_info < (3, 9):
53915385
self.skipTest("3.8 typing._type_subst does not support this substitution process")
5392-
H3 = G10[int]
5386+
H10 = G10[int]
53935387
if (3, 10) <= sys.version_info < (3, 11, 3):
53945388
self.skipTest("3.10-3.11.2 does not substitute Concatenate here")
5395-
self.assertEqual(H3.__parameters__, ())
5396-
H3args = H3.__args__[0] if sys.version_info >= (3, 10) else H3.__args__
5397-
self.assertEqual(H3args, (int, (str, int)))
5389+
self.assertEqual(H10.__parameters__, ())
5390+
H10args = H10.__args__[0] if sys.version_info >= (3, 10) else H10.__args__
5391+
self.assertEqual(H10args, (int, (str, int)))
53985392

53995393
def test_pickle(self):
54005394
global P, P_co, P_contra, P_default

src/typing_extensions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ def copy_with(self, params):
18201820
params = (*params[:-1], *params[-1].__args__)
18211821
elif isinstance(params[-1], (list, tuple)):
18221822
return (*params[:-1], *params[-1])
1823-
elif (not(params[-1] is ... or isinstance(params[-1], ParamSpec))):
1823+
elif (not (params[-1] is ... or isinstance(params[-1], ParamSpec))):
18241824
raise TypeError("The last parameter to Concatenate should be a "
18251825
"ParamSpec variable or ellipsis.")
18261826
return self.__class__(self.__origin__, params)
@@ -1861,7 +1861,7 @@ def __getitem__(self, args):
18611861
# This class inherits from list do not convert
18621862
and not isinstance(args[i], _ConcatenateGenericAlias)
18631863
):
1864-
args = (*args[:i], tuple(args[i]), *args[i+1:])
1864+
args = (*args[:i], tuple(args[i]), *args[i + 1:])
18651865

18661866
alen = len(args)
18671867
plen = len(params)
@@ -2657,6 +2657,7 @@ def _unpack_args(*args):
26572657
newargs.append(arg)
26582658
return newargs
26592659

2660+
26602661
if _PEP_696_IMPLEMENTED:
26612662
from typing import TypeVarTuple
26622663

@@ -3134,11 +3135,13 @@ def wrapper(*args, **kwargs):
31343135
f"a class or callable, not {arg!r}"
31353136
)
31363137

3138+
31373139
def _is_param_expr(arg):
31383140
return arg is ... or isinstance(
31393141
arg, (tuple, list, ParamSpec, _ConcatenateGenericAlias)
31403142
)
31413143

3144+
31423145
# We have to do some monkey patching to deal with the dual nature of
31433146
# Unpack/TypeVarTuple:
31443147
# - We want Unpack to be a kind of TypeVar so it gets accepted in

0 commit comments

Comments
 (0)