Skip to content

Commit

Permalink
function-redefined` exempts function redefined on a condition.
Browse files Browse the repository at this point in the history
Close #2410
  • Loading branch information
PCManticore committed Feb 28, 2020
1 parent a0fc570 commit eeca794
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Release date: TBA

Close #2874

* ``function-redefined`` exempts function redefined on a condition.

Close #2410

* ``typing.overload`` functions are exempted from docstring checks

Close #3350
Expand Down
24 changes: 23 additions & 1 deletion pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,6 @@ def _check_redefinition(self, redeftype, node):
node,
)
if defined_self is not node and not astroid.are_exclusive(node, defined_self):

# Additional checks for methods which are not considered
# redefined, since they are already part of the base API.
if (
Expand All @@ -854,9 +853,32 @@ def _check_redefinition(self, redeftype, node):
):
return

# Skip typing.overload() functions.
if utils.is_overload_stub(node):
return

# Exempt functions redefined on a condition.
if isinstance(node.parent, astroid.If):
# Exempt "if not <func>" cases
if (
isinstance(node.parent.test, astroid.UnaryOp)
and node.parent.test.op == "not"
and isinstance(node.parent.test.operand, astroid.Name)
and node.parent.test.operand.name == node.name
):
return

# Exempt "if <func> is not None" cases
if (
isinstance(node.parent.test, astroid.Compare)
and isinstance(node.parent.test.left, astroid.Name)
and node.parent.test.left.name == node.name
and node.parent.test.ops[0][0] == "is"
and isinstance(node.parent.test.ops[0][1], astroid.Const)
and node.parent.test.ops[0][1].value is None
):
return

# Check if we have forward references for this node.
try:
redefinition_index = redefinitions.index(node)
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/f/function_redefined.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,14 @@ def __module__(self):
@property
def __doc__(self):
return "Docstring"


# Do not emit the error for conditional definitions
def func(callback1=None, callback2=None):
if not callback1:
def callback1():
return 42
if callback2 is None:
def callback2():
return 24
return callback1(), callback2()

0 comments on commit eeca794

Please sign in to comment.