8000 Fix isinstance with type aliases to PEP 604 unions by hauntsaninja · Pull Request #17371 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix isinstance with type aliases to PEP 604 unions #17371

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 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
.
  • Loading branch information
hauntsaninja committed Jun 12, 2024
commit bd96ef6ba3e46eeca5c8f1afb3c8ab96495c18b7
4 changes: 2 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
and isinstance(node.node, TypeAlias)
and not node.node.no_args
and not (
isinstance(node.node.target, UnionType)
and node.node.target.uses_pep604_syntax
isinstance(union_target := get_proper_type(node.node.target), UnionType)
and union_target.uses_pep604_syntax
)
):
self.msg.type_arguments_not_allowed(e)
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ c.SpecialExplicit = 4

[case testNewStyleUnionInTypeAliasWithMalformedInstance]
# flags: --python-version 3.10
import types
from typing import List

A = List[int, str] | int # E: "list" expects 1 type argument, but 2 given
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-union-or-syntax.test
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def f(x: Union[int, str, None]) -> None:

[case testImplicit604TypeAliasWithCyclicImportInStub]
# flags: --python-version 3.10
import types
from was_builtins import foo
reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]"
[file was_builtins.pyi]
Expand Down
5 changes: 4 additions & 1 deletion test-data/unit/fixtures/type.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ class int: pass
class str: pass
class ellipsis: pass

def isinstance(obj: object, class_or_tuple: type | types.UnionType, /) -> bool: ...
if sys.version_info >= (3, 10): # type: ignore
def isinstance(obj: object, class_or_tuple: type | types.UnionType, /) -> bool: ...
else:
def isinstance(obj: object, class_or_tuple: type, /) -> bool: ...
0