Skip to content

Commit e734e46

Browse files
clavedelunaPierre-Sassoulas
authored andcommitted
Fix deprecated-method false positive with aliases (#7795)
When alias for method is similar to name of deprecated method. Closes #5886 (cherry picked from commit 57f38c3)
1 parent a21559d commit e734e46

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``deprecated-method`` false positive when alias for method is similar to name of deprecated method.
2+
3+
Closes #5886

pylint/checkers/deprecated.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,7 @@ def check_deprecated_method(self, node: nodes.Call, inferred: nodes.NodeNG) -> N
213213
# Not interested in other nodes.
214214
return
215215

216-
if hasattr(inferred.parent, "qname") and inferred.parent.qname():
217-
# Handling the situation when deprecated function is
218-
# alias to existing function.
219-
qnames = {
220-
inferred.qname(),
221-
f"{inferred.parent.qname()}.{func_name}",
222-
func_name,
223-
}
224-
else:
225-
qnames = {inferred.qname(), func_name}
216+
qnames = {inferred.qname(), func_name}
226217
if any(name in self.deprecated_methods() for name in qnames):
227218
self.add_message("deprecated-method", node=node, args=(func_name,))
228219
return

tests/checkers/unittest_deprecated.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,22 @@ def deprecated_method():
108108

109109
def test_deprecated_method_alias(self) -> None:
110110
# Tests detecting deprecated method defined as alias
111-
# to existing method
112111
node = astroid.extract_node(
113112
"""
114113
class Deprecated:
115-
def _deprecated_method(self):
114+
def deprecated_method(self):
116115
pass
117116
118-
deprecated_method = _deprecated_method
117+
new_name = deprecated_method
119118
120119
d = Deprecated()
121-
d.deprecated_method()
120+
d.new_name()
122121
"""
123122
)
124123
with self.assertAddsMessages(
125124
MessageTest(
126125
msg_id="deprecated-method",
127-
args=("deprecated_method",),
126+
args=("new_name",),
128127
node=node,
129128
confidence=UNDEFINED,
130129
line=9,
@@ -135,6 +134,23 @@ def _deprecated_method(self):
135134
):
136135
self.checker.visit_call(node)
137136

137+
def test_not_deprecated(self) -> None:
138+
# Tests detecting method is NOT deprecated when alias name is a deprecated name
139+
node = astroid.extract_node(
140+
"""
141+
class Deprecated:
142+
def not_deprecated(self):
143+
pass
144+
145+
deprecated_method = not_deprecated
146+
147+
d = Deprecated()
148+
d.deprecated_method()
149+
"""
150+
)
151+
with self.assertNoMessages():
152+
self.checker.visit_call(node)
153+
138154
def test_no_message(self) -> None:
139155
# Tests not raising error when no deprecated functions/methods are present.
140156
node = astroid.extract_node(

tests/functional/d/deprecated/deprecated_methods_py38.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import inspect
55
import logging
66
import nntplib
7+
import time
78
import unittest
89
import xml.etree.ElementTree
910

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

5252
d = Deprecated()
5353
d.deprecated_method() # [deprecated-method]
54+
55+
def test(clock = time.time):
56+
"""time.clock is deprecated but time.time via an alias is not!"""
57+
clock()

0 commit comments

Comments
 (0)