Skip to content

Commit

Permalink
Fix use of TypeAlias from aliased imports (#12180)
Browse files Browse the repository at this point in the history
Fixes #12179
  • Loading branch information
hauntsaninja authored Feb 15, 2022
1 parent 595fec9 commit cd3dfbd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2643,7 +2643,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:

pep_613 = False
if s.unanalyzed_type is not None and isinstance(s.unanalyzed_type, UnboundType):
lookup = self.lookup(s.unanalyzed_type.name, s, suppress_errors=True)
lookup = self.lookup_qualified(s.unanalyzed_type.name, s, suppress_errors=True)
if lookup and lookup.fullname in TYPE_ALIAS_NAMES:
pep_613 = True
if not pep_613 and s.unanalyzed_type is not None:
Expand Down
26 changes: 25 additions & 1 deletion test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Never = NoReturn
a: Never # Used to be an error here

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

[case testAliasedImportPep613]
import typing as tpp
import typing_extensions as tpx
from typing import TypeAlias as TPA
from typing_extensions import TypeAlias as TXA
import typing
import typing_extensions

Int1: tpp.TypeAlias = int
Int2: tpx.TypeAlias = int
Int3: TPA = int
Int4: TXA = int
Int5: typing.TypeAlias = int
Int6: typing_extensions.TypeAlias = int

x1: Int1 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x2: Int2 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x3: Int3 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x4: Int4 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x5: Int5 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x6: Int6 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-medium.pyi]

[case testFunctionScopePep613]
from typing_extensions import TypeAlias

Expand Down
1 change: 1 addition & 0 deletions test-data/unit/fixtures/typing-medium.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Literal = 0
TypedDict = 0
NoReturn = 0
NewType = 0
TypeAlias = 0

T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
Expand Down

0 comments on commit cd3dfbd

Please sign in to comment.