Skip to content

Commit 3677494

Browse files
committed
lint fixes
1 parent d4e7b03 commit 3677494

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

typing_extensions/src_py3/typing_extensions.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,21 @@ def _no_slots_copy(dct):
2828
dict_copy.pop(slot, None)
2929
return dict_copy
3030

31-
32-
def _check_generic(cls, parameters):
33-
if not cls.__parameters__:
31+
def _check_generic(cls, parameters, elen):
32+
"""Check correct count for parameters of a generic cls (internal helper).
33+
This gives a nice error message in case of count mismatch.
34+
"""
35+
if not elen:
3436
raise TypeError(f"{cls} is not a generic class")
3537
alen = len(parameters)
36-
elen = len(cls.__parameters__)
3738
if alen != elen:
38-
raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments for {cls};"
39+
if hasattr(cls, "__parameters__"):
40+
num_tv_tuples = sum(
41+
isinstance(p, TypeVarTuple) for p in cls.__parameters__
42+
)
43+
if num_tv_tuples > 0 and alen >= elen - num_tv_tuples:
44+
return
45+
raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
3946
f" actual {alen}, expected {elen}")
4047

4148

@@ -602,7 +609,7 @@ def __class_getitem__(cls, params):
602609
"Parameters to Protocol[...] must all be unique")
603610
else:
604611
# Subscripting a regular Generic subclass.
605-
_check_generic(cls, params)
612+
_check_generic(cls, params, len(cls.__parameters__))
606613
return typing._GenericAlias(cls, params)
607614

608615
def __init_subclass__(cls, *args, **kwargs):
@@ -883,7 +890,7 @@ def __getitem__(self, params):
883890
elif self.__origin__ in (typing.Generic, Protocol):
884891
raise TypeError(f"Cannot subscript already-subscripted {repr(self)}")
885892
else:
886-
_check_generic(self, params)
893+
_check_generic(self, params, len(self.__parameters__))
887894
tvars = _type_vars(params)
888895
args = params
889896

@@ -2296,7 +2303,9 @@ def Unpack(self, parameters):
22962303
Shape = TypeVarTuple('Shape')
22972304
Batch = NewType('Batch', int)
22982305
2299-
def add_batch_axis(x: Array[Unpack[Shape]]) -> Array[Batch, Unpack[Shape]]: ...
2306+
def add_batch_axis(
2307+
x: Array[Unpack[Shape]]
2308+
) -> Array[Batch, Unpack[Shape]]: ...
23002309
23012310
"""
23022311
item = typing._type_check(parameters, f'{self._name} accepts only single type')
@@ -2317,22 +2326,6 @@ def _collect_type_vars(types):
23172326
return tuple(tvars)
23182327

23192328
typing._collect_type_vars = _collect_type_vars
2320-
2321-
def _check_generic(cls, parameters, elen):
2322-
"""Check correct count for parameters of a generic cls (internal helper).
2323-
This gives a nice error message in case of count mismatch.
2324-
"""
2325-
if not elen:
2326-
raise TypeError(f"{cls} is not a generic class")
2327-
alen = len(parameters)
2328-
if alen != elen:
2329-
if hasattr(cls, "__parameters__"):
2330-
num_tv_tuples = sum(isinstance(p, TypeVarTuple) for p in cls.__parameters__)
2331-
if num_tv_tuples > 0 and alen >= elen - num_tv_tuples:
2332-
return
2333-
raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
2334-
f" actual {alen}, expected {elen}")
2335-
23362329
typing._check_generic = _check_generic
23372330

23382331
elif sys.version_info[:2] >= (3, 7):
@@ -2355,7 +2348,9 @@ def __getitem__(self, parameters):
23552348
Shape = TypeVarTuple('Shape')
23562349
Batch = NewType('Batch', int)
23572350
2358-
def add_batch_axis(x: Array[Unpack[Shape]]) -> Array[Batch, Unpack[Shape]]: ...
2351+
def add_batch_axis(
2352+
x: Array[Unpack[Shape]]
2353+
) -> Array[Batch, Unpack[Shape]]: ...
23592354
23602355
""")
23612356
else:
@@ -2366,7 +2361,9 @@ class _Unpack(typing._FinalTypingBase, _root=True):
23662361
Shape = TypeVarTuple('Shape')
23672362
Batch = NewType('Batch', int)
23682363
2369-
def add_batch_axis(x: Array[Unpack[Shape]]) -> Array[Batch, Unpack[Shape]]: ...
2364+
def add_batch_axis(
2365+
x: Array[Unpack[Shape]]
2366+
) -> Array[Batch, Unpack[Shape]]: ...
23702367
23712368
"""
23722369
__slots__ = ('__type__',)
@@ -2407,7 +2404,6 @@ def __eq__(self, other):
24072404
Unpack = _Unpack(_root=True)
24082405

24092406

2410-
# Inherits from list as a workaround for Callable checks in Python < 3.9.2.
24112407
class TypeVarTuple(typing._Final, _root=True):
24122408
"""Type variable tuple.
24132409

0 commit comments

Comments
 (0)