diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 9a9173899bdd..b4ae6acd5d2a 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -272,8 +272,8 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> if (isinstance(e.callee, NameExpr) and e.callee.name in ('isinstance', 'issubclass') and len(e.args) == 2): for typ in mypy.checker.flatten(e.args[1]): + node = None if isinstance(typ, NameExpr): - node = None try: node = self.chk.lookup_qualified(typ.name) except KeyError: @@ -282,6 +282,10 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> if is_expr_literal_type(typ): self.msg.cannot_use_function_with_type(e.callee.name, "Literal", e) continue + if (node and isinstance(node.node, TypeAlias) + and isinstance(get_proper_type(node.node.target), AnyType)): + self.msg.cannot_use_function_with_type(e.callee.name, "Any", e) + continue if ((isinstance(typ, IndexExpr) and isinstance(typ.analyzed, (TypeApplication, TypeAliasExpr))) or (isinstance(typ, NameExpr) and node and diff --git a/mypy/messages.py b/mypy/messages.py index 7703bf29e32f..da5db1a4d72d 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -1123,7 +1123,7 @@ def concrete_only_call(self, typ: Type, context: Context) -> None: def cannot_use_function_with_type( self, method_name: str, type_name: str, context: Context) -> None: - self.fail("Cannot use {}() with a {} type".format(method_name, type_name), context) + self.fail("Cannot use {}() with {} type".format(method_name, type_name), context) def report_non_method_protocol(self, tp: TypeInfo, members: List[str], context: Context) -> None: diff --git a/test-data/unit/check-isinstance.test b/test-data/unit/check-isinstance.test index 8729b87a297b..2b62699b5166 100644 --- a/test-data/unit/check-isinstance.test +++ b/test-data/unit/check-isinstance.test @@ -1753,6 +1753,12 @@ isinstance(x, (int, (str, Iterable[int]))) # E: Parameterized generics cannot b [builtins fixtures/isinstancelist.pyi] [typing fixtures/typing-full.pyi] +[case testIsinstanceAnyAlias] +from typing import Any +A = Any +isinstance(object(), A) # E: Cannot use isinstance() with Any type +[builtins fixtures/isinstance.pyi] + [case testIsinstanceTypeArgsAliases] from typing import Iterable, TypeVar x = 1 diff --git a/test-data/unit/check-literal.test b/test-data/unit/check-literal.test index 56d9feac3fd4..f4098c6e34b9 100644 --- a/test-data/unit/check-literal.test +++ b/test-data/unit/check-literal.test @@ -1767,17 +1767,17 @@ import typing_extensions as indirect Alias = Literal[3] -isinstance(3, Literal[3]) # E: Cannot use isinstance() with a Literal type -isinstance(3, Alias) # E: Cannot use isinstance() with a Literal type \ +isinstance(3, Literal[3]) # E: Cannot use isinstance() with Literal type +isinstance(3, Alias) # E: Cannot use isinstance() with Literal type \ # E: The type alias to Literal is invalid in runtime context -isinstance(3, Renamed[3]) # E: Cannot use isinstance() with a Literal type -isinstance(3, indirect.Literal[3]) # E: Cannot use isinstance() with a Literal type +isinstance(3, Renamed[3]) # E: Cannot use isinstance() with Literal type +isinstance(3, indirect.Literal[3]) # E: Cannot use isinstance() with Literal type -issubclass(int, Literal[3]) # E: Cannot use issubclass() with a Literal type -issubclass(int, Alias) # E: Cannot use issubclass() with a Literal type \ +issubclass(int, Literal[3]) # E: Cannot use issubclass() with Literal type +issubclass(int, Alias) # E: Cannot use issubclass() with Literal type \ # E: The type alias to Literal is invalid in runtime context -issubclass(int, Renamed[3]) # E: Cannot use issubclass() with a Literal type -issubclass(int, indirect.Literal[3]) # E: Cannot use issubclass() with a Literal type +issubclass(int, Renamed[3]) # E: Cannot use issubclass() with Literal type +issubclass(int, indirect.Literal[3]) # E: Cannot use issubclass() with Literal type [builtins fixtures/isinstancelist.pyi] [out] diff --git a/test-data/unit/check-newtype.test b/test-data/unit/check-newtype.test index 822c73e8ed7f..2a49cc63738e 100644 --- a/test-data/unit/check-newtype.test +++ b/test-data/unit/check-newtype.test @@ -366,9 +366,9 @@ Any(5) from typing import NewType T = NewType('T', int) d: object -if isinstance(d, T): # E: Cannot use isinstance() with a NewType type +if isinstance(d, T): # E: Cannot use isinstance() with NewType type reveal_type(d) # N: Revealed type is '__main__.T' -issubclass(object, T) # E: Cannot use issubclass() with a NewType type +issubclass(object, T) # E: Cannot use issubclass() with NewType type [builtins fixtures/isinstancelist.pyi] [case testInvalidNewTypeCrash] diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index e867b5a3f92f..ee53c9eb203b 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -740,9 +740,9 @@ def set_coordinate(p: TaggedPoint, key: str, value: int) -> None: from mypy_extensions import TypedDict D = TypedDict('D', {'x': int}) d: object -if isinstance(d, D): # E: Cannot use isinstance() with a TypedDict type +if isinstance(d, D): # E: Cannot use isinstance() with TypedDict type reveal_type(d) # N: Revealed type is '__main__.D' -issubclass(object, D) # E: Cannot use issubclass() with a TypedDict type +issubclass(object, D) # E: Cannot use issubclass() with TypedDict type [builtins fixtures/isinstancelist.pyi]
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: