Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ Will not show up if using lefthook instead of pre-commit/prek.
- [`PC160`](https://learn.scientific-python.org/development/guides/style#PC160): Uses a spell checker
- [`PC170`](https://learn.scientific-python.org/development/guides/style#PC170): Uses PyGrep hooks (only needed if rST present)
- [`PC180`](https://learn.scientific-python.org/development/guides/style#PC180): Uses a markdown formatter
- [`PC190`](https://learn.scientific-python.org/development/guides/style#PC190): Uses Ruff
- [`PC190`](https://learn.scientific-python.org/development/guides/style#PC190): Uses a linter (Ruff/Flake8)
- [`PC191`](https://learn.scientific-python.org/development/guides/style#PC191): Ruff show fixes if fixes enabled
- [`PC192`](https://learn.scientific-python.org/development/guides/style#PC192): Ruff uses `ruff-check` instead of `ruff` (legacy)
- [`PC901`](https://learn.scientific-python.org/development/guides/style#PC901): Custom pre-commit CI update message
Expand Down
20 changes: 12 additions & 8 deletions src/sp_repo_review/checks/precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PreCommit:
url = mk_url("style")
renamed: ClassVar[dict[str, str]] = {}
repos: ClassVar[set[str]] = set()
ids: ClassVar[dict[str, str]] = {}
ids: ClassVar[dict[str, set[str]]] = {}

@property
def describe(self) -> str:
Expand All @@ -53,7 +53,7 @@ def check(cls, precommit: dict[str, Any]) -> bool | None | str:
if repo in cls.repos:
if cls.ids and repo in cls.ids:
if any(
hook.get("id", "") == cls.ids[repo]
hook.get("id", "") in cls.ids[repo]
for hook in repo_item.get("hooks", {})
):
return True
Expand Down Expand Up @@ -83,7 +83,7 @@ class PC110(PreCommit):
renamed = {
"https://github.com/psf/black": "https://github.com/psf/black-pre-commit-mirror"
}
ids = {"https://github.com/astral-sh/ruff-pre-commit": "ruff-format"}
ids = {"https://github.com/astral-sh/ruff-pre-commit": {"ruff-format"}}


class PC111(PreCommit):
Expand All @@ -97,12 +97,17 @@ class PC111(PreCommit):


class PC190(PreCommit):
"Uses Ruff"
"Uses a linter (Ruff/Flake8)"

repos = {"https://github.com/astral-sh/ruff-pre-commit"}
repos = {
"https://github.com/astral-sh/ruff-pre-commit",
"https://github.com/pycqa/flake8",
}
renamed = {
"https://github.com/charliermarsh/ruff-pre-commit": "https://github.com/astral-sh/ruff-pre-commit"
"https://github.com/charliermarsh/ruff-pre-commit": "https://github.com/astral-sh/ruff-pre-commit",
"https://gitlab.com/pycqa/flake8 ": "https://github.com/pycqa/flake8",
}
ids = {"https://github.com/astral-sh/ruff-pre-commit": {"ruff-check", "ruff"}}


class PC140(PreCommit):
Expand Down Expand Up @@ -168,8 +173,7 @@ def check( # type: ignore[override]
return "--show-fixes" in hook["args"] or (
ruff is not None and "show-fixes" in ruff
)
return None
return False
return None


class PC192(PreCommit):
Expand Down
45 changes: 43 additions & 2 deletions tests/test_precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,56 @@ def test_pc111_rename():
assert "adamchainz" in res.err_msg


def test_pc190():
def test_pc190_ruff_check():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
hooks:
- id: ruff-check
""")
assert compute_check("PC190", precommit=precommit).result


def test_pc190_ruff():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
hooks:
- id: ruff
""")
assert compute_check("PC190", precommit=precommit).result


def test_pc190_flake8():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/pycqa/flake8
""")
assert compute_check("PC190", precommit=precommit).result


def test_pc190_rename():
def test_pc190_rename_ruff():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
hooks:
- id: ruff-check
""")
res = compute_check("PC190", precommit=precommit)
assert not res.result
assert "astral-sh" in res.err_msg


def test_pc190_rename_flake8():
precommit = yaml.safe_load("""
repos:
- repo: https://gitlab.com/pycqa/flake8
""")
res = compute_check("PC190", precommit=precommit)
assert not res.result
assert "github" in res.err_msg


def test_pc140():
precommit = yaml.safe_load("""
repos:
Expand Down Expand Up @@ -214,6 +246,15 @@ def test_pc191_no_show_fixes(ruff_check: str, ruffconfig):
assert "--show-fixes" in res.err_msg


def test_pc191_no_ruff():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/pycqa/flake8
""")
res = compute_check("PC191", precommit=precommit, ruff={})
assert res.result is None


def test_pc192():
precommit = yaml.safe_load("""
repos:
Expand Down
Loading