Skip to content

Commit 1f1c7b9

Browse files
Fix #3675: safe_infer() finds ambiguity among function definitions when number of arguments differ (#5409)
Co-authored-by: Daniël van Noord <[email protected]>
1 parent 01fa4df commit 1f1c7b9

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ Release date: TBA
3232
* Some files in ``pylint.testutils`` were deprecated. In the future imports should be done from the
3333
``pylint.testutils.functional`` namespace directly.
3434

35+
* ``safe_infer`` no longer makes an inference when given two function
36+
definitions with differing numbers of arguments.
37+
38+
Closes #3675
39+
3540
* Fix ``unnecessary_dict_index_lookup`` false positive when deleting a dictionary's entry.
3641

3742
Closes #4716

pylint/checkers/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,13 @@ def safe_infer(node: nodes.NodeNG, context=None) -> Optional[nodes.NodeNG]:
12601260
inferred_type = _get_python_type_of_node(inferred)
12611261
if inferred_type not in inferred_types:
12621262
return None # If there is ambiguity on the inferred node.
1263+
if (
1264+
isinstance(inferred, nodes.FunctionDef)
1265+
and inferred.args.args is not None
1266+
and value.args.args is not None
1267+
and len(inferred.args.args) != len(value.args.args)
1268+
):
1269+
return None # Different number of arguments indicates ambiguity
12631270
except astroid.InferenceError:
12641271
return None # There is some kind of ambiguity
12651272
except StopIteration:

tests/functional/c/consider/consider_using_with.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_urlopen():
2121

2222

2323
def test_temporary_file():
24-
_ = tempfile.TemporaryFile("r") # [consider-using-with]
24+
_ = tempfile.TemporaryFile("r") # ambiguous with NamedTemporaryFile
2525

2626

2727
def test_named_temporary_file():

tests/functional/c/consider/consider_using_with.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
consider-using-with:15:9:15:40:test_codecs_open:Consider using 'with' for resource-allocating operations:UNDEFINED
22
consider-using-with:20:8:20:55:test_urlopen:Consider using 'with' for resource-allocating operations:UNDEFINED
3-
consider-using-with:24:8:24:35:test_temporary_file:Consider using 'with' for resource-allocating operations:UNDEFINED
43
consider-using-with:28:8:28:40:test_named_temporary_file:Consider using 'with' for resource-allocating operations:UNDEFINED
54
consider-using-with:32:8:32:42:test_spooled_temporary_file:Consider using 'with' for resource-allocating operations:UNDEFINED
65
consider-using-with:36:8:36:37:test_temporary_directory:Consider using 'with' for resource-allocating operations:UNDEFINED
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""https://github.com/PyCQA/pylint/issues/3675"""
2+
3+
4+
def noop(x): # pylint: disable=invalid-name
5+
"""Return value unchanged"""
6+
return x
7+
8+
9+
def add(x, y): # pylint: disable=invalid-name
10+
"""Add two values"""
11+
return x + y
12+
13+
14+
def main(param):
15+
"""Should not emit too-many-function-args"""
16+
tmp = noop # matched first
17+
if param == 0:
18+
tmp = add
19+
return tmp(1, 1.01)

0 commit comments

Comments
 (0)