Skip to content

Commit

Permalink
Fix deprecated-method false positive with aliases (#7795)
Browse files Browse the repository at this point in the history
When alias for method is similar to name of deprecated method.

Closes #5886
  • Loading branch information
clavedeluna committed Nov 19, 2022
1 parent 7fd56d9 commit 57f38c3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/5886.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix ``deprecated-method`` false positive when alias for method is similar to name of deprecated method.

Closes #5886
11 changes: 1 addition & 10 deletions pylint/checkers/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,7 @@ def check_deprecated_method(self, node: nodes.Call, inferred: nodes.NodeNG) -> N
# Not interested in other nodes.
return

if hasattr(inferred.parent, "qname") and inferred.parent.qname():
# Handling the situation when deprecated function is
# alias to existing function.
qnames = {
inferred.qname(),
f"{inferred.parent.qname()}.{func_name}",
func_name,
}
else:
qnames = {inferred.qname(), func_name}
qnames = {inferred.qname(), func_name}
if any(name in self.deprecated_methods() for name in qnames):
self.add_message("deprecated-method", node=node, args=(func_name,))
return
Expand Down
26 changes: 21 additions & 5 deletions tests/checkers/unittest_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,22 @@ def deprecated_method():

def test_deprecated_method_alias(self) -> None:
# Tests detecting deprecated method defined as alias
# to existing method
node = astroid.extract_node(
"""
class Deprecated:
def _deprecated_method(self):
def deprecated_method(self):
pass
deprecated_method = _deprecated_method
new_name = deprecated_method
d = Deprecated()
d.deprecated_method()
d.new_name()
"""
)
with self.assertAddsMessages(
MessageTest(
msg_id="deprecated-method",
args=("deprecated_method",),
args=("new_name",),
node=node,
confidence=UNDEFINED,
line=9,
Expand All @@ -135,6 +134,23 @@ def _deprecated_method(self):
):
self.checker.visit_call(node)

def test_not_deprecated(self) -> None:
# Tests detecting method is NOT deprecated when alias name is a deprecated name
node = astroid.extract_node(
"""
class Deprecated:
def not_deprecated(self):
pass
deprecated_method = not_deprecated
d = Deprecated()
d.deprecated_method()
"""
)
with self.assertNoMessages():
self.checker.visit_call(node)

def test_no_message(self) -> None:
# Tests not raising error when no deprecated functions/methods are present.
node = astroid.extract_node(
Expand Down
6 changes: 5 additions & 1 deletion tests/functional/d/deprecated/deprecated_methods_py38.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import inspect
import logging
import nntplib
import time
import unittest
import xml.etree.ElementTree


class MyTest(unittest.TestCase):
def test(self):
self.assert_(True) # [deprecated-method]
Expand Down Expand Up @@ -51,3 +51,7 @@ class Deprecated: # pylint: disable=too-few-public-methods

d = Deprecated()
d.deprecated_method() # [deprecated-method]

def test(clock = time.time):
"""time.clock is deprecated but time.time via an alias is not!"""
clock()

0 comments on commit 57f38c3

Please sign in to comment.