Skip to content

Fix isinstance with explicit (non generic) type alias #18512

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 1 commit into from
Jan 23, 2025
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
1 change: 0 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4022,7 +4022,6 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
and not res.args
and not empty_tuple_index
and not pep_695
and not pep_613
)
if isinstance(res, ProperType) and isinstance(res, Instance):
if not validate_instance(res, self.fail, empty_tuple_index):
Expand Down
31 changes: 11 additions & 20 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -1243,31 +1243,22 @@ A = Union[int, List[A]]
def func(x: A) -> int: ...
[builtins fixtures/tuple.pyi]

[case testAliasExplicitNoArgsBasic]
from typing import Any, List, assert_type
[case testAliasNonGeneric]
from typing_extensions import TypeAlias
class Foo: ...

Implicit = List
Explicit: TypeAlias = List
ImplicitFoo = Foo
ExplicitFoo: TypeAlias = Foo

x1: Implicit[str]
x2: Explicit[str] # E: Bad number of arguments for type alias, expected 0, given 1
assert_type(x1, List[str])
assert_type(x2, List[Any])
[builtins fixtures/tuple.pyi]

[case testAliasExplicitNoArgsGenericClass]
# flags: --python-version 3.9
from typing import Any, assert_type
from typing_extensions import TypeAlias
x1: ImplicitFoo[str] # E: "Foo" expects no type arguments, but 1 given
x2: ExplicitFoo[str] # E: "Foo" expects no type arguments, but 1 given

Implicit = list
Explicit: TypeAlias = list
def is_foo(x: object):
if isinstance(x, ImplicitFoo):
pass
if isinstance(x, ExplicitFoo):
pass

x1: Implicit[str]
x2: Explicit[str] # E: Bad number of arguments for type alias, expected 0, given 1
assert_type(x1, list[str])
assert_type(x2, list[Any])
[builtins fixtures/tuple.pyi]

[case testAliasExplicitNoArgsTuple]
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/diff.test
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,7 @@ type H[T] = int
__main__.A
__main__.C
__main__.D
__main__.E
__main__.G
__main__.H

Expand Down
Loading