Skip to content

Commit

Permalink
address isinstance false negative when provided too many arguments
Browse files Browse the repository at this point in the history
- add too-many-function-args call
- include handling for too-few-function-args
  • Loading branch information
rogersheu committed Aug 13, 2024
1 parent 18f03b7 commit 9682d47
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 7 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9847.false_negative
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a false negative when `isinstance` has too many arguments.
Change now provides a `too-many-arguments` output with behavior similar to other `too-many-arguments` calls.

Closes #9847
28 changes: 24 additions & 4 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def _missing_member_hint(
"Used when a function call passes too few arguments.",
),
"E1121": (
"Too many positional arguments for %s call",
"Too many positional arguments (%s/%s)",
"too-many-function-args",
"Used when a function call passes too many positional arguments.",
),
Expand Down Expand Up @@ -377,6 +377,11 @@ def _missing_member_hint(
"Used when a slice step is 0 and the object doesn't implement "
"a custom __getitem__ method.",
),
"E1145": (
"Too few positional arguments (%s/%s)",
"too-few-function-args",
"Used when a function or method has fewer arguments than expected.",
),
"W1113": (
"Keyword argument before variable positional arguments list "
"in the definition of %s function",
Expand Down Expand Up @@ -1420,8 +1425,21 @@ def _check_argument_order(
self.add_message("arguments-out-of-order", node=node, args=())

def _check_isinstance_args(self, node: nodes.Call) -> None:
if len(node.args) != 2:
# isinstance called with wrong number of args
if len(node.args) > 2:
# for when isinstance called with too many args
self.add_message(
"too-many-function-args",
node=node,
args=(len(node.args), 2),
confidence=HIGH,
)
elif len(node.args) < 2:
self.add_message(
"too-few-function-args",
node=node,
args=(len(node.args), 2),
confidence=HIGH,
)
return

second_arg = node.args[1]
Expand Down Expand Up @@ -1554,7 +1572,9 @@ def visit_call(self, node: nodes.Call) -> None:
elif not overload_function:
# Too many positional arguments.
self.add_message(
"too-many-function-args", node=node, args=(callable_name,)
"too-many-function-args",
node=node,
args=(len(parameters), num_positional_args),
)
break

Expand Down
3 changes: 3 additions & 0 deletions tests/functional/t/too/too_few_function_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pylint: disable=missing-docstring

isinstance(1) # [too-few-function-args]
1 change: 1 addition & 0 deletions tests/functional/t/too/too_few_function_args.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
too-few-function-args:3:0:3:13::Too few positional arguments (1/2):HIGH
6 changes: 3 additions & 3 deletions tests/functional/t/too/too_many_arguments.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
too-many-arguments:4:0:4:19:stupid_function:Too many arguments (9/5):UNDEFINED
too-many-positional-arguments:4:0:4:19:stupid_function:Too many positional arguments (9/5):HIGH
too-many-arguments:37:0:37:9:name1:Too many arguments (6/5):UNDEFINED
too-many-arguments:4:0:4:19:stupid_function:Too many arguments (9/5):UNDEFINED
too-many-positional-arguments:4:0:4:19:stupid_function:Too many positional arguments (9/5):HIGH
too-many-arguments:37:0:37:9:name1:Too many arguments (6/5):UNDEFINED
5 changes: 5 additions & 0 deletions tests/functional/t/too/too_many_function_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ def main(param):
if param == 0:
tmp = add
return tmp(1, 1.01)


# Negative case, see `_check_isinstance_args` in `./pylint/checkers/typecheck.py`
isinstance(1, int, int) # [too-many-function-args]
isinstance(1, 1, int) # [too-many-function-args, isinstance-second-argument-not-valid-type]
3 changes: 3 additions & 0 deletions tests/functional/t/too/too_many_function_args.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
too-many-function-args:23:0:23:23::Too many positional arguments (3/2):HIGH
isinstance-second-argument-not-valid-type:24:0:24:21::Second argument of isinstance is not a type:INFERENCE
too-many-function-args:24:0:24:21::Too many positional arguments (3/2):HIGH

0 comments on commit 9682d47

Please sign in to comment.