def hacking_import_alphabetical(logical_line, blank_lines, previous_logical, indent_level, previous_indent_level): r"""Check for imports in alphabetical order. OpenStack HACKING guide recommendation for imports: imports in human alphabetical order Okay: import os\nimport sys\n\nimport nova\nfrom nova import test Okay: import os\nimport sys H306: import sys\nimport os """ # handle import x # use .lower since capitalization shouldn't dictate order split_line = core.import_normalize(logical_line.strip()).lower().split() split_previous = core.import_normalize(previous_logical.strip()).lower().split() if blank_lines < 1 and indent_level == previous_indent_level: length = [2, 4] if ( len(split_line) in length and len(split_previous) in length and split_line[0] == "import" and split_previous[0] == "import" ): if split_line[1] < split_previous[1]: yield (0, "H306: imports not in alphabetical order (%s, %s)" % (split_previous[1], split_line[1]))
def hacking_import_alphabetical(logical_line, blank_before, previous_logical, indent_level, previous_indent_level): r"""Check for imports in alphabetical order. OpenStack HACKING guide recommendation for imports: imports in human alphabetical order Okay: import os\nimport sys\n\nimport nova\nfrom nova import test Okay: import os\nimport sys H306: import sys\nimport os Okay: import sys\n\n# foo\nimport six """ # handle import x # use .lower since capitalization shouldn't dictate order if blank_before < 1 and indent_level == previous_indent_level: split_line = core.import_normalize(logical_line. strip()).lower().split() split_previous = core.import_normalize(previous_logical. strip()).lower().split() length = [2, 4] if (len(split_line) in length and len(split_previous) in length and split_line[0] == "import" and split_previous[0] == "import"): if split_line[1] < split_previous[1]: yield (0, "H306: imports not in alphabetical order (%s, %s)" % (split_previous[1], split_line[1]))
def hacking_import_groups(logical_line, blank_lines, previous_logical, indent_level, previous_indent_level, physical_line, noqa): r"""Check that imports are grouped correctly. OpenStack HACKING guide recommendation for imports: imports grouped such that Python standard library imports are together, third party library imports are together, and project imports are together Okay: import os\nimport sys\n\nimport six\n\nimport hacking Okay: import six\nimport znon_existent_package Okay: import os\nimport threading H305: import hacking\nimport os H305: import os\nimport six H305: import os\nimport znon_existent_package """ if (noqa or blank_lines > 0 or indent_level != previous_indent_level): return normalized_line = core.import_normalize(logical_line.strip()).split() normalized_previous = core.import_normalize(previous_logical. strip()).split() if normalized_line and normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) if normalized_previous and normalized_previous[0] == 'import': previous_type = _get_import_type(normalized_previous[1]) if current_type != previous_type: yield(0, 'H305: imports not grouped correctly ' '(%s: %s, %s: %s)' % (normalized_previous[1], previous_type, normalized_line[1], current_type))
def hacking_import_groups(logical_line, blank_lines, previous_logical, indent_level, previous_indent_level, physical_line, noqa): r"""Check that imports are grouped correctly. OpenStack HACKING guide recommendation for imports: imports grouped such that Python standard library imports are together, third party library imports are together, and project imports are together Okay: import os\nimport sys\n\nimport six\n\nimport hacking Okay: import six\nimport znon_existent_package H305: import hacking\nimport os H305: import os\nimport six H305: import os\nimport znon_existent_package """ if (noqa or blank_lines > 0 or indent_level != previous_indent_level): return normalized_line = core.import_normalize(logical_line.strip()).split() normalized_previous = core.import_normalize( previous_logical.strip()).split() if normalized_line and normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) if normalized_previous and normalized_previous[0] == 'import': previous_type = _get_import_type(normalized_previous[1]) if current_type != previous_type: yield (0, 'H305: imports not grouped correctly ' '(%s: %s, %s: %s)' % (normalized_previous[1], previous_type, normalized_line[1], current_type))
def hacking_import_groups_together(logical_line, blank_lines, indent_level, previous_indent_level, line_number, physical_line, filename, noqa): r"""Check that like imports are grouped together. OpenStack HACKING guide recommendation for imports: Imports should be grouped together by type. Okay: import os\nimport sys Okay: try:\n import foo\nexcept ImportError:\n pass\n\nimport six H307: import os\n\nimport sys """ if line_number == 1 or filename != together_data.current_filename: together_data.current_group = None together_data.current_filename = filename if noqa: return normalized_line = core.import_normalize(logical_line.strip()).split() if normalized_line and normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) previous_import = together_data.current_import together_data.current_import = normalized_line[1] matched = current_type == together_data.current_group together_data.current_group = current_type if (matched and indent_level == previous_indent_level and blank_lines >= 1): yield(0, 'H307: like imports should be grouped together (%s and ' '%s from %s are separated by whitespace)' % (previous_import, together_data.current_import, current_type))
def hacking_import_groups_together(logical_line, blank_lines, indent_level, previous_indent_level, line_number, physical_line, filename, noqa): r"""Check that like imports are grouped together. OpenStack HACKING guide recommendation for imports: Imports should be grouped together by type. Okay: import os\nimport sys Okay: try:\n import foo\nexcept ImportError:\n pass\n\nimport six H307: import os\n\nimport sys """ if line_number == 1 or filename != together_data.current_filename: together_data.current_group = None together_data.current_filename = filename if noqa: return normalized_line = core.import_normalize(logical_line.strip()).split() if normalized_line and normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) previous_import = together_data.current_import together_data.current_import = normalized_line[1] matched = current_type == together_data.current_group together_data.current_group = current_type if (matched and indent_level == previous_indent_level and blank_lines >= 1): yield ( 0, 'H307: like imports should be grouped together (%s and ' '%s from %s are separated by whitespace)' % (previous_import, together_data.current_import, current_type))
def hacking_import_groups(logical_line, blank_before, previous_logical, indent_level, previous_indent_level, physical_line, noqa): r"""Check that imports are grouped correctly. OpenStack HACKING guide recommendation for imports: imports grouped such that Python standard library imports are together, third party library imports are together, and project imports are together This check allows mixing of third-party and stdlib modules so we don't fail on new Python 3 stdlib modules that used to be third-party. Okay: import os\nimport sys\n\nimport six\n\nimport hacking Okay: import six\nimport znon_existent_package Okay: import os\nimport threading Okay: import mock\nimport os H305: import hacking\nimport os H305: import hacking\nimport nonexistent H305: import hacking\nimport mock """ if (noqa or blank_before > 0 or indent_level != previous_indent_level): return normalized_line = core.import_normalize(logical_line.strip()).split() normalized_previous = core.import_normalize(previous_logical. strip()).split() def compatible(previous, current): if previous == current: return True if ((previous in ['stdlib', 'third-party']) and (current in ['stdlib', 'third-party'])): return True return False if normalized_line and normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) if normalized_previous and normalized_previous[0] == 'import': previous_type = _get_import_type(normalized_previous[1]) if not compatible(previous_type, current_type): yield(0, 'H305: imports not grouped correctly ' '(%s: %s, %s: %s)' % (normalized_previous[1], previous_type, normalized_line[1], current_type))
def hacking_import_groups(logical_line, blank_before, previous_logical, indent_level, previous_indent_level, physical_line, noqa): r"""Check that imports are grouped correctly. OpenStack HACKING guide recommendation for imports: imports grouped such that Python standard library imports are together, third party library imports are together, and project imports are together This check allows mixing of third-party and stdlib modules so we don't fail on new Python 3 stdlib modules that used to be third-party. Okay: import os\nimport sys\n\nimport six\n\nimport hacking Okay: import six\nimport znon_existent_package Okay: import os\nimport threading Okay: import mock\nimport os H305: import hacking\nimport os H305: import hacking\nimport nonexistent H305: import hacking\nimport mock """ if (noqa or blank_before > 0 or indent_level != previous_indent_level): return normalized_line = core.import_normalize(logical_line.strip()).split() normalized_previous = core.import_normalize( previous_logical.strip()).split() def compatible(previous, current): if previous == current: return True if ((previous in ['stdlib', 'third-party']) and (current in ['stdlib', 'third-party'])): return True return False if normalized_line and normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) if normalized_previous and normalized_previous[0] == 'import': previous_type = _get_import_type(normalized_previous[1]) if not compatible(previous_type, current_type): yield (0, 'H305: imports not grouped correctly ' '(%s: %s, %s: %s)' % (normalized_previous[1], previous_type, normalized_line[1], current_type))
def hacking_import_groups_together(logical_line, blank_lines, indent_level, previous_indent_level, line_number, physical_line, filename, noqa): r"""Check that like imports are grouped together. OpenStack HACKING guide recommendation for imports: Imports should be grouped together by type. This check allows mixing of third-party libs into the stdlib group in order to accomodate new Python 3 stdlib modules. Okay: import os\nimport sys Okay: try:\n import foo\nexcept ImportError:\n pass\n\nimport six Okay: import mock\n\nimport six Okay: import abc\nimport mock\n\nimport six Okay: import eventlet\neventlet.monkey_patch()\n\nimport copy H307: import os\n\nimport sys H307: import mock\nimport os\n\nimport sys """ if line_number == 1 or filename != together_data.current_filename: together_data.current_group = None together_data.current_filename = filename if noqa: return def update_current_group(current): if together_data.current_group is None: if current in ['stdlib', 'third-party']: # Assume stdlib until we know otherwise together_data.current_group = 'stdlib' return if (together_data.current_group == 'stdlib' and current == 'third-party' and blank_lines < 1): # Keep the current group stdlib, since there may be third-party # libs in that group return together_data.current_group = current normalized_line = core.import_normalize(logical_line.strip()).split() if normalized_line: if normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) previous_import = together_data.current_import together_data.current_import = normalized_line[1] matched = current_type == together_data.current_group update_current_group(current_type) if (matched and indent_level == previous_indent_level and blank_lines >= 1): yield(0, 'H307: like imports should be grouped together (%s ' 'and %s from %s are separated by whitespace)' % (previous_import, together_data.current_import, current_type)) else: # Reset on non-import code together_data.current_group = None
def hacking_import_groups_together(logical_line, blank_lines, indent_level, previous_indent_level, line_number, physical_line, filename, noqa): r"""Check that like imports are grouped together. OpenStack HACKING guide recommendation for imports: Imports should be grouped together by type. This check allows mixing of third-party libs into the stdlib group in order to accomodate new Python 3 stdlib modules. Okay: import os\nimport sys Okay: try:\n import foo\nexcept ImportError:\n pass\n\nimport six Okay: import mock\n\nimport six Okay: import abc\nimport mock\n\nimport six Okay: import eventlet\neventlet.monkey_patch()\n\nimport copy H307: import os\n\nimport sys H307: import mock\nimport os\n\nimport sys """ if line_number == 1 or filename != together_data.current_filename: together_data.current_group = None together_data.current_filename = filename if noqa: return def update_current_group(current): if together_data.current_group is None: if current in ['stdlib', 'third-party']: # Assume stdlib until we know otherwise together_data.current_group = 'stdlib' return if (together_data.current_group == 'stdlib' and current == 'third-party' and blank_lines < 1): # Keep the current group stdlib, since there may be third-party # libs in that group return together_data.current_group = current normalized_line = core.import_normalize(logical_line.strip()).split() if normalized_line: if normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) previous_import = together_data.current_import together_data.current_import = normalized_line[1] matched = current_type == together_data.current_group update_current_group(current_type) if (matched and indent_level == previous_indent_level and blank_lines >= 1): yield (0, 'H307: like imports should be grouped together (%s ' 'and %s from %s are separated by whitespace)' % (previous_import, together_data.current_import, current_type)) else: # Reset on non-import code together_data.current_group = None
def hacking_import_groups(logical_line, blank_before, previous_logical, indent_level, previous_indent_level, physical_line, noqa): r"""Check that imports are grouped correctly. OpenStack HACKING guide recommendation for imports: imports grouped such that Python standard library imports are together, third party library imports are together, and project imports are together Okay: import os\nimport sys\n\nimport six\n\nimport hacking Okay: import six\nimport znon_existent_package Okay: import os\nimport threading S366: import mock\nimport os S366: import hacking\nimport os S366: import hacking\nimport nonexistent S366: import hacking\nimport mock """ if (noqa or blank_before > 0 or indent_level != previous_indent_level): return normalized_line = core.import_normalize(logical_line.strip()).split() normalized_previous = core.import_normalize( previous_logical.strip()).split() def compatible(previous, current): if previous == current: return True if normalized_line and normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) if normalized_previous and normalized_previous[0] == 'import': previous_type = _get_import_type(normalized_previous[1]) if not compatible(previous_type, current_type): yield (0, 'S366: imports not grouped correctly ' '(%s: %s, %s: %s)' % (normalized_previous[1], previous_type, normalized_line[1], current_type))
def hacking_no_old_style_class(logical_line, noqa): r"""Check for old style classes. Examples: Okay: class Foo(object):\n pass Okay: class Foo(Bar, Baz):\n pass Okay: class Foo(object, Baz):\n pass H238: class Bar:\n pass """ if noqa: return line = core.import_normalize(logical_line.strip()) if line.startswith("class ") and not RE_NEW_STYLE_CLASS.match(line): yield (0, "H238: old style class declaration, " "use new style (inherit from `object`)")
def hacking_no_old_style_class(logical_line, noqa): r"""Check for old style classes. Examples: Okay: class Foo(object):\n pass Okay: class Foo(Bar, Baz):\n pass Okay: class Foo(object, Baz):\n pass Okay: class Foo(somefunc()):\n pass H238: class Bar:\n pass H238: class Bar():\n pass """ if noqa: return line = core.import_normalize(logical_line.strip()) if line.startswith("class ") and not RE_NEW_STYLE_CLASS.match(line): yield (0, "H238: old style class declaration, " "use new style (inherit from `object`)")
def hacking_import_groups_together(logical_line, blank_lines, indent_level, previous_indent_level, line_number, physical_line, filename, noqa): r"""Check that like imports are grouped together. OpenStack HACKING guide recommendation for imports: Imports should be grouped together by type. Okay: import os\nimport sys Okay: try:\n import foo\nexcept ImportError:\n pass\n\nimport six Okay: import abc\nimport mock\n\nimport six Okay: import eventlet\neventlet.monkey_patch()\n\nimport copy S367: import mock\n\nimport six S367: import os\n\nimport sys S367: import mock\nimport os\n\nimport sys """ if line_number == 1 or filename != together_data.current_filename: together_data.current_group = None together_data.current_filename = filename if noqa: return def update_current_group(current): together_data.current_group = current normalized_line = core.import_normalize(logical_line.strip()).split() if normalized_line: if normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) previous_import = together_data.current_import together_data.current_import = normalized_line[1] matched = current_type == together_data.current_group update_current_group(current_type) if (matched and indent_level == previous_indent_level and blank_lines >= 1): yield(0, 'S367: like imports should be grouped together (%s ' 'and %s from %s are separated by whitespace)' % (previous_import, together_data.current_import, current_type)) else: # Reset on non-import code together_data.current_group = None
def hacking_import_groups_together(logical_line, blank_lines, indent_level, previous_indent_level, line_number, physical_line, filename, noqa): r"""Check that like imports are grouped together. OpenStack HACKING guide recommendation for imports: Imports should be grouped together by type. Okay: import os\nimport sys Okay: try:\n import foo\nexcept ImportError:\n pass\n\nimport six Okay: import abc\nimport mock\n\nimport six Okay: import eventlet\neventlet.monkey_patch()\n\nimport copy S367: import mock\n\nimport six S367: import os\n\nimport sys S367: import mock\nimport os\n\nimport sys """ if line_number == 1 or filename != together_data.current_filename: together_data.current_group = None together_data.current_filename = filename if noqa: return def update_current_group(current): together_data.current_group = current normalized_line = core.import_normalize(logical_line.strip()).split() if normalized_line: if normalized_line[0] == 'import': current_type = _get_import_type(normalized_line[1]) previous_import = together_data.current_import together_data.current_import = normalized_line[1] matched = current_type == together_data.current_group update_current_group(current_type) if (matched and indent_level == previous_indent_level and blank_lines >= 1): yield (0, 'S367: like imports should be grouped together (%s ' 'and %s from %s are separated by whitespace)' % (previous_import, together_data.current_import, current_type)) else: # Reset on non-import code together_data.current_group = None
def hacking_no_removed_module(logical_line, noqa): r"""Check for removed modules in Python 3. Examples: Okay: from os import path Okay: from os import path as p Okay: from os import (path as p) Okay: import os.path H237: import thread Okay: import thread # noqa H237: import commands H237: import md5 as std_md5 """ if noqa: return line = core.import_normalize(logical_line.strip()) if line and line.split()[0] == 'import': module_name = line.split()[1].split('.')[0] if module_name in removed_modules: yield 0, ("H237: module %s is " "removed in Python 3" % module_name)