Skip to content

Commit

Permalink
pre-commit: Add psf/black
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Dec 2, 2022
1 parent 2420f82 commit 6581a04
Show file tree
Hide file tree
Showing 28 changed files with 229 additions and 176 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 13 additions & 8 deletions flake8_import_order/__about__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
__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"

__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__
48 changes: 34 additions & 14 deletions flake8_import_order/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions flake8_import_order/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand All @@ -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<codes>([A-Z][0-9]+(?:[,\s]+)?)+))?',
re.IGNORECASE
r"# noqa(?:: (?P<codes>([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:
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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

Expand Down
22 changes: 13 additions & 9 deletions flake8_import_order/flake8_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
)

Expand Down Expand Up @@ -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("-"))
17 changes: 8 additions & 9 deletions flake8_import_order/pylama_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Loading

0 comments on commit 6581a04

Please sign in to comment.