Skip to content

Warn on type arguments in isinstance #3040

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 5 commits into from
Apr 13, 2017
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
13 changes: 13 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type:
e.callee.node.typeddict_type is not None:
return self.check_typeddict_call(e.callee.node.typeddict_type,
e.arg_kinds, e.arg_names, e.args, e)
if isinstance(e.callee, NameExpr) and e.callee.name in ('isinstance', 'issubclass'):
for typ in mypy.checker.flatten(e.args[1]):
if isinstance(typ, NameExpr):
try:
node = self.chk.lookup_qualified(typ.name)
except KeyError:
# Undefined names should already be reported in semantic analysis.
node = None
if (isinstance(typ, IndexExpr)
and isinstance(typ.analyzed, (TypeApplication, TypeAliasExpr))
# node.kind == TYPE_ALIAS only for aliases like It = Iterable[int].
or isinstance(typ, NameExpr) and node and node.kind == nodes.TYPE_ALIAS):
self.msg.type_arguments_not_allowed(e)
self.try_infer_partial_type(e)
callee_type = self.accept(e.callee)
if (self.chk.options.disallow_untyped_calls and
Expand Down
3 changes: 3 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,9 @@ def typeddict_item_name_not_found(self,
self.fail('\'{}\' is not a valid item name; expected one of {}'.format(
item_name, format_item_name_list(typ.items.keys())), context)

def type_arguments_not_allowed(self, context: Context) -> None:
self.fail('Parameterized generics cannot be used with class or instance checks', context)


def capitalize(s: str) -> str:
"""Capitalize the first character of a string."""
Expand Down
39 changes: 39 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,45 @@ def f(x: Union[int, A], a: Type[A]) -> None:
[builtins fixtures/isinstancelist.pyi]


[case testIsinstanceTypeArgs]
from typing import Iterable, TypeVar
x = 1
T = TypeVar('T')

isinstance(x, Iterable)
isinstance(x, Iterable[int]) # E: Parameterized generics cannot be used with class or instance checks
isinstance(x, Iterable[T]) # E: Parameterized generics cannot be used with class or instance checks
isinstance(x, (int, Iterable[int])) # E: Parameterized generics cannot be used with class or instance checks
isinstance(x, (int, (str, Iterable[int]))) # E: Parameterized generics cannot be used with class or instance checks

[builtins fixtures/isinstancelist.pyi]

[case testIsinstanceTypeArgsAliases]
from typing import Iterable, TypeVar
x = 1
T = TypeVar('T')
It = Iterable
It2 = Iterable[T]

isinstance(x, It[int]) # E: Parameterized generics cannot be used with class or instance checks
isinstance(x, It)
isinstance(x, It2[int]) # E: Parameterized generics cannot be used with class or instance checks
isinstance(x, It2) # E: Parameterized generics cannot be used with class or instance checks

[builtins fixtures/isinstance.pyi]


[case testIssubclassTypeArgs]
from typing import Iterable, TypeVar
x = int
T = TypeVar('T')
issubclass(x, Iterable)
issubclass(x, Iterable[int]) # E: Parameterized generics cannot be used with class or instance checks
issubclass(x, Iterable[T]) # E: Parameterized generics cannot be used with class or instance checks
issubclass(x, (int, Iterable[int])) # E: Parameterized generics cannot be used with class or instance checks

[builtins fixtures/isinstance.pyi]

[case testIsinstanceAndNarrowTypeVariable]
from typing import TypeVar

Expand Down
1 change: 1 addition & 0 deletions test-data/unit/fixtures/isinstance.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class tuple(Generic[T]): pass
class function: pass

def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass
def issubclass(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass

class int:
def __add__(self, other: 'int') -> 'int': pass
Expand Down