Skip to content

Commit 88a0194

Browse files
authored
Don't crash if isinstance() called with too few arguments (#3652)
Fix #3650.
1 parent b3b9790 commit 88a0194

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mypy/checkexpr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type:
192192
typeddict_type = e.callee.node.typeddict_type.copy_modified(
193193
fallback=Instance(e.callee.node, []))
194194
return self.check_typeddict_call(typeddict_type, e.arg_kinds, e.arg_names, e.args, e)
195-
if isinstance(e.callee, NameExpr) and e.callee.name in ('isinstance', 'issubclass'):
195+
if (isinstance(e.callee, NameExpr) and e.callee.name in ('isinstance', 'issubclass')
196+
and len(e.args) == 2):
196197
for typ in mypy.checker.flatten(e.args[1]):
197198
if isinstance(typ, NameExpr):
198199
try:

test-data/unit/check-isinstance.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,3 +1757,24 @@ if isinstance(x, (set, (list, T))):
17571757
reveal_type(x) # E: Revealed type is 'Union[builtins.set[Any], builtins.list[Any], builtins.int, builtins.str]'
17581758

17591759
[builtins fixtures/isinstancelist.pyi]
1760+
1761+
[case testIsInstanceTooFewArgs]
1762+
isinstance() # E: Too few arguments for "isinstance"
1763+
x: object
1764+
if isinstance(): # E: Too few arguments for "isinstance"
1765+
x = 1
1766+
reveal_type(x) # E: Revealed type is 'builtins.int'
1767+
if isinstance(x): # E: Too few arguments for "isinstance"
1768+
x = 1
1769+
reveal_type(x) # E: Revealed type is 'builtins.int'
1770+
[builtins fixtures/isinstancelist.pyi]
1771+
1772+
[case testIsInstanceTooManyArgs]
1773+
isinstance(1, 1, 1) # E: Too many arguments for "isinstance" \
1774+
# E: Argument 2 to "isinstance" has incompatible type "int"; expected "Union[type, tuple]"
1775+
x: object
1776+
if isinstance(x, str, 1): # E: Too many arguments for "isinstance"
1777+
reveal_type(x) # E: Revealed type is 'builtins.object'
1778+
x = 1
1779+
reveal_type(x) # E: Revealed type is 'builtins.int'
1780+
[builtins fixtures/isinstancelist.pyi]

0 commit comments

Comments
 (0)