Skip to content

Fix use of TypeAlias from aliased imports #12180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
with:
python-version: '3.7'
- name: Install tox
run: pip install --upgrade 'setuptools!=50' 'virtualenv<16.7.11' tox==3.20.1
run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.24.5
- name: Setup tox environment
run: tox -e ${{ env.TOXENV }} --notest
- name: Test
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
./misc/build-debug-python.sh $PYTHONVERSION $PYTHONDIR $VENV
source $VENV/bin/activate
- name: Install tox
run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.20.1
run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.24.5
- name: Compiled with mypyc
if: ${{ matrix.test_mypyc }}
run: |
Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2647,7 +2647,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