From 9c03a89c7d42a6af355f891c003022e3e8101934 Mon Sep 17 00:00:00 2001 From: Cohen Karnell Date: Thu, 12 Sep 2019 14:10:02 -0400 Subject: [PATCH 1/7] Fix issue 7478 --- mypy/checkexpr.py | 4 ++++ test-data/unit/check-isinstance.test | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 9a9173899bdd..81269d252927 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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 isinstance(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/test-data/unit/check-isinstance.test b/test-data/unit/check-isinstance.test index 8729b87a297b..b5e3c674a934 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 a Any type +[builtins fixtures/isinstance.pyi] + [case testIsinstanceTypeArgsAliases] from typing import Iterable, TypeVar x = 1 From 4f60213fd75c6860203d6a42f6a7a983c314c5ad Mon Sep 17 00:00:00 2001 From: Cohen Karnell Date: Thu, 12 Sep 2019 14:28:38 -0400 Subject: [PATCH 2/7] Remove unneeded new line --- mypy/checkexpr.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 81269d252927..71e863defb31 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -283,8 +283,7 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> self.msg.cannot_use_function_with_type(e.callee.name, "Literal", e) continue if isinstance(node.node.target, AnyType): - self.msg.cannot_use_function_with_type( - e.callee.name, "Any", e) + self.msg.cannot_use_function_with_type(e.callee.name, "Any", e) continue if ((isinstance(typ, IndexExpr) and isinstance(typ.analyzed, (TypeApplication, TypeAliasExpr))) From 41557fc23217d3b67eda03d6c98432e5c9e06542 Mon Sep 17 00:00:00 2001 From: Cohen Karnell Date: Thu, 12 Sep 2019 14:50:26 -0400 Subject: [PATCH 3/7] Fix tests --- mypy/checkexpr.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 71e863defb31..d7f5bf50b9ec 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -282,13 +282,13 @@ 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 isinstance(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 isinstance(node.node, TypeAlias) and not node.node.no_args)): + if isinstance(node.node.target, AnyType): + self.msg.cannot_use_function_with_type(e.callee.name, "Any", e) + continue self.msg.type_arguments_not_allowed(e) if isinstance(typ, RefExpr) and isinstance(typ.node, TypeInfo): if typ.node.typeddict_type: From 2635f0848657c384e916f2f22713e9d34f40bf9f Mon Sep 17 00:00:00 2001 From: Cohen Karnell Date: Thu, 12 Sep 2019 15:49:01 -0400 Subject: [PATCH 4/7] Add guard and move variable definition to fix tests --- mypy/checkexpr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index d7f5bf50b9ec..925b53accaf5 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: @@ -286,7 +286,7 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> and isinstance(typ.analyzed, (TypeApplication, TypeAliasExpr))) or (isinstance(typ, NameExpr) and node and isinstance(node.node, TypeAlias) and not node.node.no_args)): - if isinstance(node.node.target, AnyType): + if node and isinstance(node.node.target, AnyType): self.msg.cannot_use_function_with_type(e.callee.name, "Any", e) continue self.msg.type_arguments_not_allowed(e) From 4544de2ce079589970ca05f9c591a2e52b2f2c52 Mon Sep 17 00:00:00 2001 From: Cohen Karnell Date: Thu, 12 Sep 2019 16:08:49 -0400 Subject: [PATCH 5/7] Fix type errors --- mypy/checkexpr.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 925b53accaf5..b69f86dc4b18 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -282,13 +282,14 @@ 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(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 isinstance(node.node, TypeAlias) and not node.node.no_args)): - if node and isinstance(node.node.target, AnyType): - self.msg.cannot_use_function_with_type(e.callee.name, "Any", e) - continue self.msg.type_arguments_not_allowed(e) if isinstance(typ, RefExpr) and isinstance(typ.node, TypeInfo): if typ.node.typeddict_type: From 9161d9afe08381acae8ec43b5d93b0d402e3c93c Mon Sep 17 00:00:00 2001 From: Cohen Karnell Date: Thu, 12 Sep 2019 16:22:46 -0400 Subject: [PATCH 6/7] Fix another type error --- mypy/checkexpr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index b69f86dc4b18..b4ae6acd5d2a 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -283,7 +283,7 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> self.msg.cannot_use_function_with_type(e.callee.name, "Literal", e) continue if (node and isinstance(node.node, TypeAlias) - and isinstance(node.node.target, AnyType)): + 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) From a7db28c034f4cc374ee8fcb51594030d9adc1526 Mon Sep 17 00:00:00 2001 From: Cohen Karnell Date: Fri, 13 Sep 2019 09:23:30 -0400 Subject: [PATCH 7/7] Address review --- mypy/messages.py | 2 +- test-data/unit/check-isinstance.test | 2 +- test-data/unit/check-literal.test | 16 ++++++++-------- test-data/unit/check-newtype.test | 4 ++-- test-data/unit/check-typeddict.test | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) 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 b5e3c674a934..2b62699b5166 100644 --- a/test-data/unit/check-isinstance.test +++ b/test-data/unit/check-isinstance.test @@ -1756,7 +1756,7 @@ isinstance(x, (int, (str, Iterable[int]))) # E: Parameterized generics cannot b [case testIsinstanceAnyAlias] from typing import Any A = Any -isinstance(object(), A) # E: Cannot use isinstance() with a Any type +isinstance(object(), A) # E: Cannot use isinstance() with Any type [builtins fixtures/isinstance.pyi] [case testIsinstanceTypeArgsAliases] 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] 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