Skip to content

Commit f5f7c25

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Remove hacking rules for python 2/3 compatibility"
2 parents 63a03d8 + 9dca0d1 commit f5f7c25

File tree

4 files changed

+0
-187
lines changed

4 files changed

+0
-187
lines changed

HACKING.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ Nova Specific Commandments
3131
- [N322] Method's default argument shouldn't be mutable
3232
- [N323] Ensure that the _() function is explicitly imported to ensure proper translations.
3333
- [N324] Ensure that jsonutils.%(fun)s must be used instead of json.%(fun)s
34-
- [N325] str() and unicode() cannot be used on an exception. Remove use or use six.text_type()
3534
- [N326] Translated messages cannot be concatenated. String should be included in translated message.
36-
- [N327] Do not use xrange(). xrange() is not compatible with Python 3. Use range() or six.moves.range() instead.
3735
- [N332] Check that the api_version decorator is the first decorator on a method
3836
- [N334] Change assertTrue/False(A in/not in B, message) to the more specific
3937
assertIn/NotIn(A, B, message)
@@ -48,9 +46,6 @@ Nova Specific Commandments
4846
- [N341] contextlib.nested is deprecated
4947
- [N342] Config options should be in the central location ``nova/conf/``
5048
- [N343] Check for common double word typos
51-
- [N344] Python 3: do not use dict.iteritems.
52-
- [N345] Python 3: do not use dict.iterkeys.
53-
- [N346] Python 3: do not use dict.itervalues.
5449
- [N348] Deprecated library function os.popen()
5550
- [N349] Check for closures in tests which are not used
5651
- [N350] Policy registration should be in the central location ``nova/policies/``

nova/hacking/checks.py

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import re
3333

3434
from hacking import core
35-
import six
3635

3736

3837
UNDERSCORE_IMPORT_FILES = []
@@ -292,13 +291,6 @@ def assert_equal_type(logical_line):
292291
yield (0, "N317: assertEqual(type(A), B) sentences not allowed")
293292

294293

295-
@core.flake8ext
296-
def check_python3_xrange(logical_line):
297-
if re.search(r"\bxrange\s*\(", logical_line):
298-
yield (0, "N327: Do not use xrange(). 'xrange()' is not compatible "
299-
"with Python 3. Use range() or six.moves.range() instead.")
300-
301-
302294
@core.flake8ext
303295
def no_translate_logs(logical_line, filename):
304296
"""Check for 'LOG.foo(_('
@@ -400,58 +392,6 @@ def check_api_version_decorator(logical_line, previous_logical, blank_before,
400392
yield (0, msg)
401393

402394

403-
class CheckForStrUnicodeExc(BaseASTChecker):
404-
"""Checks for the use of str() or unicode() on an exception.
405-
406-
This currently only handles the case where str() or unicode()
407-
is used in the scope of an exception handler. If the exception
408-
is passed into a function, returned from an assertRaises, or
409-
used on an exception created in the same scope, this does not
410-
catch it.
411-
"""
412-
413-
name = 'check_for_string_unicode_exc'
414-
version = '1.0'
415-
416-
CHECK_DESC = ('N325 str() and unicode() cannot be used on an '
417-
'exception. Remove or use six.text_type()')
418-
419-
def __init__(self, tree, filename):
420-
super(CheckForStrUnicodeExc, self).__init__(tree, filename)
421-
self.name = []
422-
self.already_checked = []
423-
424-
# Python 2 produces ast.TryExcept and ast.TryFinally nodes, but Python 3
425-
# only produces ast.Try nodes.
426-
if six.PY2:
427-
def visit_TryExcept(self, node):
428-
for handler in node.handlers:
429-
if handler.name:
430-
self.name.append(handler.name.id)
431-
super(CheckForStrUnicodeExc, self).generic_visit(node)
432-
self.name = self.name[:-1]
433-
else:
434-
super(CheckForStrUnicodeExc, self).generic_visit(node)
435-
else:
436-
def visit_Try(self, node):
437-
for handler in node.handlers:
438-
if handler.name:
439-
self.name.append(handler.name)
440-
super(CheckForStrUnicodeExc, self).generic_visit(node)
441-
self.name = self.name[:-1]
442-
else:
443-
super(CheckForStrUnicodeExc, self).generic_visit(node)
444-
445-
def visit_Call(self, node):
446-
if self._check_call_names(node, ['str', 'unicode']):
447-
if node not in self.already_checked:
448-
self.already_checked.append(node)
449-
if isinstance(node.args[0], ast.Name):
450-
if node.args[0].id in self.name:
451-
self.add_error(node.args[0])
452-
super(CheckForStrUnicodeExc, self).generic_visit(node)
453-
454-
455395
class CheckForTransAdd(BaseASTChecker):
456396
"""Checks for the use of concatenation on a translated string.
457397
@@ -723,30 +663,6 @@ def check_doubled_words(physical_line, filename):
723663
return (0, msg % {'word': match.group(1)})
724664

725665

