Sean
Sean

Reputation: 3450

Running git commit is not properly running pre-commit hooks

I'm currently using Ruff and am using pre-commit hooks to run it with configuration info in .pre-commit-config.yaml and .ruff.toml. When I run pre-commit run --all-files the rules like isort run fine, but when I do git commit file they don't run.

I'm currently using pre-commit installed with Poetry, and am doing poetry install --with dev to install pre-commit.

.pre-commit-config.yaml:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
  # Ruff version.
  rev: v0.5.0
  hooks:
    # Run the linter.
    - id: ruff
      args: [ --fix ]
    # Run the formatter.
    - id: ruff-format

.ruff.toml:

# Exclude a variety of commonly ignored directories.
exclude = [
    ".bzr",
    ".direnv",
    ".eggs",
    ".git",
    ".git-rewrite",
    ".hg",
    ".ipynb_checkpoints",
    ".mypy_cache",
    ".nox",
    ".pants.d",
    ".pyenv",
    ".pytest_cache",
    ".pytype",
    ".ruff_cache",
    ".svn",
    ".tox",
    ".venv",
    ".vscode",
    "__pypackages__",
    "_build",
    "buck-out",
    "build",
    "dist",
    "node_modules",
    "site-packages",
    "venv",
]

# Same as Black.
line-length = 120
indent-width = 4

# Assume Python 3.8
target-version = "py310"

[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`)  codes by default.
select = ["E4", "E7", "E9", "F"]
ignore = []
extend-select = ["I"]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

Upvotes: -1

Views: 936

Answers (1)

Sean
Sean

Reputation: 3450

It seems that I didn't understand how rule selection works in Ruff. Judging from this discussion on their GitHub (https://github.com/astral-sh/ruff/discussions/10753) following the code as follows made Ruff work properly with pre-commit:

# Before
[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`)  codes by default.
select = ["E4", "E7", "E9", "F"]
ignore = []
extend-select = ["I"]

# After
[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`)  codes by default.
select = ["E4", "E7", "E9", "F", "I"]

Upvotes: -1

Related Questions