Skip to content

Commit 4bffdbd

Browse files
committed
address isinstance false negative when provided too many arguments
1 parent 18f03b7 commit 4bffdbd

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a false negative when `isinstance` has too many arguments.
2+
Change now provides a `too-many-arguments` output with behavior similar to other `too-many-arguments` calls.
3+
4+
Closes #9847

pylint/checkers/typecheck.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,8 +1420,16 @@ def _check_argument_order(
14201420
self.add_message("arguments-out-of-order", node=node, args=())
14211421

14221422
def _check_isinstance_args(self, node: nodes.Call) -> None:
1423-
if len(node.args) != 2:
1424-
# isinstance called with wrong number of args
1423+
if len(node.args) > 2:
1424+
# for when isinstance called with too many args
1425+
self.add_message(
1426+
"too-many-arguments",
1427+
node=node,
1428+
args=(len(node.args), 2),
1429+
confidence=HIGH,
1430+
)
1431+
elif len(node.args) < 2:
1432+
# not currently handling the too few args case
14251433
return
14261434

14271435
second_arg = node.args[1]

tests/functional/t/too/too_many_arguments.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ def func_call():
3636
# +1: [too-many-arguments]
3737
def name1(param1, param2, param3, /, param4, param5, *args, param6="apple", **kwargs):
3838
return param1, param2, param3, param4, param5, param6, args, kwargs
39+
40+
41+
# Negative case, see `_check_isinstance_args` in `./pylint/checkers/typecheck.py`
42+
isinstance(1, int, int) # [too-many-arguments]
43+
isinstance(1, 1, int) # [too-many-arguments, isinstance-second-argument-not-valid-type]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
too-many-arguments:4:0:4:19:stupid_function:Too many arguments (9/5):UNDEFINED
22
too-many-positional-arguments:4:0:4:19:stupid_function:Too many positional arguments (9/5):HIGH
33
too-many-arguments:37:0:37:9:name1:Too many arguments (6/5):UNDEFINED
4+
too-many-arguments:42:0:42:23::Too many arguments (3/2):HIGH
5+
isinstance-second-argument-not-valid-type:43:0:43:21::Second argument of isinstance is not a type:INFERENCE
6+
too-many-arguments:43:0:43:21::Too many arguments (3/2):HIGH

0 commit comments

Comments
 (0)