summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2021-12-14 08:32:53 -0500
committerGitHub <noreply@github.com>2021-12-14 14:32:53 +0100
commit1f1c7b94d41e93b42ebed5e633b548d8dcbdf387 (patch)
tree3188f4b064eb6aab005a0eece3a773eb62286217
parent01fa4df457309f8a7249d791a84084b4ae3f0fd4 (diff)
downloadpylint-git-1f1c7b94d41e93b42ebed5e633b548d8dcbdf387.tar.gz
Fix #3675: `safe_infer()` finds ambiguity among function definitions when number of arguments differ (#5409)
Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
-rw-r--r--ChangeLog5
-rw-r--r--pylint/checkers/utils.py7
-rw-r--r--tests/functional/c/consider/consider_using_with.py2
-rw-r--r--tests/functional/c/consider/consider_using_with.txt1
-rw-r--r--tests/functional/t/too/too_many_function_args.py19
5 files changed, 32 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 44e8f5459..df21f26c8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -32,6 +32,11 @@ Release date: TBA
* Some files in ``pylint.testutils`` were deprecated. In the future imports should be done from the
``pylint.testutils.functional`` namespace directly.
+* ``safe_infer`` no longer makes an inference when given two function
+ definitions with differing numbers of arguments.
+
+ Closes #3675
+
* Fix ``unnecessary_dict_index_lookup`` false positive when deleting a dictionary's entry.
Closes #4716
diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py
index 5a03ca4f4..d71669729 100644
--- a/pylint/checkers/utils.py
+++ b/pylint/checkers/utils.py
@@ -1260,6 +1260,13 @@ def safe_infer(node: nodes.NodeNG, context=None) -> Optional[nodes.NodeNG]:
inferred_type = _get_python_type_of_node(inferred)
if inferred_type not in inferred_types:
return None # If there is ambiguity on the inferred node.
+ if (
+ isinstance(inferred, nodes.FunctionDef)
+ and inferred.args.args is not None
+ and value.args.args is not None
+ and len(inferred.args.args) != len(value.args.args)
+ ):
+ return None # Different number of arguments indicates ambiguity
except astroid.InferenceError:
return None # There is some kind of ambiguity
except StopIteration:
diff --git a/tests/functional/c/consider/consider_using_with.py b/tests/functional/c/consider/consider_using_with.py
index d05866aaa..3466e15c0 100644
--- a/tests/functional/c/consider/consider_using_with.py
+++ b/tests/functional/c/consider/consider_using_with.py
@@ -21,7 +21,7 @@ def test_urlopen():
def test_temporary_file():
- _ = tempfile.TemporaryFile("r") # [consider-using-with]
+ _ = tempfile.TemporaryFile("r") # ambiguous with NamedTemporaryFile
def test_named_temporary_file():
diff --git a/tests/functional/c/consider/consider_using_with.txt b/tests/functional/c/consider/consider_using_with.txt
index dad0eb45f..973d97403 100644
--- a/tests/functional/c/consider/consider_using_with.txt
+++ b/tests/functional/c/consider/consider_using_with.txt
@@ -1,6 +1,5 @@
consider-using-with:15:9:15:40:test_codecs_open:Consider using 'with' for resource-allocating operations:UNDEFINED
consider-using-with:20:8:20:55:test_urlopen:Consider using 'with' for resource-allocating operations:UNDEFINED
-consider-using-with:24:8:24:35:test_temporary_file:Consider using 'with' for resource-allocating operations:UNDEFINED
consider-using-with:28:8:28:40:test_named_temporary_file:Consider using 'with' for resource-allocating operations:UNDEFINED
consider-using-with:32:8:32:42:test_spooled_temporary_file:Consider using 'with' for resource-allocating operations:UNDEFINED
consider-using-with:36:8:36:37:test_temporary_directory:Consider using 'with' for resource-allocating operations:UNDEFINED
diff --git a/tests/functional/t/too/too_many_function_args.py b/tests/functional/t/too/too_many_function_args.py
new file mode 100644
index 000000000..c5ca4f78e
--- /dev/null
+++ b/tests/functional/t/too/too_many_function_args.py
@@ -0,0 +1,19 @@
+"""https://github.com/PyCQA/pylint/issues/3675"""
+
+
+def noop(x): # pylint: disable=invalid-name
+ """Return value unchanged"""
+ return x
+
+
+def add(x, y): # pylint: disable=invalid-name
+ """Add two values"""
+ return x + y
+
+
+def main(param):
+ """Should not emit too-many-function-args"""
+ tmp = noop # matched first
+ if param == 0:
+ tmp = add
+ return tmp(1, 1.01)