Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-commit: Add psf/black #196

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@
# 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
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/psf/black
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:
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------
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__ = "[email protected]"

__maintainer__ = 'Phil Jones'
__maintainer_email__ = '[email protected]'
__maintainer__ = "Phil Jones"
__maintainer_email__ = "[email protected]"

__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
14 changes: 7 additions & 7 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 @@ -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,
)
Expand Down Expand Up @@ -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("-"))
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
Loading