Skip to content

Commit

Permalink
upgrade pyupgrade target version to Python 3.10, upgrade pre-commit h…
Browse files Browse the repository at this point in the history
…ooks
  • Loading branch information
jameslamb committed Aug 28, 2024
1 parent b078453 commit 99fed1b
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 137 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 'v4.4.0'
rev: 'v4.6.0'
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
Expand All @@ -27,27 +27,27 @@ repos:
- id: mixed-line-ending
args: [--fix=lf]
- repo: https://github.com/asottile/pyupgrade
rev: 'v3.3.1'
rev: 'v3.17.0'
hooks:
- id: pyupgrade
args:
- --py38-plus
- --py310-plus
- repo: https://github.com/PyCQA/isort
rev: '5.12.0'
rev: '5.13.2'
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: '23.1.0'
rev: '24.8.0'
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: '6.0.0'
rev: '7.1.1'
hooks:
- id: flake8
args:
- --show-source
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.10.0'
rev: 'v1.11.2'
hooks:
- id: mypy
args:
Expand Down
3 changes: 1 addition & 2 deletions src/rapids_pre_commit_hooks/alpha_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import os
import re
from functools import cache, total_ordering
from typing import Optional

import yaml
from packaging.requirements import InvalidRequirement, Requirement
Expand Down Expand Up @@ -63,7 +62,7 @@ def strip_cuda_suffix(args: argparse.Namespace, name: str) -> str:

def check_and_mark_anchor(
anchors: dict[str, "yaml.Node"], used_anchors: set[str], node: "yaml.Node"
) -> tuple[bool, Optional[str]]:
) -> tuple[bool, str | None]:
for key, value in anchors.items():
if value == node:
anchor = key
Expand Down
26 changes: 13 additions & 13 deletions src/rapids_pre_commit_hooks/copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import re
import warnings
from collections.abc import Callable
from typing import Optional, Union
from typing import Optional

import git

