Skip to content

Commit cb44e4d

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

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
@@ -4682,7 +4682,7 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
46824682
"""
46834683
if isinstance(tapp.expr, RefExpr) and isinstance(tapp.expr.node, TypeAlias):
46844684
if tapp.expr.node.python_3_12_type_alias:
4685-
return self.named_type("typing.TypeAliasType")
4685+
return self.type_alias_type_type()
46864686
# Subscription of a (generic) alias in runtime context, expand the alias.
46874687
item = instantiate_type_alias(
46884688
tapp.expr.node,
@@ -4746,7 +4746,7 @@ class LongName(Generic[T]): ...
47464746
y = cast(A, ...)
47474747
"""
47484748
if alias.python_3_12_type_alias:
4749-
return self.named_type("typing.TypeAliasType")
4749+
return self.type_alias_type_type()
47504750
if isinstance(alias.target, Instance) and alias.target.invalid: # type: ignore[misc]
47514751
# An invalid alias, error already has been reported
47524752
return AnyType(TypeOfAny.from_error)
@@ -5862,6 +5862,12 @@ def named_type(self, name: str) -> Instance:
58625862
"""
58635863
return self.chk.named_type(name)
58645864

5865+
def type_alias_type_type(self) -> Instance:
5866+
"""Returns a `typing.TypeAliasType` or `typing_extensions.TypeAliasType`."""
5867+
if self.chk.options.python_version >= (3, 12):
5868+
return self.named_type("typing.TypeAliasType")
5869+
return self.named_type("typing_extensions.TypeAliasType")
5870+
58655871
def is_valid_var_arg(self, typ: Type) -> bool:
58665872
"""Is a type valid as a *args argument?"""
58675873
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy