Skip to content

Fix "Implicit type aliases not recognised with PEP 604 + import cycles" (but only for stubs) #11915

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 8 commits into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 8 additions & 5 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2177,11 +2177,14 @@ def can_be_type_alias(self, rv: Expression, allow_none: bool = False) -> bool:
return True
if allow_none and isinstance(rv, NameExpr) and rv.fullname == 'builtins.None':
return True
if (isinstance(rv, OpExpr)
and rv.op == '|'
and self.can_be_type_alias(rv.left, allow_none=True)
and self.can_be_type_alias(rv.right, allow_none=True)):
return True
if isinstance(rv, OpExpr) and rv.op == '|':
if self.is_stub_file:
return True
if (
self.can_be_type_alias(rv.left, allow_none=True)
and self.can_be_type_alias(rv.right, allow_none=True)
):
return True
return False

def is_type_ref(self, rv: Expression, bare: bool = False) -> bool:
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1585,3 +1585,28 @@ class Foo(Enum):
_testEnumValueWithPlaceholderNodeType.py:5: error: Incompatible types in assignment (expression has type "object", variable has type "Foo")
_testEnumValueWithPlaceholderNodeType.py:6: error: Incompatible types in assignment (expression has type "object", variable has type "Foo")
_testEnumValueWithPlaceholderNodeType.py:6: error: Name "Missing" is not defined

[case testImplicit604TypeAliasWithCyclicImport]
from was_builtins import foo
reveal_type(foo)
[file was_abc.pyi]
from was_builtins import *
[file was_builtins.pyi]
import was_typing
from was_typeshed import ReadableBuffer
foo: ReadableBuffer
[file was_collections.pyi]
from was_builtins import *
[file was_mmap.pyi]
from was_builtins import *
class mmap: ...
[file was_typeshed.pyi]
import was_mmap
WriteableBuffer = was_mmap.mmap
ReadableBuffer = str | WriteableBuffer
[file was_typing.pyi]
from was_builtins import *
import was_abc
import was_collections
[out]
_testImplicit604TypeAliasWithCyclicImport.py:2: note: Revealed type is "Union[builtins.str, was_mmap.mmap]"