Expand Down Expand Up @@ -63,7 +63,7 @@ def add_copy_rename_note(
linter: Linter,
warning: LintWarning,
change_type: str,
old_filename: Optional[Union[str, os.PathLike[str]]],
old_filename: str | os.PathLike[str] | None,
):
CHANGE_VERBS = {
"C": "copied",
Expand All @@ -89,7 +89,7 @@ def add_copy_rename_note(
def apply_copyright_revert(
linter: Linter,
change_type: str,
old_filename: Optional[Union[str, os.PathLike[str]]],
old_filename: str | os.PathLike[str] | None,
old_match: re.Match,
new_match: re.Match,
) -> None:
Expand Down Expand Up @@ -123,8 +123,8 @@ def apply_copyright_update(
def apply_copyright_check(
linter: Linter,
change_type: str,
old_filename: Optional[Union[str, os.PathLike[str]]],
old_content: Optional[str],
old_filename: str | os.PathLike[str] | None,
old_content: str | None,
) -> None:
if linter.content != old_content:
current_year = datetime.datetime.now().year
Expand Down Expand Up @@ -154,7 +154,7 @@ def apply_copyright_check(
linter.add_warning((0, 0), "no copyright notice found")


def get_target_branch(repo: "git.Repo", args: argparse.Namespace) -> Optional[str]:
def get_target_branch(repo: "git.Repo", args: argparse.Namespace) -> str | None:
"""Determine which branch is the "target" branch.
The target branch is determined in the following order:
Expand Down Expand Up @@ -222,7 +222,7 @@ def get_target_branch(repo: "git.Repo", args: argparse.Namespace) -> Optional[st

def get_target_branch_upstream_commit(
repo: "git.Repo", args: argparse.Namespace
) -> Optional[git.Commit]:
) -> git.Commit | None:
# If no target branch can be determined, use HEAD if it exists
target_branch_name = get_target_branch(repo, args)
if target_branch_name is None:
Expand Down Expand Up @@ -278,7 +278,7 @@ def try_get_ref(remote: "git.Remote") -> Optional["git.Reference"]:

def get_changed_files(
args: argparse.Namespace,
) -> dict[Union[str, os.PathLike[str]], tuple[str, Optional["git.Blob"]]]:
) -> dict[str | os.PathLike[str], tuple[str, Optional["git.Blob"]]]:
try:
repo = git.Repo()
except git.InvalidGitRepositoryError:
Expand All @@ -288,9 +288,9 @@ def get_changed_files(
for filename in filenames
}

changed_files: dict[
Union[str, os.PathLike[str]], tuple[str, Optional["git.Blob"]]
] = {f: ("A", None) for f in repo.untracked_files}
changed_files: dict[str | os.PathLike[str], tuple[str, Optional["git.Blob"]]] = {
f: ("A", None) for f in repo.untracked_files
}
target_branch_upstream_commit = get_target_branch_upstream_commit(repo, args)
if target_branch_upstream_commit is None:
changed_files.update(
Expand All @@ -316,15 +316,15 @@ def get_changed_files(
return changed_files


def normalize_git_filename(filename: Union[str, os.PathLike[str]]) -> Optional[str]:
def normalize_git_filename(filename: str | os.PathLike[str]) -> str | None:
relpath = os.path.relpath(filename)
if re.search(r"^\.\.(/|$)", relpath):
return None
return relpath


def find_blob(
tree: "git.Tree", filename: Union[str, os.PathLike[str]]
tree: "git.Tree", filename: str | os.PathLike[str]
) -> Optional["git.Blob"]:
d1, d2 = os.path.split(filename)
split = [d2]
Expand Down
5 changes: 2 additions & 3 deletions src/rapids_pre_commit_hooks/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import warnings
from collections.abc import Callable
from itertools import pairwise
from typing import Optional

from rich.console import Console
from rich.markup import escape
Expand Down Expand Up @@ -105,7 +104,7 @@ def fix(self) -> str:
return replaced_content

def _print_note(
self, note_type: str, pos: _PosType, msg: str, newtext: Optional[str] = None
self, note_type: str, pos: _PosType, msg: str, newtext: str | None = None
) -> None:
line_index = self._line_for_pos(pos[0])
line_pos = self.lines[line_index]
Expand Down Expand Up @@ -156,7 +155,7 @@ def print_warnings(self, fix_applied: bool = False) -> None:
self._print_note("note", replacement.pos, replacement_msg, newtext)

def _print_highlighted_code(
self, pos: _PosType, replacement: Optional[str] = None
self, pos: _PosType, replacement: str | None = None
) -> None:
line_index = self._line_for_pos(pos[0])
line_pos = self.lines[line_index]
Expand Down
35 changes: 22 additions & 13 deletions test/rapids_pre_commit_hooks/test_alpha_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ def test_get_rapids_version(
),
},
)
with set_cwd(tmp_path), patch(
"rapids_pre_commit_hooks.alpha_spec.all_metadata",
Mock(return_value=MOCK_METADATA),
with (
set_cwd(tmp_path),
patch(
"rapids_pre_commit_hooks.alpha_spec.all_metadata",
Mock(return_value=MOCK_METADATA),
),
):
if version_file:
with open("VERSION", "w") as f:
Expand Down Expand Up @@ -526,11 +529,14 @@ def test_check_dependencies(
common_indices,
specific_indices,
):
with patch(
"rapids_pre_commit_hooks.alpha_spec.check_common", Mock()
) as mock_check_common, patch(
"rapids_pre_commit_hooks.alpha_spec.check_specific", Mock()
) as mock_check_specific:
with (
patch(
"rapids_pre_commit_hooks.alpha_spec.check_common", Mock()
) as mock_check_common,
patch(
"rapids_pre_commit_hooks.alpha_spec.check_specific", Mock()
) as mock_check_specific,
):
args = Mock()
linter = lint.Linter("dependencies.yaml", content)
anchors = Mock()
Expand Down Expand Up @@ -579,11 +585,14 @@ def test_check_root(content, indices):

def test_check_alpha_spec():
CONTENT = "dependencies: []"
with patch(
"rapids_pre_commit_hooks.alpha_spec.check_root", Mock()
) as mock_check_root, patch(
"rapids_pre_commit_hooks.alpha_spec.AnchorPreservingLoader", MagicMock()
) as mock_anchor_preserving_loader:
with (
patch(
"rapids_pre_commit_hooks.alpha_spec.check_root", Mock()
) as mock_check_root,
patch(
"rapids_pre_commit_hooks.alpha_spec.AnchorPreservingLoader", MagicMock()
) as mock_anchor_preserving_loader,
):
args = Mock()
linter = lint.Linter("dependencies.yaml", CONTENT)
alpha_spec.check_alpha_spec(linter, args)
Expand Down
46 changes: 29 additions & 17 deletions test/rapids_pre_commit_hooks/test_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,9 +888,11 @@ def mock_os_walk(top):
Mock(
return_value=(
(
"."
if (rel := os.path.relpath(dirpath, top)) == "."
else os.path.join(".", rel),
(
"."
if (rel := os.path.relpath(dirpath, top)) == "."
else os.path.join(".", rel)
),
dirnames,
filenames,
)
Expand All @@ -899,9 +901,11 @@ def mock_os_walk(top):
),
)

with tempfile.TemporaryDirectory() as non_git_dir, patch(
"os.getcwd", Mock(return_value=non_git_dir)
), mock_os_walk(non_git_dir):
with (
tempfile.TemporaryDirectory() as non_git_dir,
patch("os.getcwd", Mock(return_value=non_git_dir)),
mock_os_walk(non_git_dir),
):
with open(os.path.join(non_git_dir, "top.txt"), "w") as f:
f.write("Top file\n")
os.mkdir(os.path.join(non_git_dir, "subdir1"))
Expand Down Expand Up @@ -947,11 +951,13 @@ def file_contents(verbed):
]
)

with patch("os.getcwd", Mock(return_value=git_repo.working_tree_dir)), mock_os_walk(
git_repo.working_tree_dir
), patch(
"rapids_pre_commit_hooks.copyright.get_target_branch_upstream_commit",
Mock(return_value=None),
with (
patch("os.getcwd", Mock(return_value=git_repo.working_tree_dir)),
mock_os_walk(git_repo.working_tree_dir),
patch(
"rapids_pre_commit_hooks.copyright.get_target_branch_upstream_commit",
Mock(return_value=None),
),
):
assert copyright.get_changed_files(Mock()) == {
"untouched.txt": ("A", None),
Expand Down Expand Up @@ -1043,9 +1049,12 @@ def file_contents(verbed):
"renamed_2.txt": ("R", "renamed.txt"),
}

with patch("os.getcwd", Mock(return_value=git_repo.working_tree_dir)), patch(
"rapids_pre_commit_hooks.copyright.get_target_branch_upstream_commit",
Mock(return_value=target_branch.commit),
with (
patch("os.getcwd", Mock(return_value=git_repo.working_tree_dir)),
patch(
"rapids_pre_commit_hooks.copyright.get_target_branch_upstream_commit",
Mock(return_value=target_branch.commit),
),
):
changed_files = copyright.get_changed_files(Mock())
assert {
Expand Down Expand Up @@ -1134,9 +1143,12 @@ def write_file(filename, contents):
commit_date=datetime.datetime(2024, 5, 1, tzinfo=datetime.timezone.utc),
)

with patch("os.getcwd", Mock(return_value=git_repo.working_tree_dir)), patch(
"rapids_pre_commit_hooks.copyright.get_target_branch",
Mock(return_value="branch-1-2"),
with (
patch("os.getcwd", Mock(return_value=git_repo.working_tree_dir)),
patch(
"rapids_pre_commit_hooks.copyright.get_target_branch",
Mock(return_value="branch-1-2"),
),
):
changed_files = copyright.get_changed_files(Mock())
assert {
Expand Down
Loading

0 comments on commit 99fed1b

Please sign in to comment.