Skip to content

Commit a97a406

Browse files
committed
Add constants for Concatenate and Unpack type names
1 parent b82697b commit a97a406

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

mypy/semanal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@
263263
TYPE_CHECK_ONLY_NAMES,
264264
TYPE_VAR_LIKE_NAMES,
265265
TYPED_NAMEDTUPLE_NAMES,
266+
UNPACK_TYPE_NAMES,
266267
AnyType,
267268
CallableType,
268269
FunctionLike,
@@ -2281,7 +2282,7 @@ def analyze_unbound_tvar(self, t: Type) -> tuple[str, TypeVarLikeExpr] | None:
22812282
return self.analyze_unbound_tvar_impl(t.type, is_unpacked=True)
22822283
if isinstance(t, UnboundType):
22832284
sym = self.lookup_qualified(t.name, t)
2284-
if sym and sym.fullname in ("typing.Unpack", "typing_extensions.Unpack"):
2285+
if sym and sym.fullname in UNPACK_TYPE_NAMES:
22852286
inner_t = t.args[0]
22862287
if isinstance(inner_t, UnboundType):
22872288
return self.analyze_unbound_tvar_impl(inner_t, is_unpacked=True)
@@ -4166,7 +4167,7 @@ def analyze_type_alias_type_params(
41664167
base,
41674168
code=codes.TYPE_VAR,
41684169
)
4169-
if sym and sym.fullname in ("typing.Unpack", "typing_extensions.Unpack"):
4170+
if sym and sym.fullname in UNPACK_TYPE_NAMES:
41704171
self.note(
41714172
"Don't Unpack type variables in type_params", base, code=codes.TYPE_VAR
41724173
)

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)
@@ -1501,7 +1503,7 @@ def analyze_callable_args_for_concatenate(
15011503
return None
15021504
if sym.node is None:
15031505
return None
1504-
if sym.node.fullname not in ("typing_extensions.Concatenate", "typing.Concatenate"):
1506+
if sym.node.fullname not in CONCATENATE_TYPE_NAMES:
15051507
return None
15061508

15071509
tvar_def = self.anal_type(callable_args, allow_param_spec=True)
@@ -1650,7 +1652,7 @@ def analyze_callable_args(
16501652
return None
16511653
elif (
16521654
isinstance(arg, UnboundType)
1653-
and self.refers_to_full_names(arg, ("typing_extensions.Unpack", "typing.Unpack"))
1655+
and self.refers_to_full_names(arg, UNPACK_TYPE_NAMES)
16541656
or isinstance(arg, UnpackType)
16551657
):
16561658
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)