Skip to content

Commit

Permalink
Use ruff instead of black/flake8/isort (#65)
Browse files Browse the repository at this point in the history
And also rename `test/` to `tests/` to align with the rest of RAPIDS.

Issue: rapidsai/build-planning#130
  • Loading branch information
KyleFromNVIDIA authored Jan 31, 2025
1 parent b012497 commit 0e45936
Show file tree
Hide file tree
Showing 27 changed files with 557 additions and 251 deletions.
23 changes: 7 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
# Copyright (c) 2023-2025, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,30 +32,21 @@ repos:
- id: pyupgrade
args:
- --py310-plus
- repo: https://github.com/PyCQA/isort
rev: '5.13.2'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.3
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: '24.8.0'
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: '7.1.1'
hooks:
- id: flake8
args:
- --show-source
- id: ruff
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.11.2'
hooks:
- id: mypy
args:
- --config-file=pyproject.toml
- --exclude
- '^test/examples/'
- '^tests/examples/'
- src/
- test/
- tests/
pass_filenames: false
additional_dependencies:
- --extra-index-url=https://pypi.anaconda.org/rapidsai-wheels-nightly/simple
Expand Down
16 changes: 13 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,22 @@ verify-pyproject-license = "rapids_pre_commit_hooks.pyproject_license:main"
[tool.setuptools]
packages = { "find" = { where = ["src"] } }

[tool.isort]
profile = "black"
[tool.ruff]
line-length = 79

[tool.ruff.lint]
select = [
# flake8-unused-args
"ARG",
# pycodestyle
"E",
# pyflakes
"F",
]

[tool.pytest.ini_options]
testpaths = [
"test",
"tests",
]

[[tool.mypy.overrides]]
Expand Down
74 changes: 56 additions & 18 deletions src/rapids_pre_commit_hooks/alpha_spec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
# Copyright (c) 2024-2025, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -117,9 +117,13 @@ def create_specifier_string(specifiers: set[str]) -> str:
strip_cuda_suffix(args, req.name)
in get_rapids_version(args).prerelease_packages
):
descend, anchor = check_and_mark_anchor(anchors, used_anchors, node)
descend, anchor = check_and_mark_anchor(
anchors, used_anchors, node
)
if descend:
has_alpha_spec = any(str(s) == ALPHA_SPECIFIER for s in req.specifier)
has_alpha_spec = any(
str(s) == ALPHA_SPECIFIER for s in req.specifier
)
if args.mode == "development" and not has_alpha_spec:
linter.add_warning(
(node.start_mark.index, node.end_mark.index),
Expand All @@ -130,7 +134,8 @@ def create_specifier_string(specifiers: set[str]) -> str:
(f"&{anchor} " if anchor else "")
+ req.name
+ create_specifier_string(
{str(s) for s in req.specifier} | {ALPHA_SPECIFIER},
{str(s) for s in req.specifier}
| {ALPHA_SPECIFIER},
)
),
)
Expand All @@ -144,7 +149,8 @@ def create_specifier_string(specifiers: set[str]) -> str:
(f"&{anchor} " if anchor else "")
+ req.name
+ create_specifier_string(
{str(s) for s in req.specifier} - {ALPHA_SPECIFIER},
{str(s) for s in req.specifier}
- {ALPHA_SPECIFIER},
)
),
)
Expand All @@ -161,7 +167,9 @@ def check_packages(
descend, _ = check_and_mark_anchor(anchors, used_anchors, node)
if descend:
for package_spec in node.value:
check_package_spec(linter, args, anchors, used_anchors, package_spec)
check_package_spec(
linter, args, anchors, used_anchors, package_spec
)


def check_common(
Expand All @@ -174,13 +182,20 @@ def check_common(
if node_has_type(node, "seq"):
for dependency_set in node.value:
if node_has_type(dependency_set, "map"):
for dependency_set_key, dependency_set_value in dependency_set.value:
for (
dependency_set_key,
dependency_set_value,
) in dependency_set.value:
if (
node_has_type(dependency_set_key, "str")
and dependency_set_key.value == "packages"
):
check_packages(
linter, args, anchors, used_anchors, dependency_set_value
linter,
args,
anchors,
used_anchors,
dependency_set_value,
)


Expand Down Expand Up @@ -214,13 +229,20 @@ def check_specific(
if node_has_type(node, "seq"):
for matrix_matcher in node.value:
if node_has_type(matrix_matcher, "map"):
for matrix_matcher_key, matrix_matcher_value in matrix_matcher.value:
for (
matrix_matcher_key,
matrix_matcher_value,
) in matrix_matcher.value:
if (
node_has_type(matrix_matcher_key, "str")
and matrix_matcher_key.value == "matrices"
):
check_matrices(
linter, args, anchors, used_anchors, matrix_matcher_value
linter,
args,
anchors,
used_anchors,
matrix_matcher_value,
)


Expand All @@ -234,15 +256,26 @@ def check_dependencies(
if node_has_type(node, "map"):
for _, dependencies_value in node.value:
if node_has_type(dependencies_value, "map"):
for dependency_key, dependency_value in dependencies_value.value:
for (
dependency_key,
dependency_value,
) in dependencies_value.value:
if node_has_type(dependency_key, "str"):
if dependency_key.value == "common":
check_common(
linter, args, anchors, used_anchors, dependency_value
linter,
args,
anchors,
used_anchors,
dependency_value,
)
elif dependency_key.value == "specific":
check_specific(
linter, args, anchors, used_anchors, dependency_value
linter,
args,
anchors,
used_anchors,
dependency_value,
)


Expand All @@ -255,14 +288,19 @@ def check_root(
) -> None:
if node_has_type(node, "map"):
for root_key, root_value in node.value:
if node_has_type(root_key, "str") and root_key.value == "dependencies":
check_dependencies(linter, args, anchors, used_anchors, root_value)
if (
node_has_type(root_key, "str")
and root_key.value == "dependencies"
):
check_dependencies(
linter, args, anchors, used_anchors, root_value
)


class AnchorPreservingLoader(yaml.SafeLoader):
"""A SafeLoader that preserves the anchors for later reference. The anchors can
be found in the document_anchors member, which is a list of dictionaries, one
dictionary for each parsed document.
"""A SafeLoader that preserves the anchors for later reference. The anchors
can be found in the document_anchors member, which is a list of
dictionaries, one dictionary for each parsed document.
"""

def __init__(self, stream) -> None:
Expand Down
44 changes: 30 additions & 14 deletions src/rapids_pre_commit_hooks/codeowners.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,22 @@ class RequiredCodeownersLine:


def hard_coded_codeowners(owners: str) -> CodeownersTransform:
return lambda *, project_prefix: owners
return lambda *, project_prefix: owners # noqa: ARG005


def project_codeowners(category: str) -> CodeownersTransform:
return lambda *, project_prefix: f"@rapidsai/{project_prefix}-{category}-codeowners"
return (
lambda *,
project_prefix: f"@rapidsai/{project_prefix}-{category}-codeowners"
)


def required_codeowners_list(
files: list[str], owners: list[CodeownersTransform], after: list[str] = []
) -> list[RequiredCodeownersLine]:
return [
RequiredCodeownersLine(file=file, owners=owners, after=after) for file in files
RequiredCodeownersLine(file=file, owners=owners, after=after)
for file in files
]


Expand Down Expand Up @@ -124,7 +128,9 @@ def required_codeowners_list(
)


def required_codeowners_lines(args: argparse.Namespace) -> list[RequiredCodeownersLine]:
def required_codeowners_lines(
args: argparse.Namespace,
) -> list[RequiredCodeownersLine]:
return [
*(REQUIRED_CI_CODEOWNERS_LINES if args.ci else []),
*(REQUIRED_PACKAGING_CODEOWNERS_LINES if args.packaging else []),
Expand All @@ -141,12 +147,17 @@ def parse_codeowners_line(line: str, skip: int) -> CodeownersLine | None:

file_pattern = FilePattern(
filename=line_match.group("file"),
pos=(line_match.span("file")[0] + skip, line_match.span("file")[1] + skip),
pos=(
line_match.span("file")[0] + skip,
line_match.span("file")[1] + skip,
),
)
owners: list[Owner] = []

line_skip = skip + len(line_match.group("file"))
for owner_match in CODEOWNERS_OWNER_RE.finditer(line_match.group("owners")):
for owner_match in CODEOWNERS_OWNER_RE.finditer(
line_match.group("owners")
):
start, end = owner_match.span("owner")
whitespace_start, _ = owner_match.span()
owners.append(
Expand Down Expand Up @@ -187,11 +198,13 @@ def check_codeowners_line(
if extraneous_owners:
warning = linter.add_warning(
codeowners_line.file.pos,
f"file '{codeowners_line.file.filename}' has incorrect "
"owners",
f"file '{codeowners_line.file.filename}' has "
"incorrect owners",
)
for owner in extraneous_owners:
warning.add_replacement(owner.pos_with_leading_whitespace, "")
warning.add_replacement(
owner.pos_with_leading_whitespace, ""
)

missing_required_owners: list[str] = []
for required_owner in required_owners:
Expand All @@ -204,7 +217,8 @@ def check_codeowners_line(
if not warning:
warning = linter.add_warning(
codeowners_line.file.pos,
f"file '{codeowners_line.file.filename}' has incorrect owners",
f"file '{codeowners_line.file.filename}' has "
"incorrect owners",
)
extra_string = " " + " ".join(missing_required_owners)
last = codeowners_line.owners[-1].pos[1]
Expand All @@ -221,7 +235,9 @@ def check_codeowners_line(
f"file '{codeowners_line.file.filename}' is here",
)

found_files.append((required_codeowners_line, codeowners_line.file.pos))
found_files.append(
(required_codeowners_line, codeowners_line.file.pos)
)
break


Expand All @@ -247,9 +263,9 @@ def check_codeowners(linter: Linter, args: argparse.Namespace) -> None:
if linter.content and not linter.content.endswith("\n"):
new_text = f"\n{new_text}"
content_len = len(linter.content)
linter.add_warning((0, 0), "missing required codeowners").add_replacement(
(content_len, content_len), new_text
)
linter.add_warning(
(0, 0), "missing required codeowners"
).add_replacement((content_len, content_len), new_text)


def main() -> None:
Expand Down
Loading

0 comments on commit 0e45936

Please sign in to comment.