Skip to content

Commit

Permalink
Fix isinstance with explicit (non generic) type alias (#18512)
Browse files Browse the repository at this point in the history
This is a partial revert of #18173 to unblock the 1.15 release

Fixes #18488
  • Loading branch information
hauntsaninja authored Jan 23, 2025
1 parent 5986883 commit 6519335
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
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

0 comments on commit 6519335

Please sign in to comment.