Skip to content

Commit cd3dfbd

Browse files
authored
Fix use of TypeAlias from aliased imports (#12180)
Fixes #12179
1 parent 595fec9 commit cd3dfbd

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2643,7 +2643,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
26432643

26442644
pep_613 = False
26452645
if s.unanalyzed_type is not None and isinstance(s.unanalyzed_type, UnboundType):
2646-
lookup = self.lookup(s.unanalyzed_type.name, s, suppress_errors=True)
2646+
lookup = self.lookup_qualified(s.unanalyzed_type.name, s, suppress_errors=True)
26472647
if lookup and lookup.fullname in TYPE_ALIAS_NAMES:
26482648
pep_613 = True
26492649
if not pep_613 and s.unanalyzed_type is not None:

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Never = NoReturn
5757
a: Never # Used to be an error here
5858

5959
def f(a: Never): ...
60-
f(5) # E: Argument 1 to "f" has incompatible type "int"; expected "NoReturn"
60+
f(5) # E: Argument 1 to "f" has incompatible type "int"; expected "NoReturn"
6161
[case testImportUnionAlias]
6262
import typing
6363
from _m import U
@@ -702,6 +702,30 @@ x: TypeAlias = list(int) # E: Invalid type alias: expression is not a valid typ
702702
a: x
703703
[builtins fixtures/tuple.pyi]
704704

705+
[case testAliasedImportPep613]
706+
import typing as tpp
707+
import typing_extensions as tpx
708+
from typing import TypeAlias as TPA
709+
from typing_extensions import TypeAlias as TXA
710+
import typing
711+
import typing_extensions
712+
713+
Int1: tpp.TypeAlias = int
714+
Int2: tpx.TypeAlias = int
715+
Int3: TPA = int
716+
Int4: TXA = int
717+
Int5: typing.TypeAlias = int
718+
Int6: typing_extensions.TypeAlias = int
719+
720+
x1: Int1 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
721+
x2: Int2 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
722+
x3: Int3 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
723+
x4: Int4 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
724+
x5: Int5 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
725+
x6: Int6 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
726+
[builtins fixtures/tuple.pyi]
727+
[typing fixtures/typing-medium.pyi]
728+
705729
[case testFunctionScopePep613]
706730
from typing_extensions import TypeAlias
707731

test-data/unit/fixtures/typing-medium.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Literal = 0
2626
TypedDict = 0
2727
NoReturn = 0
2828
NewType = 0
29+
TypeAlias = 0
2930

3031
T = TypeVar('T')
3132
T_co = TypeVar('T_co', covariant=True)

0 commit comments

Comments
 (0)