From e387452419e09711ae7efe5b379168acbb928b42 Mon Sep 17 00:00:00 2001 From: "Jay S. Bryant" Date: Fri, 10 Jan 2020 10:31:25 -0600 Subject: [PATCH] Remove hacking check N325 This hacking check was added back when the str() function couldn't handle unicode characters. With Python3 this is no longer an issue. As a result we can now remove this check. Change-Id: I926732062dbbd1242cd382e2593e07b4caf4ffec --- HACKING.rst | 2 - cinder/tests/hacking/checks.py | 48 ----------------------- cinder/tests/unit/test_hacking.py | 65 ------------------------------- 3 files changed, 115 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index f15b8f29069..5ce778aa832 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -10,8 +10,6 @@ Cinder Specific Commandments - [N314] Check for vi editor configuration in source files. - [N322] Ensure default arguments are not mutable. - [N323] Add check for explicit import of _() to ensure proper translation. -- [N325] str() and unicode() cannot be used on an exception. Remove or use - six.text_type(). - [N336] Must use a dict comprehension instead of a dict constructor with a sequence of key-value pairs. - [C301] timeutils.utcnow() from oslo_utils should be used instead of diff --git a/cinder/tests/hacking/checks.py b/cinder/tests/hacking/checks.py index 5caa6574a56..d9d0ddb5ec9 100644 --- a/cinder/tests/hacking/checks.py +++ b/cinder/tests/hacking/checks.py @@ -167,53 +167,6 @@ def check_explicit_underscore_import(logical_line, filename): yield(0, "N323: 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. - """ - - CHECK_DESC = ('N325 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 CheckLoggingFormatArgs(BaseASTChecker): """Check for improper use of logging format arguments. @@ -461,7 +414,6 @@ def factory(register): register(no_translate_logs) register(no_mutable_default_args) register(check_explicit_underscore_import) - register(CheckForStrUnicodeExc) register(CheckLoggingFormatArgs) register(CheckOptRegistrationArgs) register(check_datetime_now) diff --git a/cinder/tests/unit/test_hacking.py b/cinder/tests/unit/test_hacking.py index 9786461e7d5..cb70e64178b 100644 --- a/cinder/tests/unit/test_hacking.py +++ b/cinder/tests/unit/test_hacking.py @@ -219,71 +219,6 @@ class HackingTestCase(test.TestCase): expected_errors=[(2, 19, 'C311'), (3, 18, 'C311')]) - def test_str_unicode_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, 'N325')] - self._assert_has_errors(code, checker, expected_errors=errors) - - 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) - - code = """ - def f(a, b): - try: - p = str(a) + str(b) - except ValueError as e: - p = unicode(e) - return p - """ - errors = [(5, 20, 'N325')] - self._assert_has_errors(code, checker, expected_errors=errors) - - 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, 'N325'), (8, 29, 'N325')] - self._assert_has_errors(code, checker, expected_errors=errors) - - 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, 'N325'), (8, 33, 'N325'), (9, 16, 'N325')] - self._assert_has_errors(code, checker, expected_errors=errors) - def test_check_no_log_audit(self): self.assertEqual(1, len(list(checks.check_no_log_audit( "LOG.audit('My test audit log')"))))