From dc3e515d9f3905901b31cccb1a462dbe35d5b824 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 27 Nov 2021 09:51:19 -0500 Subject: [PATCH] Fix #3675: `safe_infer` now finds ambiguity when number of arguments differ --- ChangeLog | 5 +++++ pylint/checkers/utils.py | 13 +++++++++++++ .../t/too/too_many_function_args.py | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 tests/functional/t/too/too_many_function_args.py diff --git a/ChangeLog b/ChangeLog index f77b7e8706..b3dab8b8b9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 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 5a03ca4f42..5fa3089b88 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -1260,6 +1260,19 @@ 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, astroid.FunctionDef): + # Special case due to unexpected inference ambiguity + if ( + inferred.name == "TemporaryFile" + and value.name == "NamedTemporaryFile" + ): + continue + if ( + 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/t/too/too_many_function_args.py b/tests/functional/t/too/too_many_function_args.py new file mode 100644 index 0000000000..c5ca4f78eb --- /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)