Skip to content

Commit f9ef036

Browse files
committed
Fix isinstance on namedtuple type aliases
1 parent bdef6ef commit f9ef036

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

mypy/checkexpr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
557557
and node
558558
and isinstance(node.node, TypeAlias)
559559
and not node.node.no_args
560+
and not (
561+
isinstance(target := get_proper_type(node.node.target), TupleType)
562+
and tuple_fallback(target).type.fullname != "builtins.tuple"
563+
)
560564
and not (
561565
isinstance(union_target := get_proper_type(node.node.target), UnionType)
562566
and (

test-data/unit/check-isinstance.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,6 +1900,21 @@ isinstance(x, It2) # E: Parameterized generics cannot be used with class or ins
19001900
[builtins fixtures/isinstance.pyi]
19011901
[typing fixtures/typing-full.pyi]
19021902

1903+
[case testIsinstanceTypeAliasToNamedTuple]
1904+
# flags: --warn-unreachable
1905+
from typing import Any, NamedTuple
1906+
from typing_extensions import TypeAlias
1907+
1908+
class Foo(NamedTuple):
1909+
pass
1910+
1911+
Alias: TypeAlias = Foo
1912+
1913+
def is_foo(x: Any) -> bool:
1914+
return isinstance(x, Alias)
1915+
[builtins fixtures/tuple.pyi]
1916+
[typing fixtures/typing-full.pyi]
1917+
19031918
[case testIssubclassTypeArgs]
19041919
# flags: --warn-unreachable
19051920
from typing import Iterable, TypeVar

0 commit comments

Comments
 (0)