Use assertIsInstance instead of assertTrue(isinstance(a, b))

Developer should use assertIsInstance to replace the
assertTrue(isinstance(a, b)).

Closes-Bug: #1510384
Change-Id: I70f68a5810a6797c7b7e112b46ef2463907bba3e
changes/01/239601/8
wangqun 7 years ago
parent 082c8ed0b8
commit 87d6056846
  1. 2
      HACKING.rst
  2. 13
      magnum/hacking/checks.py
  3. 2
      magnum/tests/unit/conductor/test_monitors.py
  4. 8
      magnum/tests/unit/test_hacking.py

@ -16,3 +16,5 @@ Magnum Specific Commandments
like assertTrue(A) or assertFalse(A)
- [M302] Change assertEqual(A is not None) by optimal assert like
assertIsNotNone(A).
- [M316] Change assertTrue(isinstance(A, B)) by optimal assert like
assertIsInstance(A, B).

@ -43,6 +43,9 @@ assert_equal_with_false_re = re.compile(
r"assertEqual\(False,")
asse_equal_with_is_not_none_re = re.compile(
r"assertEqual\(.*?\s+is+\s+not+\s+None\)$")
assert_true_isinstance_re = re.compile(
r"(.)*assertTrue\(isinstance\((\w|\.|\'|\"|\[|\])+, "
"(\w|\.|\'|\"|\[|\])+\)\)")
def check_policy_enforce_decorator(logical_line,
@ -94,9 +97,19 @@ def assert_equal_not_none(logical_line):
yield (0, msg)
def assert_true_isinstance(logical_line):
"""Check for assertTrue(isinstance(a, b)) sentences
M316
"""
if assert_true_isinstance_re.match(logical_line):
yield (0, "M316: assertTrue(isinstance(a, b)) sentences not allowed")
def factory(register):
register(check_policy_enforce_decorator)
register(no_mutable_default_args)
register(assert_equal_none)
register(assert_equal_true_or_false)
register(assert_equal_not_none)
register(assert_true_isinstance)

@ -53,7 +53,7 @@ class MonitorsTestCase(base.TestCase):
baymodel.coe = 'swarm'
mock_baymodel_get_by_uuid.return_value = baymodel
monitor = monitors.create_monitor(self.context, self.bay)
self.assertTrue(isinstance(monitor, monitors.SwarmMonitor))
self.assertIsInstance(monitor, monitors.SwarmMonitor)
@mock.patch('magnum.objects.BayModel.get_by_uuid')
def test_create_monitor_unsupported_coe(self, mock_baymodel_get_by_uuid):

@ -127,3 +127,11 @@ class HackingTestCase(base.TestCase):
self.assertEqual(0, len(list(checks.assert_equal_not_none(
"self.assertIsNone()"))))
def test_assert_true_isinstance(self):
self.assertEqual(1, len(list(checks.assert_true_isinstance(
"self.assertTrue(isinstance(e, "
"exception.BuildAbortException))"))))
self.assertEqual(0, len(list(checks.assert_true_isinstance(
"self.assertTrue()"))))

Loading…
Cancel
Save