Skip to content

Commit

Permalink
Fix pylint-dev#3675: safe_infer now finds ambiguity when number of …
Browse files Browse the repository at this point in the history
…arguments differ
  • Loading branch information
jacobtylerwalls committed Nov 27, 2021
1 parent e83fda3 commit 7a8a071
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Release date: TBA
..
Put bug fixes that should not wait for a new minor version here

* ``safe_infer`` no longer makes an inference given two function
definitions with differing numbers of arguments.

Closes #3675

..
Insert your changelog randomly, it will reduce merge conflicts
(Ie. not necessarily at the end)
Expand Down
13 changes: 13 additions & 0 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,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:
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)

0 comments on commit 7a8a071

Please sign in to comment.