Skip to content

Commit a18b4d8

Browse files
committed
Merge branch 'main' into implement-flake8-copyright
2 parents 0bd37ca + c2a3e97 commit a18b4d8

File tree

1,198 files changed

+48747
-27191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,198 files changed

+48747
-27191
lines changed

.cargo/config.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[alias]
22
dev = "run --package ruff_dev --bin ruff_dev"
3-
benchmark = "bench -p ruff_benchmark --"
3+
benchmark = "bench -p ruff_benchmark --bench linter --bench formatter --"
44

55
[target.'cfg(all())']
66
rustflags = [

.devcontainer/devcontainer.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/rust
3+
{
4+
"name": "Ruff",
5+
"image": "mcr.microsoft.com/devcontainers/rust:0-1-bullseye",
6+
"mounts": [
7+
{
8+
"source": "devcontainer-cargo-cache-${devcontainerId}",
9+
"target": "/usr/local/cargo",
10+
"type": "volume"
11+
}
12+
],
13+
"customizations": {
14+
"codespaces": {
15+
"openFiles": [
16+
"CONTRIBUTING.md"
17+
]
18+
},
19+
"vscode": {
20+
"extensions": [
21+
"ms-python.python",
22+
"rust-lang.rust-analyzer",
23+
"serayuzgur.crates",
24+
"tamasfe.even-better-toml",
25+
"Swellaby.vscode-rust-test-adapter",
26+
"charliermarsh.ruff"
27+
],
28+
"settings": {
29+
"rust-analyzer.updates.askBeforeDownload": false
30+
}
31+
}
32+
},
33+
// Features to add to the dev container. More info: https://containers.dev/features.
34+
"features": {
35+
"ghcr.io/devcontainers/features/python": {
36+
"installTools": false
37+
}
38+
},
39+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
40+
// "forwardPorts": [],
41+
"postCreateCommand": ".devcontainer/post-create.sh"
42+
// Configure tool-specific properties.
43+
// "customizations": {},
44+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
45+
// "remoteUser": "root"
46+
}

.devcontainer/post-create.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
rustup default < rust-toolchain
4+
rustup component add clippy rustfmt
5+
cargo install cargo-insta
6+
cargo fetch
7+
8+
pip install maturin pre-commit

.editorconfig

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ indent_size = 2
1414
indent_size = 4
1515

1616
[*.snap]
17-
trim_trailing_whitespace = false
17+
trim_trailing_whitespace = false
18+
19+
[*.md]
20+
max_line_length = 100

.github/PULL_REQUEST_TEMPLATE.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!--
2+
Thank you for contributing to Ruff! To help us out with reviewing, please consider the following:
3+
4+
- Does this pull request include a summary of the change? (See below.)
5+
- Does this pull request include a descriptive title?
6+
- Does this pull request include references to any relevant issues?
7+
-->
8+
9+
## Summary
10+
11+
<!-- What's the purpose of the change? What does it do, and why? -->
12+
13+
## Test Plan
14+
15+
<!-- How was it tested? -->

.github/release.yml

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes#configuring-automatically-generated-release-notes
22
changelog:
3+
exclude:
4+
labels:
5+
- internal
6+
- documentation
37
categories:
48
- title: Breaking Changes
59
labels:
@@ -11,6 +15,7 @@ changelog:
1115
- title: Settings
1216
labels:
1317
- configuration
18+
- cli
1419
- title: Bug Fixes
1520
labels:
1621
- bug

.github/workflows/docs.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: mkdocs
22

33
on:
4-
release:
5-
types: [published]
64
workflow_dispatch:
5+
release:
6+
types: [ published ]
77

88
jobs:
99
mkdocs:

.github/workflows/flake8-to-ruff.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- name: "Build wheels - universal2"
5353
uses: PyO3/maturin-action@v1
5454
with:
55-
args: --release --universal2 --out dist -m ./${{ env.CRATE_NAME }}/Cargo.toml
55+
args: --release --target universal2-apple-darwin --out dist -m ./${{ env.CRATE_NAME }}/Cargo.toml
5656
- name: "Install built wheel - universal2"
5757
run: |
5858
pip install dist/${{ env.CRATE_NAME }}-*universal2.whl --force-reinstall

.github/workflows/playground.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name: "[Playground] Release"
22

33
on:
44
workflow_dispatch:
5-
push:
6-
branches: [main]
5+
release:
6+
types: [ published ]
77

88
env:
99
CARGO_INCREMENTAL: 0

.github/workflows/release.yaml

+32-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: "[ruff] Release"
33
on:
44
workflow_dispatch:
55
release:
6-
types: [published]
6+
types: [ published ]
77

88
concurrency:
99
group: ${{ github.workflow }}-${{ github.ref }}
@@ -94,7 +94,7 @@ jobs:
9494
- name: "Build wheels - universal2"
9595
uses: PyO3/maturin-action@v1
9696
with:
97-
args: --release --universal2 --out dist
97+
args: --release --target universal2-apple-darwin --out dist
9898
- name: "Test wheel - universal2"
9999
run: |
100100
pip install dist/${{ env.PACKAGE_NAME }}-*universal2.whl --force-reinstall
@@ -394,21 +394,22 @@ jobs:
394394
- musllinux
395395
- musllinux-cross
396396
if: "startsWith(github.ref, 'refs/tags/')"
397+
environment:
398+
name: release
399+
permissions:
400+
# For pypi trusted publishing
401+
id-token: write
397402
steps:
398403
- uses: actions/download-artifact@v3
399404
with:
400405
name: wheels
401-
- uses: actions/setup-python@v4
406+
path: wheels
402407
- name: "Publish to PyPi"
403-
env:
404-
TWINE_USERNAME: __token__
405-
TWINE_PASSWORD: ${{ secrets.RUFF_TOKEN }}
406-
run: |
407-
pip install --upgrade twine
408-
twine upload --skip-existing *
409-
- name: "Update pre-commit mirror"
410-
run: |
411-
curl -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.RUFF_PRE_COMMIT_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/charliermarsh/ruff-pre-commit/dispatches --data '{"event_type": "pypi_release"}'
408+
uses: pypa/gh-action-pypi-publish@release/v1
409+
with:
410+
skip-existing: true
411+
packages-dir: wheels
412+
verbose: true
412413
- uses: actions/download-artifact@v3
413414
with:
414415
name: binaries
@@ -417,3 +418,22 @@ jobs:
417418
uses: softprops/action-gh-release@v1
418419
with:
419420
files: binaries/*
421+
422+
# After the release has been published, we update downstream repositories
423+
# This is separate because if this fails the release is still fine, we just need to do some manual workflow triggers
424+
update-dependents:
425+
name: Release
426+
runs-on: ubuntu-latest
427+
needs: release
428+
steps:
429+
- name: "Update pre-commit mirror"
430+
uses: actions/github-script@v6
431+
with:
432+
github-token: ${{ secrets.RUFF_PRE_COMMIT_PAT }}
433+
script: |
434+
github.rest.actions.createWorkflowDispatch({
435+
owner: 'astral-sh',
436+
repo: 'ruff-pre-commit',
437+
workflow_id: 'main.yml',
438+
ref: 'main',
439+
})

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# Local cache
2-
.ruff_cache
31
crates/ruff/resources/test/cpython
42
mkdocs.yml
53
.overrides
64
ruff-old
75
github_search*.jsonl
6+
schemastore
7+
.venv*
8+
scratch.py
89

910
###
1011
# Rust.gitignore

.markdownlint.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# default to true for all rules
2+
default: true
3+
4+
# MD033/no-inline-html
5+
MD033: false
6+
7+
# MD041/first-line-h1
8+
MD041: false
9+
10+
# MD013/line-length
11+
MD013:
12+
line_length: 100
13+
code_blocks: false
14+
ignore_code_blocks: true

.pre-commit-config.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ repos:
1717
rev: v0.33.0
1818
hooks:
1919
- id: markdownlint-fix
20-
args:
21-
- --disable
22-
- MD013 # line-length
23-
- MD033 # no-inline-html
24-
- --
2520

2621
- repo: https://github.com/crate-ci/typos
2722
rev: v1.14.8

BREAKING_CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ the intention of adding a stable public API in the future.
8686
### `select`, `extend-select`, `ignore`, and `extend-ignore` have new semantics ([#2312](https://github.com/charliermarsh/ruff/pull/2312))
8787

8888
Previously, the interplay between `select` and its related options could lead to unexpected
89-
behavior. For example, `ruff --select E501 --ignore ALL` and `ruff --select E501 --extend-ignore ALL` behaved differently. (See [#2312](https://github.com/charliermarsh/ruff/pull/2312) for more
89+
behavior. For example, `ruff --select E501 --ignore ALL` and `ruff --select E501 --extend-ignore ALL`
90+
behaved differently. (See [#2312](https://github.com/charliermarsh/ruff/pull/2312) for more
9091
examples.)
9192

9293
When Ruff determines the enabled rule set, it has to reconcile `select` and `ignore` from a variety

CONTRIBUTING.md

+53-20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Welcome! We're happy to have you here. Thank you in advance for your contributio
88
- [Project Structure](#project-structure)
99
- [Example: Adding a new lint rule](#example-adding-a-new-lint-rule)
1010
- [Rule naming convention](#rule-naming-convention)
11+
- [Rule testing: fixtures and snapshots](#rule-testing-fixtures-and-snapshots)
1112
- [Example: Adding a new configuration option](#example-adding-a-new-configuration-option)
1213
- [MkDocs](#mkdocs)
1314
- [Release Process](#release-process)
@@ -93,24 +94,32 @@ At time of writing, the repository includes the following crates:
9394

9495
- `crates/ruff`: library crate containing all lint rules and the core logic for running them.
9596
- `crates/ruff_cli`: binary crate containing Ruff's command-line interface.
96-
- `crates/ruff_dev`: binary crate containing utilities used in the development of Ruff itself (e.g., `cargo dev generate-all`).
97+
- `crates/ruff_dev`: binary crate containing utilities used in the development of Ruff itself (e.g.,
98+
`cargo dev generate-all`).
9799
- `crates/ruff_macros`: library crate containing macros used by Ruff.
98-
- `crates/ruff_python`: library crate implementing Python-specific functionality (e.g., lists of standard library modules by versionb).
100+
- `crates/ruff_python`: library crate implementing Python-specific functionality (e.g., lists of
101+
standard library modules by version).
99102
- `crates/flake8_to_ruff`: binary crate for generating Ruff configuration from Flake8 configuration.
100103

101104
### Example: Adding a new lint rule
102105

103106
At a high level, the steps involved in adding a new lint rule are as follows:
104107

105108
1. Determine a name for the new rule as per our [rule naming convention](#rule-naming-convention).
109+
106110
1. Create a file for your rule (e.g., `crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs`).
111+
107112
1. In that file, define a violation struct. You can grep for `#[violation]` to see examples.
108-
1. Map the violation struct to a rule code in `crates/ruff/src/registry.rs` (e.g., `E402`).
109-
1. Define the logic for triggering the violation in `crates/ruff/src/checkers/ast/mod.rs` (for AST-based
110-
checks), `crates/ruff/src/checkers/tokens.rs` (for token-based checks), `crates/ruff/src/checkers/lines.rs`
111-
(for text-based checks), or `crates/ruff/src/checkers/filesystem.rs` (for filesystem-based
112-
checks).
113-
1. Add a test fixture.
113+
114+
1. Map the violation struct to a rule code in `crates/ruff/src/codes.rs` (e.g., `E402`).
115+
116+
1. Define the logic for triggering the violation in `crates/ruff/src/checkers/ast/mod.rs` (for
117+
AST-based checks), `crates/ruff/src/checkers/tokens.rs` (for token-based checks),
118+
`crates/ruff/src/checkers/lines.rs` (for text-based checks), or
119+
`crates/ruff/src/checkers/filesystem.rs` (for filesystem-based checks).
120+
121+
1. Add proper [testing](#rule-testing-fixtures-and-snapshots) for your rule.
122+
114123
1. Update the generated files (documentation and generated code).
115124

116125
To define the violation, start by creating a dedicated file for your rule under the appropriate
@@ -125,18 +134,8 @@ collecting diagnostics as it goes.
125134
If you need to inspect the AST, you can run `cargo dev print-ast` with a Python file. Grep
126135
for the `Check::new` invocations to understand how other, similar rules are implemented.
127136

128-
To add a test fixture, create a file under `crates/ruff/resources/test/fixtures/[linter]`, named to match
129-
the code you defined earlier (e.g., `crates/ruff/resources/test/fixtures/pycodestyle/E402.py`). This file should
130-
contain a variety of violations and non-violations designed to evaluate and demonstrate the behavior
131-
of your lint rule.
132-
133-
Run `cargo dev generate-all` to generate the code for your new fixture. Then run Ruff
134-
locally with (e.g.) `cargo run -p ruff_cli -- check crates/ruff/resources/test/fixtures/pycodestyle/E402.py --no-cache --select E402`.
135-
136-
Once you're satisfied with the output, codify the behavior as a snapshot test by adding a new
137-
`test_case` macro in the relevant `crates/ruff/src/rules/[linter]/mod.rs` file. Then, run `cargo test`.
138-
Your test will fail, but you'll be prompted to follow-up with `cargo insta review`. Accept the
139-
generated snapshot, then commit the snapshot file alongside the rest of your changes.
137+
Once you're satisfied with your code, add tests for your rule. See [rule testing](#rule-testing-fixtures-and-snapshots)
138+
for more details.
140139

141140
Finally, regenerate the documentation and generated code with `cargo dev generate-all`.
142141

@@ -154,6 +153,38 @@ This implies that rule names:
154153
When re-implementing rules from other linters, this convention is given more importance than
155154
preserving the original rule name.
156155

156+
#### Rule testing: fixtures and snapshots
157+
158+
To test rules, Ruff uses snapshots of Ruff's output for a given file (fixture). Generally, there
159+
will be one file per rule (e.g., `E402.py`), and each file will contain all necessary examples of
160+
both violations and non-violations. `cargo insta review` will generate a snapshot file containing
161+
Ruff's output for each fixture, which you can then commit alongside your changes.
162+
163+
Once you've completed the code for the rule itself, you can define tests with the following steps:
164+
165+
1. Add a Python file to `crates/ruff/resources/test/fixtures/[linter]` that contains the code you
166+
want to test. The file name should match the rule name (e.g., `E402.py`), and it should include
167+
examples of both violations and non-violations.
168+
169+
1. Run Ruff locally against your file and verify the output is as expected. Once you're satisfied
170+
with the output (you see the violations you expect, and no others), proceed to the next step.
171+
For example, if you're adding a new rule named `E402`, you would run:
172+
173+
```shell
174+
cargo run -p ruff_cli -- check crates/ruff/resources/test/fixtures/pycodestyle/E402.py --no-cache
175+
```
176+
177+
1. Add the test to the relevant `crates/ruff/src/rules/[linter]/mod.rs` file. If you're contributing
178+
a rule to a pre-existing set, you should be able to find a similar example to pattern-match
179+
against. If you're adding a new linter, you'll need to create a new `mod.rs` file (see,
180+
e.g., `crates/ruff/src/rules/flake8_bugbear/mod.rs`)
181+
182+
1. Run `cargo test`. Your test will fail, but you'll be prompted to follow-up
183+
with `cargo insta review`. Run `cargo insta review`, review and accept the generated snapshot,
184+
then commit the snapshot file alongside the rest of your changes.
185+
186+
1. Run `cargo test` again to ensure that your test passes.
187+
157188
### Example: Adding a new configuration option
158189

159190
Ruff's user-facing settings live in a few different places.
@@ -184,6 +215,8 @@ Finally, regenerate the documentation and generated code with `cargo dev generat
184215

185216
To preview any changes to the documentation locally:
186217

218+
1. Install the [Rust toolchain](https://www.rust-lang.org/tools/install).
219+
187220
1. Install MkDocs and Material for MkDocs with:
188221

189222
```shell

0 commit comments

Comments
 (0)