Skip to content

Fix issue 7478 #7504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-newtype.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down