Skip to content

Commit 7d084e9

Browse files
authored
Add constants for Concatenate and Unpack type names (#18553)
1 parent 05d3898 commit 7d084e9

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

mypy/semanal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@
268268
TYPE_CHECK_ONLY_NAMES,
269269
TYPE_VAR_LIKE_NAMES,
270270
TYPED_NAMEDTUPLE_NAMES,
271+
UNPACK_TYPE_NAMES,
271272
AnyType,
272273
CallableType,
273274
FunctionLike,
@@ -2286,7 +2287,7 @@ def analyze_unbound_tvar(self, t: Type) -> tuple[str, TypeVarLikeExpr] | None:
22862287
return self.analyze_unbound_tvar_impl(t.type, is_unpacked=True)
22872288
if isinstance(t, UnboundType):
22882289
sym = self.lookup_qualified(t.name, t)
2289-
if sym and sym.fullname in ("typing.Unpack", "typing_extensions.Unpack"):
2290+
if sym and sym.fullname in UNPACK_TYPE_NAMES:
22902291
inner_t = t.args[0]
22912292
if isinstance(inner_t, UnboundType):
22922293
return self.analyze_unbound_tvar_impl(inner_t, is_unpacked=True)
@@ -4171,7 +4172,7 @@ def analyze_type_alias_type_params(
41714172
base,
41724173
code=codes.TYPE_VAR,
41734174
)
4174-
if sym and sym.fullname in ("typing.Unpack", "typing_extensions.Unpack"):
4175+
if sym and sym.fullname in UNPACK_TYPE_NAMES:
41754176
self.note(
41764177
"Don't Unpack type variables in type_params", base, code=codes.TYPE_VAR
41774178
)

mypy/stubgen.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
DATACLASS_TRANSFORM_NAMES,
146146
OVERLOAD_NAMES,
147147
TPDICT_NAMES,
148+
TYPE_VAR_LIKE_NAMES,
148149
TYPED_NAMEDTUPLE_NAMES,
149150
AnyType,
150151
CallableType,
@@ -1090,14 +1091,7 @@ def is_alias_expression(self, expr: Expression, top_level: bool = True) -> bool:
10901091
or module alias.
10911092
"""
10921093
# Assignment of TypeVar(...) and other typevar-likes are passed through
1093-
if isinstance(expr, CallExpr) and self.get_fullname(expr.callee) in (
1094-
"typing.TypeVar",
1095-
"typing_extensions.TypeVar",
1096-
"typing.ParamSpec",
1097-
"typing_extensions.ParamSpec",
1098-
"typing.TypeVarTuple",
1099-
"typing_extensions.TypeVarTuple",
1100-
):
1094+
if isinstance(expr, CallExpr) and self.get_fullname(expr.callee) in TYPE_VAR_LIKE_NAMES:
11011095
return True
11021096
elif isinstance(expr, EllipsisExpr):
11031097
return not top_level

mypy/typeanal.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@
6262
from mypy.types import (
6363
ANNOTATED_TYPE_NAMES,
6464
ANY_STRATEGY,
65+
CONCATENATE_TYPE_NAMES,
6566
FINAL_TYPE_NAMES,
6667
LITERAL_TYPE_NAMES,
6768
NEVER_NAMES,
6869
TYPE_ALIAS_NAMES,
70+
UNPACK_TYPE_NAMES,
6971
AnyType,
7072
BoolTypeQuery,
7173
CallableArgument,
@@ -525,7 +527,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
525527
elif node.fullname in TYPE_ALIAS_NAMES:
526528
return AnyType(TypeOfAny.special_form)
527529
# Concatenate is an operator, no need for a proper type
528-
elif node.fullname in ("typing_extensions.Concatenate", "typing.Concatenate"):
530+
elif node.fullname in CONCATENATE_TYPE_NAMES:
529531
# We check the return type further up the stack for valid use locations
530532
return self.apply_concatenate_operator(t)
531533
else:
@@ -779,7 +781,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
779781
):
780782
# In most contexts, TypeGuard[...] acts as an alias for bool (ignoring its args)
781783
return self.named_type("builtins.bool")
782-
elif fullname in ("typing.Unpack", "typing_extensions.Unpack"):
784+
elif fullname in UNPACK_TYPE_NAMES:
783785
if len(t.args) != 1:
784786
self.fail("Unpack[...] requires exactly one type argument", t)
785787
return AnyType(TypeOfAny.from_error)
@@ -1503,7 +1505,7 @@ def analyze_callable_args_for_concatenate(
15031505
return None
15041506
if sym.node is None:
15051507
return None
1506-
if sym.node.fullname not in ("typing_extensions.Concatenate", "typing.Concatenate"):
1508+
if sym.node.fullname not in CONCATENATE_TYPE_NAMES:
15071509
return None
15081510

15091511
tvar_def = self.anal_type(callable_args, allow_param_spec=True)
@@ -1652,7 +1654,7 @@ def analyze_callable_args(
16521654
return None
16531655
elif (
16541656
isinstance(arg, UnboundType)
1655-
and self.refers_to_full_names(arg, ("typing_extensions.Unpack", "typing.Unpack"))
1657+
and self.refers_to_full_names(arg, UNPACK_TYPE_NAMES)
16561658
or isinstance(arg, UnpackType)
16571659
):
16581660
if seen_unpack:

mypy/types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@
130130
# Supported Annotated type names.
131131
ANNOTATED_TYPE_NAMES: Final = ("typing.Annotated", "typing_extensions.Annotated")
132132

133+
# Supported Concatenate type names.
134+
CONCATENATE_TYPE_NAMES: Final = ("typing.Concatenate", "typing_extensions.Concatenate")
135+
136+
# Supported Unpack type names.
137+
UNPACK_TYPE_NAMES: Final = ("typing.Unpack", "typing_extensions.Unpack")
138+
133139
# Supported @deprecated type names
134140
DEPRECATED_TYPE_NAMES: Final = ("warnings.deprecated", "typing_extensions.deprecated")
135141

0 commit comments

Comments
 (0)