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

feat: infer ruff target version #205

Merged
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
32 changes: 21 additions & 11 deletions docs/pages/guides/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ extend-ignore = [
"E501", # Line too long
"PT004", # Use underscore for non-returning fixture (use usefixture instead)
]
target-version = "py38"
typing-modules = ["mypackage._compat.typing"]
src = ["src"]
unfixable = [
Expand All @@ -229,16 +228,27 @@ ignore certain error codes via `extend-ignore`. You can also set codes per paths
to ignore in `per-file-ignores`. If you don't like certain auto-fixes, you can
disable auto-fixing for specific error codes via `unfixable`.

There are other configuration options, such as `target-version`, which selects
the minimum version you want to target (primarily for `"UP"` and `"I"`)
{% rr RF002 %}, the `src` list which tells it where to look for top level
packages (mostly for "I" codes, which also have a lot of custom configuration
options) {% rr RF003 %}, `typing-modules`, which helps apply typing-specific
rules to a re-exported typing module (a common practice for unifying typing and
`typing_extensions` based on Python version). There's also a file `exclude` set,
which you can override if you are running this entirely from pre-commit (default
excludes include "build", so if you have a `build` module or file named
`build.py`, it would get skipped by default without this).
There are other configuration options, such as the `src` list which tells it
where to look for top level packages (mostly for "I" codes, which also have a
lot of custom configuration options) {% rr RF003 %}, `typing-modules`, which
helps apply typing-specific rules to a re-exported typing module (a common
practice for unifying typing and `typing_extensions` based on Python version).
There's also a file `exclude` set, which you can override if you are running
this entirely from pre-commit (default excludes include "build", so if you have
a `build` module or file named `build.py`, it would get skipped by default
without this).

{: .warning }

> If you don't use a `[project]` table (older setuptools or Poetry), then you
> should also set:
>
> ```toml
> target-version = "py38"
> ```
>
> This selects the minimum version you want to target (primarily for `"UP"` and
> `"I"`) {% rr RF002 %},

Here are some good error codes to enable on most (but not all!) projects:

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ extend-ignore = [
"E501", # Line too long
"PT004", # Incorrect check, usefixtures is the correct way to do this
]
target-version = "py310"
src = ["src"]
unfixable = [
"T20", # Removes print statements
Expand Down
4 changes: 3 additions & 1 deletion src/sp_repo_review/checks/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ class RF002(Ruff):
def check(pyproject: dict[str, Any]) -> bool:
"""
Must select a minimum version to target. Affects pyupgrade,
isort, and others.
isort, and others. Can be inferred from `project.requires-python`.
"""

match pyproject:
case {"tool": {"ruff": {"target-version": str()}}}:
return True
case {"project": {"requires-python": str()}}:
return True
case _:
return False

Expand Down
2 changes: 2 additions & 0 deletions {{cookiecutter.project_name}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ extend-ignore = [
"PLR", # Design related pylint codes
"E501", # Line too long
]
{%- if cookiecutter.backend in ["setuptools", "pybind11", "poetry"] %}
target-version = "py38"
{%- endif %}
typing-modules = ["{{ cookiecutter.__project_slug }}._compat.typing"]
src = ["src"]
unfixable = [
Expand Down