-
-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,991 additions
and
1,240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
# https://linkchecker.github.io/linkchecker/man/linkcheckerrc.html | ||
[filtering] | ||
ignore= | ||
https://docs.astral.sh/ruff/rules/.+ | ||
|
||
[AnchorCheck] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import re | ||
from collections import defaultdict | ||
from functools import cache | ||
|
||
MARKER_VERSION = '<HATCH_RUFF_VERSION>' | ||
MARKER_SELECTED_RULES = '<HATCH_RUFF_SELECTED_RULES>' | ||
MARKER_PER_FILE_IGNORED_RULES = '<HATCH_RUFF_PER_FILE_IGNORED_RULES>' | ||
RULE_URLS = {'S': 'https://docs.astral.sh/ruff/rules/#flake8-bandit-s'} | ||
|
||
|
||
@cache | ||
def ruff_data(): | ||
generated_file = os.path.join(os.getcwd(), 'src', 'hatch', 'env', 'internal', 'fmt.py') | ||
with open(generated_file, encoding='utf-8') as f: | ||
lines = f.read().splitlines() | ||
|
||
for i, line in enumerate(lines): | ||
if line.startswith('RUFF_MINIMUM_VERSION'): | ||
block_start = i | ||
break | ||
else: | ||
message = f'Could not find RUFF_MINIMUM_VERSION in {generated_file}' | ||
raise RuntimeError(message) | ||
|
||
data = {} | ||
exec('\n'.join(lines[block_start:]), None, data) # noqa: S102 | ||
return data | ||
|
||
|
||
@cache | ||
def get_ruff_version(): | ||
return ruff_data()['RUFF_MINIMUM_VERSION'] | ||
|
||
|
||
@cache | ||
def get_selected_rules(): | ||
selected_rules = defaultdict(list) | ||
separator = re.compile(r'^(\D+)(\d+)$') | ||
|
||
data = ruff_data() | ||
for rules, preview in ((data['STABLE_RULES'], False), (data['PREVIEW_RULES'], True)): | ||
for rule in rules: | ||
match = separator.search(rule) | ||
if match is None: | ||
message = f'Could not parse rule {rule}' | ||
raise RuntimeError(message) | ||
|
||
group, number = match.groups() | ||
selected_rules[group].append((number, preview)) | ||
|
||
lines = [] | ||
for group, rule_data in sorted(selected_rules.items()): | ||
rule_data.sort(key=lambda x: int(x[0])) | ||
|
||
parts = [] | ||
for number, preview in rule_data: | ||
rule = f'{group}{number}' | ||
part = f'[{rule}](https://docs.astral.sh/ruff/rules/{rule})' | ||
if preview: | ||
part += '^P^' | ||
parts.append(part) | ||
|
||
lines.append(f'- {", ".join(parts)}') | ||
|
||
return '\n'.join(lines) | ||
|
||
|
||
@cache | ||
def get_per_file_ignored_rules(): | ||
lines = [] | ||
for glob, rules in sorted(ruff_data()['PER_FILE_IGNORED_RULES'].items()): | ||
parts = [] | ||
for rule in rules: | ||
url = RULE_URLS.get(rule) or f'https://docs.astral.sh/ruff/rules/{rule}' | ||
parts.append(f'[{rule}]({url})') | ||
|
||
lines.append(f'- `{glob}`: {", ".join(parts)}') | ||
|
||
return '\n'.join(lines) | ||
|
||
|
||
def on_page_read_source( | ||
page, | ||
config, # noqa: ARG001 | ||
): | ||
with open(page.file.abs_src_path, encoding='utf-8') as f: | ||
return ( | ||
f.read() | ||
.replace(MARKER_VERSION, get_ruff_version()) | ||
.replace(MARKER_SELECTED_RULES, get_selected_rules()) | ||
.replace(MARKER_PER_FILE_IGNORED_RULES, get_per_file_ignored_rules()) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Static analysis configuration | ||
|
||
----- | ||
|
||
Static analysis performed by the [`fmt`](../cli/reference.md#hatch-fmt) command is backed entirely by [Ruff](https://github.com/astral-sh/ruff). | ||
|
||
Hatch provides [default settings](#default-settings) that user configuration can [extend](#extending-config). | ||
|
||
## Extending config | ||
|
||
When defining your configuration, be sure to use options that are prefixed by `extend-` such as [`extend-select`](https://docs.astral.sh/ruff/settings/#extend-select), for example: | ||
|
||
=== ":octicons-file-code-16: pyproject.toml" | ||
|
||
```toml | ||
[tool.ruff.lint] | ||
preview = true | ||
extend-select = ["C901"] | ||
|
||
[tool.ruff.lint.extend-per-file-ignores] | ||
"docs/.hooks/*" = ["INP001", "T201"] | ||
|
||
[tool.ruff.lint.isort] | ||
known-first-party = ["foo", "bar"] | ||
|
||
[tool.ruff.format] | ||
preview = true | ||
quote-style = "single" | ||
``` | ||
|
||
=== ":octicons-file-code-16: ruff.toml" | ||
|
||
```toml | ||
[lint] | ||
preview = true | ||
extend-select = ["C901"] | ||
|
||
[lint.extend-per-file-ignores] | ||
"docs/.hooks/*" = ["INP001", "T201"] | ||
|
||
[lint.isort] | ||
known-first-party = ["foo", "bar"] | ||
|
||
[format] | ||
preview = true | ||
quote-style = "single" | ||
``` | ||
|
||
!!! note | ||
When not [persisting config](#persistent-config), there is no need to explicitly [extend](https://docs.astral.sh/ruff/settings/#extend) the defaults as Hatch automatically handles that. | ||
|
||
## Persistent config | ||
|
||
If you want to store the default configuration in the project, set an explicit path like so: | ||
|
||
```toml config-example | ||
[tool.hatch.format] | ||
config-path = "ruff_defaults.toml" | ||
``` | ||
|
||
Then instruct Ruff to consider your configuration as an extension of the default file: | ||
|
||
=== ":octicons-file-code-16: pyproject.toml" | ||
|
||
```toml | ||
[tool.ruff] | ||
extend = "ruff_defaults.toml" | ||
``` | ||
|
||
=== ":octicons-file-code-16: ruff.toml" | ||
|
||
```toml | ||
extend = "ruff_defaults.toml" | ||
``` | ||
|
||
Anytime you wish to update the defaults (such as when upgrading Hatch), you must run the [`fmt`](../cli/reference.md#hatch-fmt) command once with the `--sync` flag e.g.: | ||
|
||
``` | ||
hatch fmt --check --sync | ||
``` | ||
|
||
!!! tip | ||
This is the recommended approach since it allows other tools like IDEs to use the default configuration. | ||
|
||
## Default settings | ||
|
||
### Non-rule settings | ||
|
||
- [Line length](https://docs.astral.sh/ruff/settings/#line-length) set to 120 | ||
- Only absolute imports [are allowed](https://docs.astral.sh/ruff/settings/#flake8-tidy-imports-ban-relative-imports), [except for tests](#per-file-ignored-rules) | ||
- The normalized [project name](metadata.md#name) is a [known first party](https://docs.astral.sh/ruff/settings/#isort-known-first-party) import | ||
|
||
### Per-file ignored rules | ||
|
||
<HATCH_RUFF_PER_FILE_IGNORED_RULES> | ||
|
||
### Selected rules | ||
|
||
The following rules are based on version <HATCH_RUFF_VERSION> of Ruff. | ||
|
||
<HATCH_RUFF_SELECTED_RULES> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.