diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1d49968..cdbf941 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ # Learn more about this config here: https://pre-commit.com/ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-yaml - id: detect-private-key @@ -14,19 +14,19 @@ repos: - id: trailing-whitespace - repo: https://github.com/psf/black - rev: 22.10.0 + rev: 24.1.1 hooks: - id: black # See pyproject.toml for args - repo: https://github.com/codespell-project/codespell - rev: v2.2.2 + rev: v2.2.6 hooks: - id: codespell additional_dependencies: - tomli - repo: https://github.com/asottile/pyupgrade - rev: v3.2.3 + rev: v3.15.0 hooks: - id: pyupgrade args: diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8e012eb..2d2271e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -40,7 +40,7 @@ custom styles (that use the nodes directly). This also drops the asttokens dependency as it is no longer required. * Understand the existence of namespaced packages, thereby allowing - different namespaced pacakages to be defined as local or third party. + different namespaced packages to be defined as local or third party. 0.15 2017-11-06 --------------- diff --git a/flake8_import_order/flake8_linter.py b/flake8_import_order/flake8_linter.py index 4d7453b..79623c8 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 @@ -48,8 +49,7 @@ def add_options(cls, parser): action="store", type=str, help=( - "Style to follow. Available: " - ", ".join(cls.list_available_styles()) + "Style to follow. Available: " ", ".join(cls.list_available_styles()) ), parse_from_config=True, ) @@ -99,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/styles.py b/flake8_import_order/styles.py index ff13cee..623a732 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: @@ -46,17 +46,17 @@ 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), ) @@ -73,11 +73,12 @@ def _check_I100(self, previous_import, current_import): # noqa: N802 self._explain_import(previous_import), ) same_section = self.same_section( - previous_import, current_import, + previous_import, + current_import, ) if not same_section: message = f"{message} and in a different group." - yield Error(current_import.lineno, 'I100', message) + 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) @@ -85,10 +86,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, ) ), ) @@ -99,10 +101,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, ) ), ) @@ -118,34 +121,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("_", " "), ) @@ -201,10 +200,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, ) ), ) @@ -222,7 +222,11 @@ def sorted_names(names): @staticmethod def import_key(import_): return ( - import_.type, import_.is_from, import_.level, import_.modules, import_.names + import_.type, + import_.is_from, + import_.level, + import_.modules, + import_.names, ) @@ -248,26 +252,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 )