add hacking for assertIsNotNone

add hacking assertIsNotNone(A),errors = [(1, 0, "M319")]

Change-Id: I08beb689a16f212de4413bc1b888f6dbe4c183c3
changes/37/344037/4
huang.huayong 6 years ago
parent 7a66c1cf56
commit 514dc58906
  1. 2
      HACKING.rst
  2. 18
      magnum/hacking/checks.py
  3. 13
      magnum/tests/unit/test_hacking.py

@ -16,6 +16,8 @@ Magnum Specific Commandments
assertIsInstance(A, B).
- [M318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert
like assertIsNone(A)
- [M319] Change assertNotEqual(A, None) or assertNotEqual(None, A)
by optimal assert like assertIsNotNone(A)
- [M322] Method's default argument shouldn't be mutable.
- [M323] Change assertEqual(True, A) or assertEqual(False, A) by optimal assert
like assertTrue(A) or assertFalse(A)

@ -39,6 +39,10 @@ assert_equal_end_with_none_re = re.compile(
r"(.)*assertEqual\((\w|\.|\'|\"|\[|\])+, None\)")
assert_equal_start_with_none_re = re.compile(
r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)")
assert_not_equal_end_with_none_re = re.compile(
r"(.)*assertNotEqual\((\w|\.|\'|\"|\[|\])+, None\)")
assert_not_equal_start_with_none_re = re.compile(
r"(.)*assertNotEqual\(None, (\w|\.|\'|\"|\[|\])+\)")
assert_equal_with_true_re = re.compile(
r"assertEqual\(True,")
assert_equal_with_false_re = re.compile(
@ -66,6 +70,19 @@ def assert_equal_none(logical_line):
yield (0, msg)
def assert_not_equal_none(logical_line):
"""Check for assertNotEqual(A, None) or assertNotEqual(None, A) sentences
M319
"""
msg = ("M319: assertNotEqual(A, None) or assertNotEqual(None, A) "
"sentences not allowed")
res = (assert_not_equal_start_with_none_re.match(logical_line) or
assert_not_equal_end_with_none_re.match(logical_line))
if res:
yield (0, msg)
def no_mutable_default_args(logical_line):
msg = "M322: Method's default argument shouldn't be mutable!"
if mutable_default_args.match(logical_line):
@ -147,6 +164,7 @@ def dict_constructor_with_list_copy(logical_line):
def factory(register):
register(no_mutable_default_args)
register(assert_equal_none)
register(assert_not_equal_none)
register(assert_equal_true_or_false)
register(assert_equal_not_none)
register(assert_true_isinstance)

@ -130,6 +130,19 @@ class HackingTestCase(base.TestCase):
code = "self.assertIsNone()"
self._assert_has_no_errors(code, check)
def test_assert_not_equal_none(self):
errors = [(1, 0, "M319")]
check = checks.assert_not_equal_none
code = "self.assertNotEqual(A, None)"
self._assert_has_errors(code, check, errors)
code = "self.assertNotEqual(None, A)"
self._assert_has_errors(code, check, errors)
code = "self.assertIsNotNone()"
self._assert_has_no_errors(code, check)
def test_assert_equal_true_or_false(self):
errors = [(1, 0, "M323")]
check = checks.assert_equal_true_or_false

Loading…
Cancel
Save