726-
@core.flake8ext
727-
def check_python3_no_iteritems(logical_line):
728-
msg = ("N344: Use items() instead of dict.iteritems().")
729-
730-
if re.search(r".*\.iteritems\(\)", logical_line):
731-
yield (0, msg)
732-
733-
734-
@core.flake8ext
735-
def check_python3_no_iterkeys(logical_line):
736-
msg = ("N345: Use six.iterkeys() instead of dict.iterkeys().")
737-
738-
if re.search(r".*\.iterkeys\(\)", logical_line):
739-
yield (0, msg)
740-
741-
742-
@core.flake8ext
743-
def check_python3_no_itervalues(logical_line):
744-
msg = ("N346: Use six.itervalues() instead of dict.itervalues().")
745-
746-
if re.search(r".*\.itervalues\(\)", logical_line):
747-
yield (0, msg)
748-
749-
750666
@core.flake8ext
751667
def no_os_popen(logical_line):
752668
"""Disallow 'os.popen('

nova/tests/unit/test_hacking.py

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -285,71 +285,6 @@ def _assert_has_errors(self, code, checker, expected_errors=None,
285285
def _assert_has_no_errors(self, code, checker, filename=None):
286286
self._assert_has_errors(code, checker, filename=filename)
287287

288-
def test_str_unicode_exception(self):
289-
290-
checker = checks.CheckForStrUnicodeExc
291-
code = """
292-
def f(a, b):
293-
try:
294-
p = str(a) + str(b)
295-
except ValueError as e:
296-
p = str(e)
297-
return p
298-
"""
299-
errors = [(5, 16, 'N325')]
300-
self._assert_has_errors(code, checker, expected_errors=errors)
301-
302-
code = """
303-
def f(a, b):
304-
try:
305-
p = unicode(a) + str(b)
306-
except ValueError as e:
307-
p = e
308-
return p
309-
"""
310-
self._assert_has_no_errors(code, checker)
311-
312-
code = """
313-
def f(a, b):
314-
try:
315-
p = str(a) + str(b)
316-
except ValueError as e:
317-
p = unicode(e)
318-
return p
319-
"""
320-
errors = [(5, 20, 'N325')]
321-
self._assert_has_errors(code, checker, expected_errors=errors)
322-
323-
code = """
324-
def f(a, b):
325-
try:
326-
p = str(a) + str(b)
327-
except ValueError as e:
328-
try:
329-
p = unicode(a) + unicode(b)
330-
except ValueError as ve:
331-
p = str(e) + str(ve)
332-
p = e
333-
return p
334-
"""
335-
errors = [(8, 20, 'N325'), (8, 29, 'N325')]
336-
self._assert_has_errors(code, checker, expected_errors=errors)
337-
338-
code = """
339-
def f(a, b):
340-
try:
341-
p = str(a) + str(b)
342-
except ValueError as e:
343-
try:
344-
p = unicode(a) + unicode(b)
345-
except ValueError as ve:
346-
p = str(e) + unicode(ve)
347-
p = str(e)
348-
return p
349-
"""
350-
errors = [(8, 20, 'N325'), (8, 33, 'N325'), (9, 16, 'N325')]
351-
self._assert_has_errors(code, checker, expected_errors=errors)
352-
353288
def test_api_version_decorator_check(self):
354289
code = """
355290
@some_other_decorator
@@ -571,27 +506,6 @@ def test_check_doubled_words(self):
571506
code = "'This is the then best comment'\n"
572507
self._assert_has_no_errors(code, checks.check_doubled_words)
573508

574-
def test_dict_iteritems(self):
575-
self.assertEqual(1, len(list(checks.check_python3_no_iteritems(
576-
"obj.iteritems()"))))
577-
578-
self.assertEqual(0, len(list(checks.check_python3_no_iteritems(
579-
"six.iteritems(ob))"))))
580-
581-
def test_dict_iterkeys(self):
582-
self.assertEqual(1, len(list(checks.check_python3_no_iterkeys(
583-
"obj.iterkeys()"))))
584-
585-
self.assertEqual(0, len(list(checks.check_python3_no_iterkeys(
586-
"six.iterkeys(ob))"))))
587-
588-
def test_dict_itervalues(self):
589-
self.assertEqual(1, len(list(checks.check_python3_no_itervalues(
590-
"obj.itervalues()"))))
591-
592-
self.assertEqual(0, len(list(checks.check_python3_no_itervalues(
593-
"six.itervalues(ob))"))))
594-
595509
def test_no_os_popen(self):
596510
code = """
597511
import os
@@ -685,13 +599,6 @@ def test_check_policy_enforce_does_not_catch_other_enforce(self):
685599
"""
686600
self._assert_has_no_errors(code, checks.check_policy_enforce)
687601

688-
def test_check_python3_xrange(self):
689-
func = checks.check_python3_xrange
690-
self.assertEqual(1, len(list(func('for i in xrange(10)'))))
691-
self.assertEqual(1, len(list(func('for i in xrange (10)'))))
692-
self.assertEqual(0, len(list(func('for i in range(10)'))))
693-
self.assertEqual(0, len(list(func('for i in six.moves.range(10)'))))
694-
695602
def test_log_context(self):
696603
code = """
697604
LOG.info("Rebooting instance",

tox.ini

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ extension =
288288
N323 = checks:check_explicit_underscore_import
289289
N324 = checks:use_jsonutils
290290
N332 = checks:check_api_version_decorator
291-
N325 = checks:CheckForStrUnicodeExc
292291
N326 = checks:CheckForTransAdd
293292
N334 = checks:assert_true_or_false_with_in
294293
N336 = checks:dict_constructor_with_list_copy
@@ -300,10 +299,6 @@ extension =
300299
N350 = checks:check_policy_registration_in_central_place
301300
N351 = checks:check_policy_enforce
302301
N343 = checks:check_doubled_words
303-
N344 = checks:check_python3_no_iteritems
304-
N345 = checks:check_python3_no_iterkeys
305-
N346 = checks:check_python3_no_itervalues
306-
N327 = checks:check_python3_xrange
307302
N348 = checks:no_os_popen
308303
N352 = checks:no_log_warn
309304
N349 = checks:CheckForUncalledTestClosure

0 commit comments

Comments
 (0)