Skip to content

Commit

Permalink
feat: infer ruff target version (#205)
Browse files Browse the repository at this point in the history
* feat: allow automatic inference of ruff version

Signed-off-by: burgholzer <[email protected]>

* chore: update template for automatic ruff target-version inference

Signed-off-by: burgholzer <[email protected]>

* chore: update project for automatic ruff target-version inference

Signed-off-by: burgholzer <[email protected]>

* fix: include target-version where requires-python is not specified in pyproject.toml

Signed-off-by: burgholzer <[email protected]>

* docs: update guidelines too

Signed-off-by: Henry Schreiner <[email protected]>

---------

Signed-off-by: burgholzer <[email protected]>
Signed-off-by: Henry Schreiner <[email protected]>
Co-authored-by: Henry Schreiner <[email protected]>
  • Loading branch information
burgholzer and henryiii authored Jul 3, 2023
1 parent 7116d0e commit 282458f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
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

0 comments on commit 282458f

Please sign in to comment.