From 8b617551711a4e8061a3cacc023ebacb19beab80 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 5 Jan 2022 20:50:30 +0000 Subject: [PATCH 1/6] Initial commit --- mypy/semanal.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 29dfc96c2422..cf9d21ce8a60 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -2201,11 +2201,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: From dfd1a272ff10ec3fd86ff39785c4d291aa8a3952 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 20 Jan 2022 00:49:34 +0000 Subject: [PATCH 2/6] Add a regression test --- test-data/unit/pythoneval.test | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 9bf511e1aba3..834afb2c0142 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1573,3 +1573,28 @@ x: OrderedDict[str, str] = OrderedDict({}) reveal_type(x) # Revealed type is "collections.OrderedDict[builtins.str, builtins.int]" [out] _testTypingExtensionsOrderedDictAlias.py:3: note: Revealed type is "collections.OrderedDict[builtins.str, builtins.str]" + +[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]" \ No newline at end of file From 6ad779026f31ae24149408dc4795adcaeb4c94dd Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 20 Jan 2022 13:27:57 +0000 Subject: [PATCH 3/6] Move test --- test-data/unit/check-union-or-syntax.test | 24 ++++++++++++++++++++ test-data/unit/pythoneval.test | 27 +---------------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index 3e5a19b8fe61..ca38c2ab0e26 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -196,3 +196,27 @@ def f(x: Union[int, str, None]) -> None: else: reveal_type(x) # N: Revealed type is "None" [builtins fixtures/isinstance.pyi] + +[case testImplicit604TypeAliasWithCyclicImport] +# flags: --python-version 3.10 +from was_builtins import foo +reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]" +[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 diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index eff8ebf6fba2..827c5b7c7c31 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1584,29 +1584,4 @@ class Foo(Enum): [out] _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]" +_testEnumValueWithPlaceholderNodeType.py:6: error: Name "Missing" is not defined \ No newline at end of file From 2890f8f40577d6d00076412ffe76b21b2a9279e0 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 20 Jan 2022 13:43:56 +0000 Subject: [PATCH 4/6] Add x-fail test for `.py` files --- test-data/unit/check-union-or-syntax.test | 27 ++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index ca38c2ab0e26..5d4b95481052 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -197,7 +197,7 @@ def f(x: Union[int, str, None]) -> None: reveal_type(x) # N: Revealed type is "None" [builtins fixtures/isinstance.pyi] -[case testImplicit604TypeAliasWithCyclicImport] +[case testImplicit604TypeAliasWithCyclicImportInStub] # flags: --python-version 3.10 from was_builtins import foo reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]" @@ -220,3 +220,28 @@ ReadableBuffer = str | WriteableBuffer from was_builtins import * import was_abc import was_collections + +# TODO: Get this test to pass +[case testImplicit604TypeAliasWithCyclicImportNotInStub-xfail] +# flags: --python-version 3.10 +from was_builtins import foo +reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]" +[file was_abc.py] +from was_builtins import * +[file was_builtins.py] +import was_typing +from was_typeshed import ReadableBuffer +foo: ReadableBuffer +[file was_collections.py] +from was_builtins import * +[file was_mmap.py] +from was_builtins import * +class mmap: ... +[file was_typeshed.py] +import was_mmap +WriteableBuffer = was_mmap.mmap +ReadableBuffer = str | WriteableBuffer +[file was_typing.py] +from was_builtins import * +import was_abc +import was_collections From 8f2cc842b72066d40e369688bdb75cb5dc64404f Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 20 Jan 2022 14:28:18 +0000 Subject: [PATCH 5/6] Add back whitespace --- test-data/unit/pythoneval.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 827c5b7c7c31..ab1593dac754 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1584,4 +1584,4 @@ class Foo(Enum): [out] _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 \ No newline at end of file +_testEnumValueWithPlaceholderNodeType.py:6: error: Name "Missing" is not defined From 964a7de191445944c2caa5d5079cc6ca15a140af Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 30 Jan 2022 22:53:21 +0000 Subject: [PATCH 6/6] Reduce size of regression tests --- test-data/unit/check-union-or-syntax.test | 34 ++++------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index 5d4b95481052..58526cfd0623 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -201,47 +201,25 @@ def f(x: Union[int, str, None]) -> None: # flags: --python-version 3.10 from was_builtins import foo reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]" -[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] +foo: ReadableBuffer +[file was_mmap.pyi] from was_builtins import * -import was_abc -import was_collections +class mmap: ... # TODO: Get this test to pass [case testImplicit604TypeAliasWithCyclicImportNotInStub-xfail] # flags: --python-version 3.10 from was_builtins import foo reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]" -[file was_abc.py] -from was_builtins import * [file was_builtins.py] -import was_typing -from was_typeshed import ReadableBuffer -foo: ReadableBuffer -[file was_collections.py] -from was_builtins import * -[file was_mmap.py] -from was_builtins import * -class mmap: ... -[file was_typeshed.py] import was_mmap WriteableBuffer = was_mmap.mmap ReadableBuffer = str | WriteableBuffer -[file was_typing.py] +foo: ReadableBuffer +[file was_mmap.py] from was_builtins import * -import was_abc -import was_collections +class mmap: ...