From 6581a04176eea4dc3401abe3a35945870f3f9912 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 2 Dec 2022 16:35:12 +0100 Subject: [PATCH] pre-commit: Add psf/black --- .pre-commit-config.yaml | 5 + flake8_import_order/__about__.py | 21 +-- flake8_import_order/__init__.py | 48 +++++-- flake8_import_order/checker.py | 22 +-- flake8_import_order/flake8_linter.py | 22 +-- flake8_import_order/pylama_linter.py | 17 ++- flake8_import_order/styles.py | 131 ++++++++++-------- tests/test_cases/additional_newline.py | 14 +- .../additional_newline_cryptography.py | 2 +- tests/test_cases/additional_newline_edited.py | 14 +- tests/test_cases/missing_newline.py | 5 +- .../missing_newline_cryptography.py | 2 +- tests/test_cases/mixed_groups.py | 2 +- tests/test_cases/noqa.py | 4 +- tests/test_cases/stdlib_shadowing.py | 2 +- tests/test_cases/wrong_from_import_order.py | 6 +- .../wrong_from_import_order_cryptography.py | 6 +- tests/test_cases/wrong_from_import_same.py | 2 +- tests/test_cases/wrong_import_order.py | 2 +- .../wrong_import_order_cryptography.py | 2 +- tests/test_cases/wrong_relative_order.py | 2 +- tests/test_cases/wrong_section.py | 4 +- tests/test_cases/wrong_section_appnexus.py | 6 +- tests/test_flake8_linter.py | 28 ++-- tests/test_pylama_linter.py | 8 +- tests/test_stdlib.py | 2 +- tests/test_style_cases.py | 22 +-- tox.ini | 4 +- 28 files changed, 229 insertions(+), 176 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c098e83..1d49968 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,6 +13,11 @@ repos: - id: mixed-line-ending - id: trailing-whitespace + - repo: https://github.com/psf/black + rev: 22.10.0 + hooks: + - id: black # See pyproject.toml for args + - repo: https://github.com/codespell-project/codespell rev: v2.2.2 hooks: diff --git a/flake8_import_order/__about__.py b/flake8_import_order/__about__.py index af23381..9cd434b 100644 --- a/flake8_import_order/__about__.py +++ b/flake8_import_order/__about__.py @@ -1,13 +1,18 @@ __all__ = [ - "__title__", "__summary__", "__uri__", "__version__", "__author__", - "__email__", "__license__", "__copyright__", '__maintainer__', - '__maintainer_email__', + "__title__", + "__summary__", + "__uri__", + "__version__", + "__author__", + "__email__", + "__license__", + "__copyright__", + "__maintainer__", + "__maintainer_email__", ] __title__ = "flake8-import-order" -__summary__ = ( - "Flake8 and pylama plugin that checks the ordering of import statements." -) +__summary__ = "Flake8 and pylama plugin that checks the ordering of import statements." __uri__ = "https://github.com/PyCQA/flake8-import-order" __version__ = "0.18.2" @@ -15,8 +20,8 @@ __author__ = "Alex Stapleton" __email__ = "alexs@prol.etari.at" -__maintainer__ = 'Phil Jones' -__maintainer_email__ = 'philip.graham.jones+flake8-import@gmail.com' +__maintainer__ = "Phil Jones" +__maintainer_email__ = "philip.graham.jones+flake8-import@gmail.com" __license__ = "LGPLv3" __copyright__ = "Copyright 2013-2016 %s" % __author__ diff --git a/flake8_import_order/__init__.py b/flake8_import_order/__init__.py index fcf8fdb..423bc50 100644 --- a/flake8_import_order/__init__.py +++ b/flake8_import_order/__init__.py @@ -3,23 +3,35 @@ from enum import IntEnum from .__about__ import ( - __author__, __copyright__, __email__, __license__, __summary__, __title__, - __uri__, __version__, + __author__, + __copyright__, + __email__, + __license__, + __summary__, + __title__, + __uri__, + __version__, ) from .stdlib_list import STDLIB_NAMES __all__ = [ - "__title__", "__summary__", "__uri__", "__version__", "__author__", - "__email__", "__license__", "__copyright__", + "__title__", + "__summary__", + "__uri__", + "__version__", + "__author__", + "__email__", + "__license__", + "__copyright__", ] -DEFAULT_IMPORT_ORDER_STYLE = 'cryptography' +DEFAULT_IMPORT_ORDER_STYLE = "cryptography" ClassifiedImport = namedtuple( - 'ClassifiedImport', - ['type', 'is_from', 'modules', 'names', 'lineno', 'level', 'package'], + "ClassifiedImport", + ["type", "is_from", "modules", "names", "lineno", "level", "package"], ) -NewLine = namedtuple('NewLine', ['lineno']) +NewLine = namedtuple("NewLine", ["lineno"]) class ImportType(IntEnum): @@ -48,7 +60,7 @@ def get_package_names(name): package_names = [last_package_name] for part in reversed(parts): - last_package_name = f'{last_package_name}.{part}' + last_package_name = f"{last_package_name}.{part}" package_names.append(last_package_name) return package_names @@ -64,7 +76,6 @@ def root_package_name(name): class ImportVisitor(ast.NodeVisitor): - def __init__(self, application_import_names, application_package_names): self.imports = [] self.application_import_names = frozenset(application_import_names) @@ -79,22 +90,31 @@ def visit_Import(self, node): # noqa: N802 else: type_ = ImportType.MIXED classified_import = ClassifiedImport( - type_, False, modules, [], node.lineno, 0, + type_, + False, + modules, + [], + node.lineno, + 0, root_package_name(modules[0]), ) self.imports.append(classified_import) def visit_ImportFrom(self, node): # noqa: N802 if node.col_offset == 0: - module = node.module or '' + module = node.module or "" if node.level > 0: type_ = ImportType.APPLICATION_RELATIVE else: type_ = self._classify_type(module) names = [alias.name for alias in node.names] classified_import = ClassifiedImport( - type_, True, [module], names, - node.lineno, node.level, + type_, + True, + [module], + names, + node.lineno, + node.level, root_package_name(module), ) self.imports.append(classified_import) diff --git a/flake8_import_order/checker.py b/flake8_import_order/checker.py index 7bcd356..2e49e5a 100644 --- a/flake8_import_order/checker.py +++ b/flake8_import_order/checker.py @@ -7,7 +7,7 @@ from flake8_import_order import ImportVisitor, NewLine from flake8_import_order.styles import lookup_entry_point -DEFAULT_IMPORT_ORDER_STYLE = 'cryptography' +DEFAULT_IMPORT_ORDER_STYLE = "cryptography" NOQA_INLINE_REGEXP = re.compile( # We're looking for items that look like this: # ``# noqa`` @@ -18,11 +18,11 @@ # We do not care about the ``: `` that follows ``noqa`` # We do not care about the casing of ``noqa`` # We want a comma-separated list of errors - r'# noqa(?:: (?P([A-Z][0-9]+(?:[,\s]+)?)+))?', - re.IGNORECASE + r"# noqa(?:: (?P([A-Z][0-9]+(?:[,\s]+)?)+))?", + re.IGNORECASE, ) -COMMA_SEPARATED_LIST_RE = re.compile(r'[,\s]') -BLANK_LINE_RE = re.compile(r'\s*\n') +COMMA_SEPARATED_LIST_RE = re.compile(r"[,\s]") +BLANK_LINE_RE = re.compile(r"\s*\n") class ImportOrderChecker: @@ -42,7 +42,7 @@ def load_file(self): self.lines = pycodestyle.readlines(self.filename) if self.tree is None: - self.tree = ast.parse(''.join(self.lines)) + self.tree = ast.parse("".join(self.lines)) def error(self, error): return error @@ -52,19 +52,19 @@ def check_order(self): self.load_file() try: - style_entry_point = self.options['import_order_style'] + style_entry_point = self.options["import_order_style"] except KeyError: style_entry_point = lookup_entry_point(DEFAULT_IMPORT_ORDER_STYLE) style_cls = style_entry_point.load() if style_cls.accepts_application_package_names: visitor = self.visitor_class( - self.options.get('application_import_names', []), - self.options.get('application_package_names', []), + self.options.get("application_import_names", []), + self.options.get("application_package_names", []), ) else: visitor = self.visitor_class( - self.options.get('application_import_names', []), + self.options.get("application_import_names", []), [], ) visitor.visit(self.tree) @@ -90,7 +90,7 @@ def error_is_ignored(self, error): if noqa_match is None: return False - codes_str = noqa_match.group('codes') + codes_str = noqa_match.group("codes") if codes_str is None: return True diff --git a/flake8_import_order/flake8_linter.py b/flake8_import_order/flake8_linter.py index 07bb4d3..42f8218 100644 --- a/flake8_import_order/flake8_linter.py +++ b/flake8_import_order/flake8_linter.py @@ -2,7 +2,8 @@ from flake8_import_order import __version__ from flake8_import_order.checker import ( - DEFAULT_IMPORT_ORDER_STYLE, ImportOrderChecker, + DEFAULT_IMPORT_ORDER_STYLE, + ImportOrderChecker, ) from flake8_import_order.styles import list_entry_points, lookup_entry_point @@ -34,8 +35,10 @@ def add_options(cls, parser): default="", action="store", type=str, - help=("Package names to consider as company-specific " - "(used only by 'appnexus' style)"), + help=( + "Package names to consider as company-specific " + "(used only by 'appnexus' style)" + ), parse_from_config=True, comma_separated_list=True, ) @@ -45,8 +48,9 @@ def add_options(cls, parser): default=DEFAULT_IMPORT_ORDER_STYLE, action="store", type=str, - help=("Style to follow. Available: " + - ", ".join(cls.list_available_styles())), + help=( + "Style to follow. Available: " + ", ".join(cls.list_available_styles()) + ), parse_from_config=True, ) @@ -95,9 +99,9 @@ def register_opt(parser, *args, **kwargs): parser.add_option(*args, **kwargs) except (optparse.OptionError, TypeError): # Flake8 2.x registration - parse_from_config = kwargs.pop('parse_from_config', False) - kwargs.pop('comma_separated_list', False) - kwargs.pop('normalize_paths', False) + parse_from_config = kwargs.pop("parse_from_config", False) + kwargs.pop("comma_separated_list", False) + kwargs.pop("normalize_paths", False) parser.add_option(*args, **kwargs) if parse_from_config: - parser.config_options.append(args[-1].lstrip('-')) + parser.config_options.append(args[-1].lstrip("-")) diff --git a/flake8_import_order/pylama_linter.py b/flake8_import_order/pylama_linter.py index d85f49b..cb3155d 100644 --- a/flake8_import_order/pylama_linter.py +++ b/flake8_import_order/pylama_linter.py @@ -2,7 +2,8 @@ from flake8_import_order import __version__ from flake8_import_order.checker import ( - DEFAULT_IMPORT_ORDER_STYLE, ImportOrderChecker, + DEFAULT_IMPORT_ORDER_STYLE, + ImportOrderChecker, ) from flake8_import_order.styles import lookup_entry_point @@ -19,19 +20,17 @@ def allow(self, path): def error(self, error): return { - 'lnum': error.lineno, - 'col': 0, - 'text': error.message, - 'type': error.code, + "lnum": error.lineno, + "col": 0, + "text": error.message, + "type": error.code, } def run(self, path, **meta): self.filename = path self.ast_tree = None - meta.setdefault('import_order_style', DEFAULT_IMPORT_ORDER_STYLE) - meta['import_order_style'] = lookup_entry_point( - meta['import_order_style'] - ) + meta.setdefault("import_order_style", DEFAULT_IMPORT_ORDER_STYLE) + meta["import_order_style"] = lookup_entry_point(meta["import_order_style"]) self.options = meta yield from self.check_order() diff --git a/flake8_import_order/styles.py b/flake8_import_order/styles.py index b5826a3..ef10136 100644 --- a/flake8_import_order/styles.py +++ b/flake8_import_order/styles.py @@ -4,18 +4,18 @@ from flake8_import_order import ClassifiedImport, ImportType, NewLine -Error = namedtuple('Error', ['lineno', 'code', 'message']) +Error = namedtuple("Error", ["lineno", "code", "message"]) def list_entry_points(): - return iter_entry_points('flake8_import_order.styles') + return iter_entry_points("flake8_import_order.styles") def lookup_entry_point(name): try: - return next(iter_entry_points('flake8_import_order.styles', name=name)) + return next(iter_entry_points("flake8_import_order.styles", name=name)) except StopIteration: - raise LookupError(f'Unknown style {name}') + raise LookupError(f"Unknown style {name}") class Style: @@ -40,27 +40,31 @@ def _check(self, previous_import, previous, current_import): if previous_import is not None: yield from self._check_I100(previous_import, current_import) yield from self._check_I201( - previous_import, previous, current_import, + previous_import, + previous, + current_import, ) yield from self._check_I202( - previous_import, previous, current_import, + previous_import, + previous, + current_import, ) def _check_I666(self, current_import): # noqa: N802 if current_import.type == ImportType.MIXED: yield Error( current_import.lineno, - 'I666', - 'Import statement mixes groups', + "I666", + "Import statement mixes groups", ) def _check_I101(self, current_import): # noqa: N802 correct_names = self.sorted_names(current_import.names) if correct_names != current_import.names: - corrected = ', '.join(correct_names) + corrected = ", ".join(correct_names) yield Error( current_import.lineno, - 'I101', + "I101", "Imported names are in the wrong order. " "Should be {}".format(corrected), ) @@ -69,19 +73,20 @@ def _check_I100(self, previous_import, current_import): # noqa: N802 previous_key = self.import_key(previous_import) current_key = self.import_key(current_import) if previous_key > current_key: - message = ( - "Import statements are in the wrong order. " - "'{}' should be before '{}'" - ).format( - self._explain_import(current_import), - self._explain_import(previous_import), - ) - same_section = self.same_section( - previous_import, current_import, - ) - if not same_section: - message = f"{message} and in a different group." - yield Error(current_import.lineno, 'I100', message) + message = ( + "Import statements are in the wrong order. " + "'{}' should be before '{}'" + ).format( + self._explain_import(current_import), + self._explain_import(previous_import), + ) + same_section = self.same_section( + previous_import, + current_import, + ) + if not same_section: + message = f"{message} and in a different group." + yield Error(current_import.lineno, "I100", message) def _check_I201(self, previous_import, previous, current_import): # noqa: N802,E501 same_section = self.same_section(previous_import, current_import) @@ -89,10 +94,11 @@ def _check_I201(self, previous_import, previous, current_import): # noqa: N802, if not same_section and not has_newline: yield Error( current_import.lineno, - 'I201', + "I201", "Missing newline between import groups. {}".format( self._explain_grouping( - current_import, previous_import, + current_import, + previous_import, ) ), ) @@ -103,10 +109,11 @@ def _check_I202(self, previous_import, previous, current_import): # noqa: N802, if same_section and has_newline: yield Error( current_import.lineno, - 'I202', + "I202", "Additional newline in a group of imports. {}".format( self._explain_grouping( - current_import, previous_import, + current_import, + previous_import, ) ), ) @@ -122,34 +129,30 @@ def import_key(import_): @staticmethod def same_section(previous, current): same_type = current.type == previous.type - both_first = ( - {previous.type, current.type} <= { - ImportType.APPLICATION, ImportType.APPLICATION_RELATIVE, - } - ) + both_first = {previous.type, current.type} <= { + ImportType.APPLICATION, + ImportType.APPLICATION_RELATIVE, + } return same_type or both_first @staticmethod def _explain_import(import_): if import_.is_from: return "from {}{} import {}".format( - import_.level * '.', - ', '.join(import_.modules), - ', '.join(import_.names), + import_.level * ".", + ", ".join(import_.modules), + ", ".join(import_.names), ) else: - return "import {}".format(', '.join(import_.modules)) + return "import {}".format(", ".join(import_.modules)) @staticmethod def _explain_grouping(current_import, previous_import): - return ( - "'{}' is identified as {} and " - "'{}' is identified as {}." - ).format( + return ("'{}' is identified as {} and " "'{}' is identified as {}.").format( Style._explain_import(current_import), - current_import.type.name.title().replace('_', ' '), + current_import.type.name.title().replace("_", " "), Style._explain_import(previous_import), - previous_import.type.name.title().replace('_', ' '), + previous_import.type.name.title().replace("_", " "), ) @@ -158,7 +161,6 @@ class PEP8(Style): class Google(Style): - @staticmethod def sorted_names(names): return sorted(names, key=Google.name_key) @@ -179,7 +181,6 @@ class AppNexus(Google): class Smarkets(Style): - @staticmethod def sorted_names(names): return sorted(names, key=Smarkets.name_key) @@ -205,10 +206,11 @@ def _check_I202(self, previous_import, previous, current_import): # noqa: N802, if same_section and has_newline and not optional_split: yield Error( current_import.lineno, - 'I202', + "I202", "Additional newline in a group of imports. {}".format( self._explain_grouping( - current_import, previous_import, + current_import, + previous_import, ) ), ) @@ -225,7 +227,13 @@ def sorted_names(names): @staticmethod def import_key(import_): - return (import_.type, import_.is_from, import_.level, import_.modules, import_.names) + return ( + import_.type, + import_.is_from, + import_.level, + import_.modules, + import_.names, + ) class ISort(PyCharm): @@ -241,7 +249,6 @@ def sorted_names(names): class Cryptography(Style): - @staticmethod def sorted_names(names): return sorted(names) @@ -250,26 +257,32 @@ def sorted_names(names): def import_key(import_): if import_.type in {ImportType.THIRD_PARTY, ImportType.APPLICATION}: return ( - import_.type, import_.package, import_.is_from, - import_.level, import_.modules, import_.names, + import_.type, + import_.package, + import_.is_from, + import_.level, + import_.modules, + import_.names, ) else: return ( - import_.type, '', import_.is_from, import_.level, - import_.modules, import_.names, + import_.type, + "", + import_.is_from, + import_.level, + import_.modules, + import_.names, ) @staticmethod def same_section(previous, current): app_or_third = current.type in { - ImportType.THIRD_PARTY, ImportType.APPLICATION, + ImportType.THIRD_PARTY, + ImportType.APPLICATION, } same_type = current.type == previous.type - both_relative = ( - previous.type == current.type == ImportType.APPLICATION_RELATIVE - ) + both_relative = previous.type == current.type == ImportType.APPLICATION_RELATIVE same_package = previous.package == current.package - return ( - (not app_or_third and same_type or both_relative) or - (app_or_third and same_package) + return (not app_or_third and same_type or both_relative) or ( + app_or_third and same_package ) diff --git a/tests/test_cases/additional_newline.py b/tests/test_cases/additional_newline.py index 95c4711..8d26819 100644 --- a/tests/test_cases/additional_newline.py +++ b/tests/test_cases/additional_newline.py @@ -1,18 +1,20 @@ # appnexus google pep8 smarkets import ast + # This comment should not trigger a I202 (not a newline) import os -import signal # I202 +import signal # I202 import X -from X import B, b, \ - C, d +from X import B, b, C, d -from Y import A # I202 +from Y import A # I202 from Y import ( - B, b, - C, d, + B, + b, + C, + d, ) from Z import A diff --git a/tests/test_cases/additional_newline_cryptography.py b/tests/test_cases/additional_newline_cryptography.py index 9e0a7bd..8f33fa9 100644 --- a/tests/test_cases/additional_newline_cryptography.py +++ b/tests/test_cases/additional_newline_cryptography.py @@ -1,7 +1,7 @@ # cryptography import ast -import signal # I202 +import signal # I202 import X diff --git a/tests/test_cases/additional_newline_edited.py b/tests/test_cases/additional_newline_edited.py index 605324b..a1e40fd 100644 --- a/tests/test_cases/additional_newline_edited.py +++ b/tests/test_cases/additional_newline_edited.py @@ -1,18 +1,20 @@ # edited import ast + # This comment should not trigger a I202 (not a newline) import os -import signal # I202 +import signal # I202 import X -from X import B, b, \ - C, d +from X import B, b, C, d -from Y import A # I202 +from Y import A # I202 from Y import ( - B, b, - C, d, + B, + b, + C, + d, ) from Z import A diff --git a/tests/test_cases/missing_newline.py b/tests/test_cases/missing_newline.py index 8eb796c..f13546c 100644 --- a/tests/test_cases/missing_newline.py +++ b/tests/test_cases/missing_newline.py @@ -1,5 +1,6 @@ # appnexus cryptography edited google pep8 smarkets import ast + # This comment should not prevent the I201 below, it is not a newline. -import X # I201 -import flake8_import_order # I201 +import X # I201 +import flake8_import_order # I201 diff --git a/tests/test_cases/missing_newline_cryptography.py b/tests/test_cases/missing_newline_cryptography.py index 8ebd7bf..031b2e0 100644 --- a/tests/test_cases/missing_newline_cryptography.py +++ b/tests/test_cases/missing_newline_cryptography.py @@ -1,3 +1,3 @@ # cryptography import flake8_import_order -import tests # I201 +import tests # I201 diff --git a/tests/test_cases/mixed_groups.py b/tests/test_cases/mixed_groups.py index cf22129..1ab631c 100644 --- a/tests/test_cases/mixed_groups.py +++ b/tests/test_cases/mixed_groups.py @@ -1,2 +1,2 @@ # appnexus cryptography edited google pep8 smarkets -import ast, X, flake_import_order # I666 +import ast, X, flake_import_order # I666 diff --git a/tests/test_cases/noqa.py b/tests/test_cases/noqa.py index efb6945..08efff7 100644 --- a/tests/test_cases/noqa.py +++ b/tests/test_cases/noqa.py @@ -3,7 +3,7 @@ import sys # noqa: I202 -import os # noqa +import os # noqa import unittest -import X # noqa +import X # noqa from . import B, C, A # I201 # noqa: I101 diff --git a/tests/test_cases/stdlib_shadowing.py b/tests/test_cases/stdlib_shadowing.py index eb4343e..cb15f98 100644 --- a/tests/test_cases/stdlib_shadowing.py +++ b/tests/test_cases/stdlib_shadowing.py @@ -1,3 +1,3 @@ # appnexus cryptography google pep8 smarkets from .filesystem import FilesystemStorage -from os import path # I100 I201 +from os import path # I100 I201 diff --git a/tests/test_cases/wrong_from_import_order.py b/tests/test_cases/wrong_from_import_order.py index 3b18815..79a99ea 100644 --- a/tests/test_cases/wrong_from_import_order.py +++ b/tests/test_cases/wrong_from_import_order.py @@ -1,4 +1,4 @@ # appnexus edited google smarkets -from A import a, A # I101 -from B import b, A # I101 -from C import b, a # I101 +from A import a, A # I101 +from B import b, A # I101 +from C import b, a # I101 diff --git a/tests/test_cases/wrong_from_import_order_cryptography.py b/tests/test_cases/wrong_from_import_order_cryptography.py index e471eb5..da779ea 100644 --- a/tests/test_cases/wrong_from_import_order_cryptography.py +++ b/tests/test_cases/wrong_from_import_order_cryptography.py @@ -1,6 +1,6 @@ # cryptography -from A import a, A # I101 +from A import a, A # I101 -from B import A, a, B # I101 +from B import A, a, B # I101 -from C import b, a # I101 +from C import b, a # I101 diff --git a/tests/test_cases/wrong_from_import_same.py b/tests/test_cases/wrong_from_import_same.py index 70f8dd2..fca0500 100644 --- a/tests/test_cases/wrong_from_import_same.py +++ b/tests/test_cases/wrong_from_import_same.py @@ -1,3 +1,3 @@ # cryptography from os import system -from os import path # I100 +from os import path # I100 diff --git a/tests/test_cases/wrong_import_order.py b/tests/test_cases/wrong_import_order.py index 0bcac9e..54e6e2b 100644 --- a/tests/test_cases/wrong_import_order.py +++ b/tests/test_cases/wrong_import_order.py @@ -1,3 +1,3 @@ # appnexus edited google smarkets import a -import A # I100 +import A # I100 diff --git a/tests/test_cases/wrong_import_order_cryptography.py b/tests/test_cases/wrong_import_order_cryptography.py index 4ed0df0..5e36dad 100644 --- a/tests/test_cases/wrong_import_order_cryptography.py +++ b/tests/test_cases/wrong_import_order_cryptography.py @@ -3,4 +3,4 @@ import a -import B # I100 +import B # I100 diff --git a/tests/test_cases/wrong_relative_order.py b/tests/test_cases/wrong_relative_order.py index 4ca180f..a251142 100644 --- a/tests/test_cases/wrong_relative_order.py +++ b/tests/test_cases/wrong_relative_order.py @@ -1,3 +1,3 @@ # appnexus cryptography edited google smarkets from .. import A -from . import B # I100 +from . import B # I100 diff --git a/tests/test_cases/wrong_section.py b/tests/test_cases/wrong_section.py index 2e98c25..f8e06ae 100644 --- a/tests/test_cases/wrong_section.py +++ b/tests/test_cases/wrong_section.py @@ -1,8 +1,8 @@ # cryptography edited google pep8 smarkets import ast -import flake8_import_order # I100 +import flake8_import_order # I100 import A -import os # I100 +import os # I100 diff --git a/tests/test_cases/wrong_section_appnexus.py b/tests/test_cases/wrong_section_appnexus.py index f2957e4..f8c59f5 100644 --- a/tests/test_cases/wrong_section_appnexus.py +++ b/tests/test_cases/wrong_section_appnexus.py @@ -1,5 +1,5 @@ # appnexus edited import ast -import flake8_import_order # I201 I100 -import localpackage # I201 I100 -import X # I201 +import flake8_import_order # I201 I100 +import localpackage # I201 I100 +import X # I201 diff --git a/tests/test_flake8_linter.py b/tests/test_flake8_linter.py index 6269e57..4891cae 100644 --- a/tests/test_flake8_linter.py +++ b/tests/test_flake8_linter.py @@ -7,38 +7,38 @@ def test_parsing(): - style = 'google' - import_names = ['flake8_import_order', 'tests'] - package_names = ['local_package'] + style = "google" + import_names = ["flake8_import_order", "tests"] + package_names = ["local_package"] argv = [ - "--application-import-names={}".format(','.join(import_names)), + "--application-import-names={}".format(",".join(import_names)), f"--import-order-style={style}", - "--application-package-names={}".format(','.join(package_names)), + "--application-package-names={}".format(",".join(package_names)), ] - parser = pycodestyle.get_parser('', '') + parser = pycodestyle.get_parser("", "") Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) - assert Linter.options['import_order_style'].name == style - assert Linter.options['import_order_style'].load() is Google - assert Linter.options['application_import_names'] == import_names - assert Linter.options['application_package_names'] == package_names + assert Linter.options["import_order_style"].name == style + assert Linter.options["import_order_style"].load() is Google + assert Linter.options["application_import_names"] == import_names + assert Linter.options["application_package_names"] == package_names def test_linter(): - argv = ['--application-import-names=flake8_import_order'] - parser = pycodestyle.get_parser('', '') + argv = ["--application-import-names=flake8_import_order"] + parser = pycodestyle.get_parser("", "") Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) - data = 'import ast\nimport flake8_import_order\n' + data = "import ast\nimport flake8_import_order\n" pycodestyle.stdin_get_value = lambda: data tree = ast.parse(data) checker = Linter(tree, None) for lineno, col_offset, msg, instance in checker.run(): assert msg.startswith( - 'I201 Missing newline between import groups.', + "I201 Missing newline between import groups.", ) diff --git a/tests/test_pylama_linter.py b/tests/test_pylama_linter.py index 5e0a397..6cb3119 100644 --- a/tests/test_pylama_linter.py +++ b/tests/test_pylama_linter.py @@ -2,13 +2,13 @@ def test_linter(tmpdir): - file_ = tmpdir.join('flake8_import_order.py') - file_.write('import ast\nimport flake8_import_order\n') + file_ = tmpdir.join("flake8_import_order.py") + file_.write("import ast\nimport flake8_import_order\n") options = { - 'application-import-names': ['flake8_import_order'], + "application-import-names": ["flake8_import_order"], } checker = Linter() assert checker.allow(str(file_)) for error in checker.run(str(file_), **options): - assert error['type'] == 'I201' + assert error["type"] == "I201" diff --git a/tests/test_stdlib.py b/tests/test_stdlib.py index 57bf4aa..462bacf 100644 --- a/tests/test_stdlib.py +++ b/tests/test_stdlib.py @@ -24,7 +24,7 @@ def _checker(data): return checker -@pytest.mark.parametrize('import_name', _load_test_cases()) +@pytest.mark.parametrize("import_name", _load_test_cases()) def test_styles(import_name): data = f"import {import_name}\nimport zoneinfo\n" checker = _checker(data) diff --git a/tests/test_style_cases.py b/tests/test_style_cases.py index 44959d2..f2a2ebd 100644 --- a/tests/test_style_cases.py +++ b/tests/test_style_cases.py @@ -26,17 +26,17 @@ def _extract_expected_errors(data): def _load_test_cases(): base_path = os.path.dirname(__file__) test_cases = [] - test_case_path = os.path.join(base_path, 'test_cases') - wildcard_path = os.path.join(test_case_path, '*.py') + test_case_path = os.path.join(base_path, "test_cases") + wildcard_path = os.path.join(test_case_path, "*.py") for filename in glob.glob(wildcard_path): # The namespace.py test only works with Python3 - if filename.endswith('namespace.py') and sys.version_info.major < 3: + if filename.endswith("namespace.py") and sys.version_info.major < 3: continue fullpath = os.path.join(test_case_path, filename) with open(fullpath) as file_: data = file_.read() - styles = data.splitlines()[0].lstrip('#').strip().split() + styles = data.splitlines()[0].lstrip("#").strip().split() codes = _extract_expected_errors(data) tree = ast.parse(data, fullpath) for style_name in styles: @@ -48,11 +48,13 @@ def _load_test_cases(): def _checker(filename, tree, style_entry_point): options = { - 'application_import_names': [ - 'flake8_import_order', 'namespace.package_b', 'tests', + "application_import_names": [ + "flake8_import_order", + "namespace.package_b", + "tests", ], - 'application_package_names': ['localpackage'], - 'import_order_style': style_entry_point, + "application_package_names": ["localpackage"], + "import_order_style": style_entry_point, } checker = ImportOrderChecker(filename, tree) checker.options = options @@ -60,7 +62,7 @@ def _checker(filename, tree, style_entry_point): @pytest.mark.parametrize( - 'filename, tree, style, expected_codes', + "filename, tree, style, expected_codes", _load_test_cases(), ) def test_styles(filename, tree, style, expected_codes): @@ -71,4 +73,4 @@ def test_styles(filename, tree, style, expected_codes): def test_unknown_style(): with pytest.raises(LookupError): - lookup_entry_point('Unknown') + lookup_entry_point("Unknown") diff --git a/tox.ini b/tox.ini index d3bb0c2..ef2fef8 100644 --- a/tox.ini +++ b/tox.ini @@ -17,7 +17,7 @@ deps = flake8 pep8-naming flake8-import-order -commands = flake8 flake8_import_order/ tests/ +commands = flake8 --max-line-length=88 --max-complexity=10 flake8_import_order/ tests/ [testenv:py3pep8] basepython = python3.7 @@ -25,7 +25,7 @@ deps = flake8 pep8-naming flake8-import-order -commands = flake8 flake8_import_order/ tests/ +commands = flake8 --max-line-length=88 --max-complexity=10 flake8_import_order/ tests/ [testenv:setuppy] basepython = python3.7