Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3675: safe_infer() finds ambiguity among function definitions when number of arguments differ #5409

Merged
merged 4 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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
Expand Down
7 changes: 7 additions & 0 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/c/consider/consider_using_with.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
1 change: 0 additions & 1 deletion tests/functional/c/consider/consider_using_with.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/t/too/too_many_function_args.py
Original file line number Diff line number Diff line change
@@ -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)