Skip to content

Commit d60d225

Browse files
authored
simplify typing.py
remove redundant code
1 parent fbe18bd commit d60d225

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

Lib/typing.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def _collect_type_parameters(args, *, enforce_default_ordering: bool = True):
276276
for t in args:
277277
if isinstance(t, type):
278278
# We don't want __parameters__ descriptor of a bare Python class.
279-
pass
279+
continue
280280
elif isinstance(t, tuple):
281281
# `t` might be a tuple, when `ParamSpec` is substituted with
282282
# `[T, int]`, or `[int, *Ts]`, etc.
@@ -498,8 +498,7 @@ def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=f
498498
return GenericAlias(t.__origin__, ev_args)
499499
if isinstance(t, types.UnionType):
500500
return functools.reduce(operator.or_, ev_args)
501-
else:
502-
return t.copy_with(ev_args)
501+
return t.copy_with(ev_args)
503502
return t
504503

505504

@@ -1140,14 +1139,9 @@ def _typevar_subst(self, arg):
11401139
def _typevartuple_prepare_subst(self, alias, args):
11411140
params = alias.__parameters__
11421141
typevartuple_index = params.index(self)
1143-
for param in params[typevartuple_index + 1:]:
1144-
if isinstance(param, TypeVarTuple):
1145-
raise TypeError(f"More than one TypeVarTuple parameter in {alias}")
1142+
if any(isinstance(param, TypeVarTuple) for param in params[typevartuple_index + 1:]):
1143+
raise TypeError(f"More than one TypeVarTuple parameter in {alias}")
11461144

1147-
alen = len(args)
1148-
plen = len(params)
1149-
left = typevartuple_index
1150-
right = plen - typevartuple_index - 1
11511145
var_tuple_index = None
11521146
fillarg = None
11531147
for k, arg in enumerate(args):
@@ -1158,6 +1152,11 @@ def _typevartuple_prepare_subst(self, alias, args):
11581152
raise TypeError("More than one unpacked arbitrary-length tuple argument")
11591153
var_tuple_index = k
11601154
fillarg = subargs[0]
1155+
1156+
alen = len(args)
1157+
plen = len(params)
1158+
left = typevartuple_index
1159+
right = plen - typevartuple_index - 1
11611160
if var_tuple_index is not None:
11621161
left = min(left, var_tuple_index)
11631162
right = min(right, alen - var_tuple_index - 1)
@@ -1783,16 +1782,10 @@ def __repr__(self):
17831782
return super().__repr__()
17841783

17851784
def __instancecheck__(self, obj):
1786-
for arg in self.__args__:
1787-
if isinstance(obj, arg):
1788-
return True
1789-
return False
1785+
return any(True for arg in self.__args__ if issubclass(obj, arg))
17901786

17911787
def __subclasscheck__(self, cls):
1792-
for arg in self.__args__:
1793-
if issubclass(cls, arg):
1794-
return True
1795-
return False
1788+
return any(True for arg in self.__args__ if issubclass(cls, arg))
17961789

17971790
def __reduce__(self):
17981791
func, (origin, args) = super().__reduce__()
@@ -2052,9 +2045,7 @@ class _ProtocolMeta(ABCMeta):
20522045
# This metaclass is somewhat unfortunate,
20532046
# but is necessary for several reasons...
20542047
def __new__(mcls, name, bases, namespace, /, **kwargs):
2055-
if name == "Protocol" and bases == (Generic,):
2056-
pass
2057-
elif Protocol in bases:
2048+
if (name != "Protocol" or bases != (Generic,)) and Protocol in bases:
20582049
for base in bases:
20592050
if not (
20602051
base in {object, Generic}
@@ -2471,18 +2462,15 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
24712462
while hasattr(nsobj, '__wrapped__'):
24722463
nsobj = nsobj.__wrapped__
24732464
globalns = getattr(nsobj, '__globals__', {})
2474-
if localns is None:
2475-
localns = globalns
2476-
elif localns is None:
2465+
if localns is None:
24772466
localns = globalns
24782467
hints = getattr(obj, '__annotations__', None)
24792468
if hints is None:
24802469
# Return empty annotations for something that _could_ have them.
24812470
if isinstance(obj, _allowed_types):
24822471
return {}
2483-
else:
2484-
raise TypeError('{!r} is not a module, class, method, '
2485-
'or function.'.format(obj))
2472+
raise TypeError('{!r} is not a module, class, method, '
2473+
'or function.'.format(obj))
24862474
hints = dict(hints)
24872475
type_params = getattr(obj, "__type_params__", ())
24882476
for name, value in hints.items():

0 commit comments

Comments
 (0)