Browse Source

disable M325

Since we don't support python2.x anymore, we don't
need to use six.text_type.

Change-Id: Id1647b55af476ab4559759a27498ede353dd10ba
changes/93/756993/4
haixin 12 months ago
parent
commit
cc33a4b23e
  1. 1
      HACKING.rst
  2. 49
      manila/tests/hacking/checks.py
  3. 73
      manila/tests/test_hacking.py
  4. 1
      tox.ini

1
HACKING.rst

@ -12,7 +12,6 @@ Manila Specific Commandments
- [M310] Check for improper use of logging format arguments.
- [M313] Use assertTrue(...) rather than assertEqual(True, ...).
- [M323] Ensure that the _() function is explicitly imported to ensure proper translations.
- [M325] str() and unicode() cannot be used on an exception. Remove or use six.text_type().
- [M326] Translated messages cannot be concatenated. String should be
included in translated message.
- [M333] ``oslo_`` should be used instead of ``oslo.``

49
manila/tests/hacking/checks.py

@ -197,55 +197,6 @@ def check_explicit_underscore_import(logical_line, filename):
yield(0, "M323: Found use of _() without explicit import of _ !")
class CheckForStrUnicodeExc(BaseASTChecker):
"""Checks for the use of str() or unicode() on an exception.
This currently only handles the case where str() or unicode()
is used in the scope of an exception handler. If the exception
is passed into a function, returned from an assertRaises, or
used on an exception created in the same scope, this does not
catch it.
"""
name = "check_for_str_unicode_exc"
version = "1.0"
CHECK_DESC = ('M325 str() and unicode() cannot be used on an '
'exception. Remove or use six.text_type()')
def __init__(self, tree, filename):
super(CheckForStrUnicodeExc, self).__init__(tree, filename)
self.name = []
self.already_checked = []
# Python 2
def visit_TryExcept(self, node):
for handler in node.handlers:
if handler.name:
self.name.append(handler.name.id)
super(CheckForStrUnicodeExc, self).generic_visit(node)
self.name = self.name[:-1]
else:
super(CheckForStrUnicodeExc, self).generic_visit(node)
# Python 3
def visit_ExceptHandler(self, node):
if node.name:
self.name.append(node.name)
super(CheckForStrUnicodeExc, self).generic_visit(node)
self.name = self.name[:-1]
else:
super(CheckForStrUnicodeExc, self).generic_visit(node)
def visit_Call(self, node):
if self._check_call_names(node, ['str', 'unicode']):
if node not in self.already_checked:
self.already_checked.append(node)
if isinstance(node.args[0], ast.Name):
if node.args[0].id in self.name:
self.add_error(node.args[0])
super(CheckForStrUnicodeExc, self).generic_visit(node)
class CheckForTransAdd(BaseASTChecker):
"""Checks for the use of concatenation on a translated string.

73
manila/tests/test_hacking.py

@ -158,79 +158,6 @@ class HackingTestCase(test.TestCase):
self._assert_has_errors(code.format(log_method), checker,
expected_errors=[(4, mock.ANY, 'M310')])
def test_str_on_exception(self):
checker = checks.CheckForStrUnicodeExc
code = """
def f(a, b):
try:
p = str(a) + str(b)
except ValueError as e:
p = str(e)
return p
"""
errors = [(5, 16, 'M325')]
self._assert_has_errors(code, checker, expected_errors=errors)
def test_no_str_unicode_on_exception(self):
checker = checks.CheckForStrUnicodeExc
code = """
def f(a, b):
try:
p = unicode(a) + str(b)
except ValueError as e:
p = e
return p
"""
self._assert_has_no_errors(code, checker)
def test_unicode_on_exception(self):
checker = checks.CheckForStrUnicodeExc
code = """
def f(a, b):
try:
p = str(a) + str(b)
except ValueError as e:
p = unicode(e)
return p
"""
errors = [(5, 20, 'M325')]
self._assert_has_errors(code, checker, expected_errors=errors)
def test_str_on_multiple_exceptions(self):
checker = checks.CheckForStrUnicodeExc
code = """
def f(a, b):
try:
p = str(a) + str(b)
except ValueError as e:
try:
p = unicode(a) + unicode(b)
except ValueError as ve:
p = str(e) + str(ve)
p = e
return p
"""
errors = [(8, 20, 'M325'), (8, 29, 'M325')]
self._assert_has_errors(code, checker, expected_errors=errors)
def test_str_unicode_on_multiple_exceptions(self):
checker = checks.CheckForStrUnicodeExc
code = """
def f(a, b):
try:
p = str(a) + str(b)
except ValueError as e:
try:
p = unicode(a) + unicode(b)
except ValueError as ve:
p = str(e) + unicode(ve)
p = str(e)
return p
"""
errors = [(8, 20, 'M325'), (8, 33, 'M325'), (9, 16, 'M325')]
self._assert_has_errors(code, checker, expected_errors=errors)
def test_trans_add(self):
checker = checks.CheckForTransAdd

1
tox.ini

@ -151,7 +151,6 @@ extension =
M310 = checks:CheckLoggingFormatArgs
M313 = checks:validate_assertTrue
M323 = checks:check_explicit_underscore_import
M325 = checks:CheckForStrUnicodeExc
M326 = checks:CheckForTransAdd
M333 = checks:check_oslo_namespace_imports
M336 = checks:dict_constructor_with_list_copy

Loading…
Cancel
Save