Skip to content

Commit 36d4999

Browse files
authored
Suggest the preferred names for deprecated methods (#6)
This introduces a new error (A503) that suggests the preferred names for deprecated methods, such as assertEqual() instead of assertEquals(). Because our existing suggestions are always use the preferred names, we only emit this new error if we don't have any other suggestions to make.
1 parent 06fb89d commit 36d4999

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changes
22
=======
33

4+
1.2.0 (2019-12-05)
5+
------------------
6+
7+
* Suggest the preferred names for deprecated methods, such as
8+
``assertEqual()`` instead of ``assertEquals()``.
9+
410
1.1.0 (2019-06-26)
511
------------------
612

README.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ the generic methods.
6969
+------------------------------------------+-----------------------------------+------+
7070
| ``assertEqual(a, False)`` | ``assertFalse(a)`` | A502 |
7171
+------------------------------------------+-----------------------------------+------+
72+
| ``assertEquals(a, b)`` | ``assertEqual(a, b)`` | A503 |
73+
+------------------------------------------+-----------------------------------+------+
74+
| ``assertNotEquals(a, b)`` | ``assertNotEqual(a, b)`` | A503 |
75+
+------------------------------------------+-----------------------------------+------+
76+
| ``assertAlmostEquals(a, b, x)`` | ``assertAlmostEqual(a, b, x)`` | A503 |
77+
+------------------------------------------+-----------------------------------+------+
78+
| ``assertNotAlmostEquals(a, b, x)`` | ``assertNotAlmostEqual(a, b, x)`` | A503 |
79+
+------------------------------------------+-----------------------------------+------+
7280

7381
Note that some suggestions are normalized forms of the original, such as when
7482
a double-negative is used (``assertFalse(a != b)`` → ``assertEqual(a, b)``).
@@ -91,7 +99,7 @@ that it has been loaded by inspecting the ``flake8 --version`` string.
9199
.. code-block:: sh
92100
93101
$ flake8 --version
94-
3.7.7 (assertive: 1.1.0, ...) CPython 2.7.16 on Darwin
102+
3.7.9 (assertive: 1.2.0, ...) CPython 2.7.17 on Darwin
95103
96104
97105
Error Codes
@@ -102,6 +110,7 @@ This extension adds three new `error codes`__ (using the ``A50`` prefix):
102110
- ``A500``: prefer *{func}* for '*{op}*' comparisons
103111
- ``A501``: prefer *{func}* for '*{op}*' expressions
104112
- ``A502``: prefer *{func}* instead of comparing to *{obj}*
113+
- ``A503``: use *{func}* instead of the deprecated *{name}*
105114

106115
.. __: http://flake8.pycqa.org/en/latest/user/error-codes.html
107116

flake8_assertive.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import re
2626

2727
__all__ = ['Checker']
28-
__version__ = '1.1.0'
28+
__version__ = '1.2.0'
2929

3030

3131
# Python 3.4 introduced `ast.NameConstant` for `None`, `True`, and `False`.
@@ -49,6 +49,21 @@ def is_assert_method_call(node):
4949
node.func.attr.startswith('assert'))
5050

5151

52+
def wrap_deprecated(func, name):
53+
"""Return a check function for a deprecated assert method call.
54+
55+
If the `assertive-deprecated` option has been enabled and the wrapped
56+
check function doesn't yield any errors of its own, this function will
57+
yield an A503 error that includes the new name of deprecated method.
58+
"""
59+
def wrapper(self, node):
60+
for error in func(self, node):
61+
yield error
62+
else:
63+
yield self.error(node, 'A503', func=name, name=node.func.attr)
64+
return wrapper
65+
66+
5267
class Checker(object):
5368
"""Unittest assert method checker"""
5469

@@ -60,6 +75,7 @@ class Checker(object):
6075
A500 = "prefer {func}() for '{op}' comparisons"
6176
A501 = "prefer {func}() for '{op}' expressions"
6277
A502 = "prefer {func}() instead of comparing to {obj}"
78+
A503 = "use {func}() instead of the deprecated {name}()"
6379

6480
def __init__(self, tree, filename):
6581
self.tree = tree
@@ -140,11 +156,6 @@ def check_assertnotalmostequal(self, node):
140156
'built-in rounding of assertNotAlmostEqual',
141157
op='round')
142158

143-
check_assertequals = check_assertequal
144-
check_assertnotequals = check_assertnotequal
145-
check_assertalmostequals = check_assertalmostequal
146-
check_assertnotalmostequals = check_assertnotalmostequal
147-
148159
def check_asserttrue(self, node):
149160
if (isinstance(node.args[0], ast.Compare) and
150161
len(node.args[0].ops) == 1):
@@ -204,3 +215,19 @@ def check_assertfalse(self, node):
204215
elif is_function_call(node.args[0], 'isinstance'):
205216
yield self.error(
206217
node, 'A501', 'assertNotIsInstance', op='isinstance()')
218+
219+
check_assertequals = wrap_deprecated(
220+
check_assertequal,
221+
'assertEqual')
222+
223+
check_assertnotequals = wrap_deprecated(
224+
check_assertnotequal,
225+
'assertNotEqual')
226+
227+
check_assertalmostequals = wrap_deprecated(
228+
check_assertalmostequal,
229+
'assertAlmostEqual')
230+
231+
check_assertnotalmostequals = wrap_deprecated(
232+
check_assertnotalmostequal,
233+
'assertNotAlmostEqual')

tests/test_checker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,7 @@ def test_pattern(self):
160160
def test_snakecase(self):
161161
Checker.snakecase = True
162162
self.check("self.assert_equal(True, a)", "A502", "assert_true()")
163+
164+
def test_deprecated(self):
165+
self.check("self.assertEquals(True, a)", "A502", "assertTrue()")
166+
self.check("self.assertEquals(a, b)", "A503", "assertEqual()")

0 commit comments

Comments
 (0)