8000 Fix `typing.TypeAliasType` being undefined on python < 3.12 (#17558) · python/mypy@b202f30 · GitHub
[go: up one dir, main page]

Skip to content

Commit b202f30

Browse files
authored
Fix typing.TypeAliasType being undefined on python < 3.12 (#17558)
Closes #17554 CC @JukkaL Refs #17320
1 parent 6aa46f0 commit b202f30

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

mypy/checkexpr.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4679,7 +4679,7 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
46794679
"""
46804680
if isinstance(tapp.expr, RefExpr) and isinstance(tapp.expr.node, TypeAlias):
46814681
if tapp.expr.node.python_3_12_type_alias:
4682-
return self.named_type("typing.TypeAliasType")
4682+
return self.type_alias_type_type()
46834683
# Subscription of a (generic) alias in runtime context, expand the alias.
46844684
item = instantiate_type_alias(
46854685
tapp.expr.node,
@@ -4743,7 +4743,7 @@ class LongName(Generic[T]): ...
47434743
y = cast(A, ...)
47444744
"""
47454745
if alias.python_3_12_type_alias:
4746-
return self.named_type("typing.TypeAliasType")
4746+
return self.type_alias_type_type()
47474747
if isinstance(alias.target, Instance) and alias.target.invalid: # type: ignore[misc]
47484748
# An invalid alias, error already has been reported
47494749
return AnyType(TypeOfAny.from_error)
@@ -5863,6 +5863,12 @@ def named_type(self, name: str) -> Instance:
58635863
"""
58645864
return self.chk.named_type(name)
58655865

5866+
def type_alias_type_type(self) -> Instance:
5867+
"""Returns a `typing.TypeAliasType` or `typing_extensions.TypeAliasType`."""
5868+
if self.chk.options.python_version >= (3, 12):
5869+
return self.named_type("typing.TypeAliasType")
5870+
return self.named_type("typing_extensions.TypeAliasType")
5871+
58665872
def is_valid_var_arg(self, typ: Type) -> bool:
58675873
"""Is a type valid as a *args argument?"""
58685874
typ = get_proper_type(typ)

test-data/unit/check-type-aliases.test

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ x: TestType = 42
10741074
y: TestType = 'a'
10751075
z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[int, str]")
10761076

1077-
reveal_type(TestType) # N: Revealed type is "typing.TypeAliasType"
1077+
reveal_type(TestType) # N: Revealed type is "typing_extensions.TypeAliasType"
10781078
TestType() # E: "TypeAliasType" not callable
10791079

10801080
class A:
@@ -1084,6 +1084,15 @@ yc: A.ClassAlias = "" # E: Incompatible types in assignment (expression has typ
10841084
[builtins fixtures/tuple.pyi]
10851085
[typing fixtures/typing-full.pyi]
10861086

1087+
[case testTypeAliasTypePython311]
1088+
# flags: --python-version 3.11
1089+
# Pinning to 3.11, because 3.12 has `TypeAliasType`
1090+
from typing_extensions import TypeAliasType
1091+
1092+
TestType = TypeAliasType("TestType", int)
1093+
x: TestType = 1
1094+
[builtins fixtures/tuple.pyi]
1095+
10871096
[case testTypeAliasTypeInvalid]
10881097
from typing_extensions import TypeAliasType
10891098

0 commit comments

Comments
 (0)
0