disable M325

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

Change-Id: Id1647b55af476ab4559759a27498ede353dd10ba
This commit is contained in:
haixin 2020-10-09 11:39:31 +08:00
parent 52c926e32c
commit cc33a4b23e
4 changed files with 0 additions and 124 deletions

View File

@ -12,7 +12,6 @@ Manila Specific Commandments
- [M310] Check for improper use of logging format arguments. - [M310] Check for improper use of logging format arguments.
- [M313] Use assertTrue(...) rather than assertEqual(True, ...). - [M313] Use assertTrue(...) rather than assertEqual(True, ...).
- [M323] Ensure that the _() function is explicitly imported to ensure proper translations. - [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 - [M326] Translated messages cannot be concatenated. String should be
included in translated message. included in translated message.
- [M333] ``oslo_`` should be used instead of ``oslo.`` - [M333] ``oslo_`` should be used instead of ``oslo.``

View File

@ -197,55 +197,6 @@ def check_explicit_underscore_import(logical_line, filename):
yield(0, "M323: Found use of _() without explicit import of _ !") 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): class CheckForTransAdd(BaseASTChecker):
"""Checks for the use of concatenation on a translated string. """Checks for the use of concatenation on a translated string.

View File

@ -158,79 +158,6 @@ class HackingTestCase(test.TestCase):
self._assert_has_errors(code.format(log_method), checker, self._assert_has_errors(code.format(log_method), checker,
expected_errors=[(4, mock.ANY, 'M310')]) 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): def test_trans_add(self):
checker = checks.CheckForTransAdd checker = checks.CheckForTransAdd

View File

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