Skip to content

Don't crash if isinstance() called with too few arguments #3652

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 1 commit into from
Jul 4, 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
3 changes: 2 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type:
typeddict_type = e.callee.node.typeddict_type.copy_modified(
fallback=Instance(e.callee.node, []))
return self.check_typeddict_call(typeddict_type, e.arg_kinds, e.arg_names, e.args, e)
if isinstance(e.callee, NameExpr) and e.callee.name in ('isinstance', 'issubclass'):
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]):
if isinstance(typ, NameExpr):
try:
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1757,3 +1757,24 @@ if isinstance(x, (set, (list, T))):
reveal_type(x) # E: Revealed type is 'Union[builtins.set[Any], builtins.list[Any], builtins.int, builtins.str]'

[builtins fixtures/isinstancelist.pyi]

[case testIsInstanceTooFewArgs]
isinstance() # E: Too few arguments for "isinstance"
x: object
if isinstance(): # E: Too few arguments for "isinstance"
x = 1
reveal_type(x) # E: Revealed type is 'builtins.int'
if isinstance(x): # E: Too few arguments for "isinstance"
x = 1
reveal_type(x) # E: Revealed type is 'builtins.int'
[builtins fixtures/isinstancelist.pyi]

[case testIsInstanceTooManyArgs]
isinstance(1, 1, 1) # E: Too many arguments for "isinstance" \
# E: Argument 2 to "isinstance" has incompatible type "int"; expected "Union[type, tuple]"
x: object
if isinstance(x, str, 1): # E: Too many arguments for "isinstance"
reveal_type(x) # E: Revealed type is 'builtins.object'
x = 1
reveal_type(x) # E: Revealed type is 'builtins.int'
[builtins fixtures/isinstancelist.pyi]