Skip to content

Commit 8782ae7

Browse files
ckarnellJukkaL
authored andcommitted
Fix message when second argument to isinstance is an alias to Any (python#7504)
Fixes python#7478.
1 parent 125ef0d commit 8782ae7

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
272272
if (isinstance(e.callee, NameExpr) and e.callee.name in ('isinstance', 'issubclass')
273273
and len(e.args) == 2):
274274
for typ in mypy.checker.flatten(e.args[1]):
275+
node = None
275276
if isinstance(typ, NameExpr):
276-
node = None
277277
try:
278278
node = self.chk.lookup_qualified(typ.name)
279279
except KeyError:
@@ -282,6 +282,10 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
282282
if is_expr_literal_type(typ):
283283
self.msg.cannot_use_function_with_type(e.callee.name, "Literal", e)
284284
continue
285+
if (node and isinstance(node.node, TypeAlias)
286+
and isinstance(get_proper_type(node.node.target), AnyType)):
287+
self.msg.cannot_use_function_with_type(e.callee.name, "Any", e)
288+
continue
285289
if ((isinstance(typ, IndexExpr)
286290
and isinstance(typ.analyzed, (TypeApplication, TypeAliasExpr)))
287291
or (isinstance(typ, NameExpr) and node and

mypy/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ def concrete_only_call(self, typ: Type, context: Context) -> None:
11241124

11251125
def cannot_use_function_with_type(
11261126
self, method_name: str, type_name: str, context: Context) -> None:
1127-
self.fail("Cannot use {}() with a {} type".format(method_name, type_name), context)
1127+
self.fail("Cannot use {}() with {} type".format(method_name, type_name), context)
11281128

11291129
def report_non_method_protocol(self, tp: TypeInfo, members: List[str],
11301130
context: Context) -> None:

test-data/unit/check-isinstance.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,12 @@ isinstance(x, (int, (str, Iterable[int]))) # E: Parameterized generics cannot b
17531753
[builtins fixtures/isinstancelist.pyi]
17541754
[typing fixtures/typing-full.pyi]
17551755

1756+
[case testIsinstanceAnyAlias]
1757+
from typing import Any
1758+
A = Any
1759+
isinstance(object(), A) # E: Cannot use isinstance() with Any type
1760+
[builtins fixtures/isinstance.pyi]
1761+
17561762
[case testIsinstanceTypeArgsAliases]
17571763
from typing import Iterable, TypeVar
17581764
x = 1

test-data/unit/check-literal.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,17 +1767,17 @@ import typing_extensions as indirect
17671767

17681768
Alias = Literal[3]
17691769

1770-
isinstance(3, Literal[3]) # E: Cannot use isinstance() with a Literal type
1771-
isinstance(3, Alias) # E: Cannot use isinstance() with a Literal type \
1770+
isinstance(3, Literal[3]) # E: Cannot use isinstance() with Literal type
1771+
isinstance(3, Alias) # E: Cannot use isinstance() with Literal type \
17721772
# E: The type alias to Literal is invalid in runtime context
1773-
isinstance(3, Renamed[3]) # E: Cannot use isinstance() with a Literal type
1774-
isinstance(3, indirect.Literal[3]) # E: Cannot use isinstance() with a Literal type
1773+
isinstance(3, Renamed[3]) # E: Cannot use isinstance() with Literal type
1774+
isinstance(3, indirect.Literal[3]) # E: Cannot use isinstance() with Literal type
17751775

1776-
issubclass(int, Literal[3]) # E: Cannot use issubclass() with a Literal type
1777-
issubclass(int, Alias) # E: Cannot use issubclass() with a Literal type \
1776+
issubclass(int, Literal[3]) # E: Cannot use issubclass() with Literal type
1777+
issubclass(int, Alias) # E: Cannot use issubclass() with Literal type \
17781778
# E: The type alias to Literal is invalid in runtime context
1779-
issubclass(int, Renamed[3]) # E: Cannot use issubclass() with a Literal type
1780-
issubclass(int, indirect.Literal[3]) # E: Cannot use issubclass() with a Literal type
1779+
issubclass(int, Renamed[3]) # E: Cannot use issubclass() with Literal type
1780+
issubclass(int, indirect.Literal[3]) # E: Cannot use issubclass() with Literal type
17811781
[builtins fixtures/isinstancelist.pyi]
17821782
[out]
17831783

test-data/unit/check-newtype.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ Any(5)
366366
from typing import NewType
367367
T = NewType('T', int)
368368
d: object
369-
if isinstance(d, T): # E: Cannot use isinstance() with a NewType type
369+
if isinstance(d, T): # E: Cannot use isinstance() with NewType type
370370
reveal_type(d) # N: Revealed type is '__main__.T'
371-
issubclass(object, T) # E: Cannot use issubclass() with a NewType type
371+
issubclass(object, T) # E: Cannot use issubclass() with NewType type
372372
[builtins fixtures/isinstancelist.pyi]
373373

374374
[case testInvalidNewTypeCrash]

test-data/unit/check-typeddict.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,9 @@ def set_coordinate(p: TaggedPoint, key: str, value: int) -> None:
740740
from mypy_extensions import TypedDict
741741
D = TypedDict('D', {'x': int})
742742
d: object
743-
if isinstance(d, D): # E: Cannot use isinstance() with a TypedDict type
743+
if isinstance(d, D): # E: Cannot use isinstance() with TypedDict type
744744
reveal_type(d) # N: Revealed type is '__main__.D'
745-
issubclass(object, D) # E: Cannot use issubclass() with a TypedDict type
745+
issubclass(object, D) # E: Cannot use issubclass() with TypedDict type
746746
[builtins fixtures/isinstancelist.pyi]
747747

748748

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy