Compare commits
123 Commits
brent/lamb
...
0.14.6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59c6cb521d | ||
|
|
54dba15088 | ||
|
|
1af318534a | ||
|
|
553e568624 | ||
|
|
cdef3f5ab8 | ||
|
|
6178822427 | ||
|
|
6b7adb0537 | ||
|
|
06941c1987 | ||
|
|
eb7c098d6b | ||
|
|
1b28fc1f14 | ||
|
|
290a5720cb | ||
|
|
c4767f5aa8 | ||
|
|
6e84f4fd7a | ||
|
|
78ce17ce8f | ||
|
|
0761ea42d9 | ||
|
|
416e2267da | ||
|
|
02c102da88 | ||
|
|
29c24bc8a6 | ||
|
|
83134fb380 | ||
|
|
0d47334f3b | ||
|
|
a8f7ccf2ca | ||
|
|
ce06094ada | ||
|
|
97935518e9 | ||
|
|
68ebd5132c | ||
|
|
ac9c83e581 | ||
|
|
5dd56264fb | ||
|
|
18a14bfaf1 | ||
|
|
ffce0de3c4 | ||
|
|
e29f3ed0fe | ||
|
|
663f78e644 | ||
|
|
c796a70ec9 | ||
|
|
192c37d540 | ||
|
|
0645418f00 | ||
|
|
62343a101a | ||
|
|
8dad289062 | ||
|
|
f67236b932 | ||
|
|
cbc6863b8c | ||
|
|
7043d51df0 | ||
|
|
5ca9c15fc8 | ||
|
|
7a739d6b76 | ||
|
|
d5a95ec824 | ||
|
|
b1e354bd99 | ||
|
|
e4a32ba644 | ||
|
|
ac2d07e83c | ||
|
|
8156b45173 | ||
|
|
d063c71177 | ||
|
|
c16ef709f6 | ||
|
|
04a3ec3689 | ||
|
|
1a86e13472 | ||
|
|
901e9cdf49 | ||
|
|
58fa1d71b6 | ||
|
|
d9fc0f08b4 | ||
|
|
09deeabda5 | ||
|
|
1436e688cc | ||
|
|
d6c34b98a5 | ||
|
|
1b50e032a4 | ||
|
|
687ed292f6 | ||
|
|
0554b1ca8a | ||
|
|
bbe42bc775 | ||
|
|
0d2cd84df4 | ||
|
|
665f68036c | ||
|
|
f5fb5c388a | ||
|
|
dbd72480a9 | ||
|
|
75c1a0ae55 | ||
|
|
7a546809c4 | ||
|
|
3065f8dbbc | ||
|
|
fb5b8c3653 | ||
|
|
efa2b5167f | ||
|
|
29acc1e860 | ||
|
|
698231a47a | ||
|
|
d63b4b0383 | ||
|
|
c5d654bce8 | ||
|
|
3e7e91724c | ||
|
|
2a2b719f00 | ||
|
|
ffb7bdd595 | ||
|
|
0a55327d64 | ||
|
|
008e9d06e1 | ||
|
|
8529d79a70 | ||
|
|
8599c7e5b3 | ||
|
|
5f501374c4 | ||
|
|
e9a5337136 | ||
|
|
05cf53aae8 | ||
|
|
6a26f86778 | ||
|
|
d0314131fb | ||
|
|
696d7a5d68 | ||
|
|
66e9d57797 | ||
|
|
87dafb8787 | ||
|
|
9e80e5a3a6 | ||
|
|
f9cc26aa12 | ||
|
|
d49c326309 | ||
|
|
e70fccbf25 | ||
|
|
90b32f3b3b | ||
|
|
99694b6e4a | ||
|
|
67e54fffe1 | ||
|
|
a01b0d7780 | ||
|
|
04ab9170d6 | ||
|
|
12e74ae894 | ||
|
|
d64b2f747c | ||
|
|
cd183c5e1f | ||
|
|
eb1957cd17 | ||
|
|
7e3dd0764a | ||
|
|
a6abd65c2c | ||
|
|
3d4b0559f1 | ||
|
|
2f6f3e1042 | ||
|
|
9dd666d677 | ||
|
|
a1d9cb5830 | ||
|
|
8a85a2961e | ||
|
|
43427abb61 | ||
|
|
84c3cecad6 | ||
|
|
e8e8180888 | ||
|
|
f5cf672ed4 | ||
|
|
6322f37015 | ||
|
|
d272a623d3 | ||
|
|
19c7994e90 | ||
|
|
725ae69773 | ||
|
|
d2c3996f4e | ||
|
|
988c38c013 | ||
|
|
164c2a6cc6 | ||
|
|
1bbe4f0d5e | ||
|
|
cd7354a5c6 | ||
|
|
ec48a47a88 | ||
|
|
43297d3455 | ||
|
|
4373974dd9 |
@@ -7,6 +7,10 @@ serial = { max-threads = 1 }
|
|||||||
filter = 'binary(file_watching)'
|
filter = 'binary(file_watching)'
|
||||||
test-group = 'serial'
|
test-group = 'serial'
|
||||||
|
|
||||||
|
[[profile.default.overrides]]
|
||||||
|
filter = 'binary(e2e)'
|
||||||
|
test-group = 'serial'
|
||||||
|
|
||||||
[profile.ci]
|
[profile.ci]
|
||||||
# Print out output for failing tests as soon as they fail, and also at the end
|
# Print out output for failing tests as soon as they fail, and also at the end
|
||||||
# of the run (for easy scrollability).
|
# of the run (for easy scrollability).
|
||||||
|
|||||||
54
.github/workflows/ci.yaml
vendored
54
.github/workflows/ci.yaml
vendored
@@ -261,15 +261,15 @@ jobs:
|
|||||||
- name: "Install mold"
|
- name: "Install mold"
|
||||||
uses: rui314/setup-mold@725a8794d15fc7563f59595bd9556495c0564878 # v1
|
uses: rui314/setup-mold@725a8794d15fc7563f59595bd9556495c0564878 # v1
|
||||||
- name: "Install cargo nextest"
|
- name: "Install cargo nextest"
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
uses: taiki-e/install-action@537c30d2b45cc3aa3fb35e2bbcfb61ef93fd6f02 # v2.62.52
|
||||||
with:
|
with:
|
||||||
tool: cargo-nextest
|
tool: cargo-nextest
|
||||||
- name: "Install cargo insta"
|
- name: "Install cargo insta"
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
uses: taiki-e/install-action@537c30d2b45cc3aa3fb35e2bbcfb61ef93fd6f02 # v2.62.52
|
||||||
with:
|
with:
|
||||||
tool: cargo-insta
|
tool: cargo-insta
|
||||||
- name: "Install uv"
|
- name: "Install uv"
|
||||||
uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
with:
|
with:
|
||||||
enable-cache: "true"
|
enable-cache: "true"
|
||||||
- name: ty mdtests (GitHub annotations)
|
- name: ty mdtests (GitHub annotations)
|
||||||
@@ -319,19 +319,17 @@ jobs:
|
|||||||
- name: "Install mold"
|
- name: "Install mold"
|
||||||
uses: rui314/setup-mold@725a8794d15fc7563f59595bd9556495c0564878 # v1
|
uses: rui314/setup-mold@725a8794d15fc7563f59595bd9556495c0564878 # v1
|
||||||
- name: "Install cargo nextest"
|
- name: "Install cargo nextest"
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
uses: taiki-e/install-action@537c30d2b45cc3aa3fb35e2bbcfb61ef93fd6f02 # v2.62.52
|
||||||
with:
|
with:
|
||||||
tool: cargo-nextest
|
tool: cargo-nextest
|
||||||
- name: "Install cargo insta"
|
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
|
||||||
with:
|
|
||||||
tool: cargo-insta
|
|
||||||
- name: "Install uv"
|
- name: "Install uv"
|
||||||
uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
with:
|
with:
|
||||||
enable-cache: "true"
|
enable-cache: "true"
|
||||||
- name: "Run tests"
|
- name: "Run tests"
|
||||||
run: cargo insta test --release --all-features --unreferenced reject --test-runner nextest
|
run: cargo nextest run --cargo-profile profiling --all-features
|
||||||
|
- name: "Run doctests"
|
||||||
|
run: cargo test --doc --profile profiling --all-features
|
||||||
|
|
||||||
cargo-test-other:
|
cargo-test-other:
|
||||||
strategy:
|
strategy:
|
||||||
@@ -354,11 +352,11 @@ jobs:
|
|||||||
- name: "Install Rust toolchain"
|
- name: "Install Rust toolchain"
|
||||||
run: rustup show
|
run: rustup show
|
||||||
- name: "Install cargo nextest"
|
- name: "Install cargo nextest"
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
uses: taiki-e/install-action@537c30d2b45cc3aa3fb35e2bbcfb61ef93fd6f02 # v2.62.52
|
||||||
with:
|
with:
|
||||||
tool: cargo-nextest
|
tool: cargo-nextest
|
||||||
- name: "Install uv"
|
- name: "Install uv"
|
||||||
uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
with:
|
with:
|
||||||
enable-cache: "true"
|
enable-cache: "true"
|
||||||
- name: "Run tests"
|
- name: "Run tests"
|
||||||
@@ -464,7 +462,7 @@ jobs:
|
|||||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
shared-key: ruff-linux-debug
|
shared-key: ruff-linux-debug
|
||||||
@@ -499,7 +497,7 @@ jobs:
|
|||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- name: "Install Rust toolchain"
|
- name: "Install Rust toolchain"
|
||||||
run: rustup component add rustfmt
|
run: rustup component add rustfmt
|
||||||
# Run all code generation scripts, and verify that the current output is
|
# Run all code generation scripts, and verify that the current output is
|
||||||
@@ -534,7 +532,7 @@ jobs:
|
|||||||
ref: ${{ github.event.pull_request.base.ref }}
|
ref: ${{ github.event.pull_request.base.ref }}
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.PYTHON_VERSION }}
|
python-version: ${{ env.PYTHON_VERSION }}
|
||||||
activate-environment: true
|
activate-environment: true
|
||||||
@@ -640,7 +638,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
@@ -699,7 +697,7 @@ jobs:
|
|||||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
@@ -750,7 +748,7 @@ jobs:
|
|||||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
@@ -794,7 +792,7 @@ jobs:
|
|||||||
- name: "Install Rust toolchain"
|
- name: "Install Rust toolchain"
|
||||||
run: rustup show
|
run: rustup show
|
||||||
- name: Install uv
|
- name: Install uv
|
||||||
uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
with:
|
with:
|
||||||
python-version: 3.13
|
python-version: 3.13
|
||||||
activate-environment: true
|
activate-environment: true
|
||||||
@@ -949,13 +947,13 @@ jobs:
|
|||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
|
|
||||||
- name: "Install Rust toolchain"
|
- name: "Install Rust toolchain"
|
||||||
run: rustup show
|
run: rustup show
|
||||||
|
|
||||||
- name: "Install codspeed"
|
- name: "Install codspeed"
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
uses: taiki-e/install-action@537c30d2b45cc3aa3fb35e2bbcfb61ef93fd6f02 # v2.62.52
|
||||||
with:
|
with:
|
||||||
tool: cargo-codspeed
|
tool: cargo-codspeed
|
||||||
|
|
||||||
@@ -963,7 +961,7 @@ jobs:
|
|||||||
run: cargo codspeed build --features "codspeed,instrumented" --profile profiling --no-default-features -p ruff_benchmark --bench formatter --bench lexer --bench linter --bench parser
|
run: cargo codspeed build --features "codspeed,instrumented" --profile profiling --no-default-features -p ruff_benchmark --bench formatter --bench lexer --bench linter --bench parser
|
||||||
|
|
||||||
- name: "Run benchmarks"
|
- name: "Run benchmarks"
|
||||||
uses: CodSpeedHQ/action@bb005fe1c1eea036d3894f02c049cb6b154a1c27 # v4.3.3
|
uses: CodSpeedHQ/action@6a8e2b874c338bf81cc5e8be715ada75908d3871 # v4.3.4
|
||||||
with:
|
with:
|
||||||
mode: instrumentation
|
mode: instrumentation
|
||||||
run: cargo codspeed run
|
run: cargo codspeed run
|
||||||
@@ -989,13 +987,13 @@ jobs:
|
|||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
|
|
||||||
- name: "Install Rust toolchain"
|
- name: "Install Rust toolchain"
|
||||||
run: rustup show
|
run: rustup show
|
||||||
|
|
||||||
- name: "Install codspeed"
|
- name: "Install codspeed"
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
uses: taiki-e/install-action@537c30d2b45cc3aa3fb35e2bbcfb61ef93fd6f02 # v2.62.52
|
||||||
with:
|
with:
|
||||||
tool: cargo-codspeed
|
tool: cargo-codspeed
|
||||||
|
|
||||||
@@ -1003,7 +1001,7 @@ jobs:
|
|||||||
run: cargo codspeed build --features "codspeed,instrumented" --profile profiling --no-default-features -p ruff_benchmark --bench ty
|
run: cargo codspeed build --features "codspeed,instrumented" --profile profiling --no-default-features -p ruff_benchmark --bench ty
|
||||||
|
|
||||||
- name: "Run benchmarks"
|
- name: "Run benchmarks"
|
||||||
uses: CodSpeedHQ/action@bb005fe1c1eea036d3894f02c049cb6b154a1c27 # v4.3.3
|
uses: CodSpeedHQ/action@6a8e2b874c338bf81cc5e8be715ada75908d3871 # v4.3.4
|
||||||
with:
|
with:
|
||||||
mode: instrumentation
|
mode: instrumentation
|
||||||
run: cargo codspeed run
|
run: cargo codspeed run
|
||||||
@@ -1029,13 +1027,13 @@ jobs:
|
|||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
|
|
||||||
- name: "Install Rust toolchain"
|
- name: "Install Rust toolchain"
|
||||||
run: rustup show
|
run: rustup show
|
||||||
|
|
||||||
- name: "Install codspeed"
|
- name: "Install codspeed"
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
uses: taiki-e/install-action@537c30d2b45cc3aa3fb35e2bbcfb61ef93fd6f02 # v2.62.52
|
||||||
with:
|
with:
|
||||||
tool: cargo-codspeed
|
tool: cargo-codspeed
|
||||||
|
|
||||||
@@ -1043,7 +1041,7 @@ jobs:
|
|||||||
run: cargo codspeed build --features "codspeed,walltime" --profile profiling --no-default-features -p ruff_benchmark
|
run: cargo codspeed build --features "codspeed,walltime" --profile profiling --no-default-features -p ruff_benchmark
|
||||||
|
|
||||||
- name: "Run benchmarks"
|
- name: "Run benchmarks"
|
||||||
uses: CodSpeedHQ/action@bb005fe1c1eea036d3894f02c049cb6b154a1c27 # v4.3.3
|
uses: CodSpeedHQ/action@6a8e2b874c338bf81cc5e8be715ada75908d3871 # v4.3.4
|
||||||
env:
|
env:
|
||||||
# enabling walltime flamegraphs adds ~6 minutes to the CI time, and they don't
|
# enabling walltime flamegraphs adds ~6 minutes to the CI time, and they don't
|
||||||
# appear to provide much useful insight for our walltime benchmarks right now
|
# appear to provide much useful insight for our walltime benchmarks right now
|
||||||
|
|||||||
2
.github/workflows/daily_fuzz.yaml
vendored
2
.github/workflows/daily_fuzz.yaml
vendored
@@ -34,7 +34,7 @@ jobs:
|
|||||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- name: "Install Rust toolchain"
|
- name: "Install Rust toolchain"
|
||||||
run: rustup show
|
run: rustup show
|
||||||
- name: "Install mold"
|
- name: "Install mold"
|
||||||
|
|||||||
5
.github/workflows/mypy_primer.yaml
vendored
5
.github/workflows/mypy_primer.yaml
vendored
@@ -43,7 +43,7 @@ jobs:
|
|||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
- name: Install the latest version of uv
|
- name: Install the latest version of uv
|
||||||
uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
@@ -55,6 +55,7 @@ jobs:
|
|||||||
- name: Run mypy_primer
|
- name: Run mypy_primer
|
||||||
env:
|
env:
|
||||||
PRIMER_SELECTOR: crates/ty_python_semantic/resources/primer/good.txt
|
PRIMER_SELECTOR: crates/ty_python_semantic/resources/primer/good.txt
|
||||||
|
CLICOLOR_FORCE: "1"
|
||||||
DIFF_FILE: mypy_primer.diff
|
DIFF_FILE: mypy_primer.diff
|
||||||
run: |
|
run: |
|
||||||
cd ruff
|
cd ruff
|
||||||
@@ -80,7 +81,7 @@ jobs:
|
|||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
- name: Install the latest version of uv
|
- name: Install the latest version of uv
|
||||||
uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
|
||||||
with:
|
with:
|
||||||
|
|||||||
2
.github/workflows/publish-pypi.yml
vendored
2
.github/workflows/publish-pypi.yml
vendored
@@ -22,7 +22,7 @@ jobs:
|
|||||||
id-token: write
|
id-token: write
|
||||||
steps:
|
steps:
|
||||||
- name: "Install uv"
|
- name: "Install uv"
|
||||||
uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
|
- uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
|
||||||
with:
|
with:
|
||||||
pattern: wheels-*
|
pattern: wheels-*
|
||||||
|
|||||||
15
.github/workflows/sync_typeshed.yaml
vendored
15
.github/workflows/sync_typeshed.yaml
vendored
@@ -77,7 +77,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
git config --global user.name typeshedbot
|
git config --global user.name typeshedbot
|
||||||
git config --global user.email '<>'
|
git config --global user.email '<>'
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- name: Sync typeshed stubs
|
- name: Sync typeshed stubs
|
||||||
run: |
|
run: |
|
||||||
rm -rf "ruff/${VENDORED_TYPESHED}"
|
rm -rf "ruff/${VENDORED_TYPESHED}"
|
||||||
@@ -131,7 +131,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: true
|
persist-credentials: true
|
||||||
ref: ${{ env.UPSTREAM_BRANCH}}
|
ref: ${{ env.UPSTREAM_BRANCH}}
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- name: Setup git
|
- name: Setup git
|
||||||
run: |
|
run: |
|
||||||
git config --global user.name typeshedbot
|
git config --global user.name typeshedbot
|
||||||
@@ -170,7 +170,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: true
|
persist-credentials: true
|
||||||
ref: ${{ env.UPSTREAM_BRANCH}}
|
ref: ${{ env.UPSTREAM_BRANCH}}
|
||||||
- uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
- uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
- name: Setup git
|
- name: Setup git
|
||||||
run: |
|
run: |
|
||||||
git config --global user.name typeshedbot
|
git config --global user.name typeshedbot
|
||||||
@@ -207,17 +207,22 @@ jobs:
|
|||||||
uses: rui314/setup-mold@725a8794d15fc7563f59595bd9556495c0564878 # v1
|
uses: rui314/setup-mold@725a8794d15fc7563f59595bd9556495c0564878 # v1
|
||||||
- name: "Install cargo nextest"
|
- name: "Install cargo nextest"
|
||||||
if: ${{ success() }}
|
if: ${{ success() }}
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
uses: taiki-e/install-action@537c30d2b45cc3aa3fb35e2bbcfb61ef93fd6f02 # v2.62.52
|
||||||
with:
|
with:
|
||||||
tool: cargo-nextest
|
tool: cargo-nextest
|
||||||
- name: "Install cargo insta"
|
- name: "Install cargo insta"
|
||||||
if: ${{ success() }}
|
if: ${{ success() }}
|
||||||
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
|
uses: taiki-e/install-action@537c30d2b45cc3aa3fb35e2bbcfb61ef93fd6f02 # v2.62.52
|
||||||
with:
|
with:
|
||||||
tool: cargo-insta
|
tool: cargo-insta
|
||||||
- name: Update snapshots
|
- name: Update snapshots
|
||||||
if: ${{ success() }}
|
if: ${{ success() }}
|
||||||
run: |
|
run: |
|
||||||
|
cargo r \
|
||||||
|
--profile=profiling \
|
||||||
|
-p ty_completion_eval \
|
||||||
|
-- all --tasks ./crates/ty_completion_eval/completion-evaluation-tasks.csv
|
||||||
|
|
||||||
# The `cargo insta` docs indicate that `--unreferenced=delete` might be a good option,
|
# The `cargo insta` docs indicate that `--unreferenced=delete` might be a good option,
|
||||||
# but from local testing it appears to just revert all changes made by `cargo insta test --accept`.
|
# but from local testing it appears to just revert all changes made by `cargo insta test --accept`.
|
||||||
#
|
#
|
||||||
|
|||||||
4
.github/workflows/ty-ecosystem-analyzer.yaml
vendored
4
.github/workflows/ty-ecosystem-analyzer.yaml
vendored
@@ -33,7 +33,7 @@ jobs:
|
|||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
- name: Install the latest version of uv
|
- name: Install the latest version of uv
|
||||||
uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
with:
|
with:
|
||||||
enable-cache: true # zizmor: ignore[cache-poisoning] acceptable risk for CloudFlare pages artifact
|
enable-cache: true # zizmor: ignore[cache-poisoning] acceptable risk for CloudFlare pages artifact
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ jobs:
|
|||||||
|
|
||||||
cd ..
|
cd ..
|
||||||
|
|
||||||
uv tool install "git+https://github.com/astral-sh/ecosystem-analyzer@908758da02a73ef3f3308e1dbb2248510029bbe4"
|
uv tool install "git+https://github.com/astral-sh/ecosystem-analyzer@e26ebfb78d372b8b091e1cb1d6fc522e135474c1"
|
||||||
|
|
||||||
ecosystem-analyzer \
|
ecosystem-analyzer \
|
||||||
--repository ruff \
|
--repository ruff \
|
||||||
|
|||||||
4
.github/workflows/ty-ecosystem-report.yaml
vendored
4
.github/workflows/ty-ecosystem-report.yaml
vendored
@@ -29,7 +29,7 @@ jobs:
|
|||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
- name: Install the latest version of uv
|
- name: Install the latest version of uv
|
||||||
uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
|
uses: astral-sh/setup-uv@5a7eac68fb9809dea845d802897dc5c723910fa3 # v7.1.3
|
||||||
with:
|
with:
|
||||||
enable-cache: true # zizmor: ignore[cache-poisoning] acceptable risk for CloudFlare pages artifact
|
enable-cache: true # zizmor: ignore[cache-poisoning] acceptable risk for CloudFlare pages artifact
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ jobs:
|
|||||||
|
|
||||||
cd ..
|
cd ..
|
||||||
|
|
||||||
uv tool install "git+https://github.com/astral-sh/ecosystem-analyzer@908758da02a73ef3f3308e1dbb2248510029bbe4"
|
uv tool install "git+https://github.com/astral-sh/ecosystem-analyzer@e26ebfb78d372b8b091e1cb1d6fc522e135474c1"
|
||||||
|
|
||||||
ecosystem-analyzer \
|
ecosystem-analyzer \
|
||||||
--verbose \
|
--verbose \
|
||||||
|
|||||||
96
CHANGELOG.md
96
CHANGELOG.md
@@ -1,5 +1,101 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.14.6
|
||||||
|
|
||||||
|
Released on 2025-11-21.
|
||||||
|
|
||||||
|
### Preview features
|
||||||
|
|
||||||
|
- \[`flake8-bandit`\] Support new PySNMP API paths (`S508`, `S509`) ([#21374](https://github.com/astral-sh/ruff/pull/21374))
|
||||||
|
|
||||||
|
### Bug fixes
|
||||||
|
|
||||||
|
- Adjust own-line comment placement between branches ([#21185](https://github.com/astral-sh/ruff/pull/21185))
|
||||||
|
- Avoid syntax error when formatting attribute expressions with outer parentheses, parenthesized value, and trailing comment on value ([#20418](https://github.com/astral-sh/ruff/pull/20418))
|
||||||
|
- Fix panic when formatting comments in unary expressions ([#21501](https://github.com/astral-sh/ruff/pull/21501))
|
||||||
|
- Respect `fmt: skip` for compound statements on a single line ([#20633](https://github.com/astral-sh/ruff/pull/20633))
|
||||||
|
- \[`refurb`\] Fix `FURB103` autofix ([#21454](https://github.com/astral-sh/ruff/pull/21454))
|
||||||
|
- \[`ruff`\] Fix false positive for complex conversion specifiers in `logging-eager-conversion` (`RUF065`) ([#21464](https://github.com/astral-sh/ruff/pull/21464))
|
||||||
|
|
||||||
|
### Rule changes
|
||||||
|
|
||||||
|
- \[`ruff`\] Avoid false positive on `ClassVar` reassignment (`RUF012`) ([#21478](https://github.com/astral-sh/ruff/pull/21478))
|
||||||
|
|
||||||
|
### CLI
|
||||||
|
|
||||||
|
- Render hyperlinks for lint errors ([#21514](https://github.com/astral-sh/ruff/pull/21514))
|
||||||
|
- Add a `ruff analyze` option to skip over imports in `TYPE_CHECKING` blocks ([#21472](https://github.com/astral-sh/ruff/pull/21472))
|
||||||
|
|
||||||
|
### Documentation
|
||||||
|
|
||||||
|
- Limit `eglot-format` hook to eglot-managed Python buffers ([#21459](https://github.com/astral-sh/ruff/pull/21459))
|
||||||
|
- Mention `force-exclude` in "Configuration > Python file discovery" ([#21500](https://github.com/astral-sh/ruff/pull/21500))
|
||||||
|
|
||||||
|
### Contributors
|
||||||
|
|
||||||
|
- [@ntBre](https://github.com/ntBre)
|
||||||
|
- [@dylwil3](https://github.com/dylwil3)
|
||||||
|
- [@gauthsvenkat](https://github.com/gauthsvenkat)
|
||||||
|
- [@MichaReiser](https://github.com/MichaReiser)
|
||||||
|
- [@thamer](https://github.com/thamer)
|
||||||
|
- [@Ruchir28](https://github.com/Ruchir28)
|
||||||
|
- [@thejcannon](https://github.com/thejcannon)
|
||||||
|
- [@danparizher](https://github.com/danparizher)
|
||||||
|
- [@chirizxc](https://github.com/chirizxc)
|
||||||
|
|
||||||
|
## 0.14.5
|
||||||
|
|
||||||
|
Released on 2025-11-13.
|
||||||
|
|
||||||
|
### Preview features
|
||||||
|
|
||||||
|
- \[`flake8-simplify`\] Apply `SIM113` when index variable is of type `int` ([#21395](https://github.com/astral-sh/ruff/pull/21395))
|
||||||
|
- \[`pydoclint`\] Fix false positive when Sphinx directives follow a "Raises" section (`DOC502`) ([#20535](https://github.com/astral-sh/ruff/pull/20535))
|
||||||
|
- \[`pydoclint`\] Support NumPy-style comma-separated parameters (`DOC102`) ([#20972](https://github.com/astral-sh/ruff/pull/20972))
|
||||||
|
- \[`refurb`\] Auto-fix annotated assignments (`FURB101`) ([#21278](https://github.com/astral-sh/ruff/pull/21278))
|
||||||
|
- \[`ruff`\] Ignore `str()` when not used for simple conversion (`RUF065`) ([#21330](https://github.com/astral-sh/ruff/pull/21330))
|
||||||
|
|
||||||
|
### Bug fixes
|
||||||
|
|
||||||
|
- Fix syntax error false positive on alternative `match` patterns ([#21362](https://github.com/astral-sh/ruff/pull/21362))
|
||||||
|
- \[`flake8-simplify`\] Fix false positive for iterable initializers with generator arguments (`SIM222`) ([#21187](https://github.com/astral-sh/ruff/pull/21187))
|
||||||
|
- \[`pyupgrade`\] Fix false positive on relative imports from local `.builtins` module (`UP029`) ([#21309](https://github.com/astral-sh/ruff/pull/21309))
|
||||||
|
- \[`pyupgrade`\] Consistently set the deprecated tag (`UP035`) ([#21396](https://github.com/astral-sh/ruff/pull/21396))
|
||||||
|
|
||||||
|
### Rule changes
|
||||||
|
|
||||||
|
- \[`refurb`\] Detect empty f-strings (`FURB105`) ([#21348](https://github.com/astral-sh/ruff/pull/21348))
|
||||||
|
|
||||||
|
### CLI
|
||||||
|
|
||||||
|
- Add option to provide a reason to `--add-noqa` ([#21294](https://github.com/astral-sh/ruff/pull/21294))
|
||||||
|
- Add upstream linter URL to `ruff linter --output-format=json` ([#21316](https://github.com/astral-sh/ruff/pull/21316))
|
||||||
|
- Add color to `--help` ([#21337](https://github.com/astral-sh/ruff/pull/21337))
|
||||||
|
|
||||||
|
### Documentation
|
||||||
|
|
||||||
|
- Add a new "Opening a PR" section to the contribution guide ([#21298](https://github.com/astral-sh/ruff/pull/21298))
|
||||||
|
- Added the PyScripter IDE to the list of "Who is using Ruff?" ([#21402](https://github.com/astral-sh/ruff/pull/21402))
|
||||||
|
- Update PyCharm setup instructions ([#21409](https://github.com/astral-sh/ruff/pull/21409))
|
||||||
|
- \[`flake8-annotations`\] Add link to `allow-star-arg-any` option (`ANN401`) ([#21326](https://github.com/astral-sh/ruff/pull/21326))
|
||||||
|
|
||||||
|
### Other changes
|
||||||
|
|
||||||
|
- \[`configuration`\] Improve error message when `line-length` exceeds `u16::MAX` ([#21329](https://github.com/astral-sh/ruff/pull/21329))
|
||||||
|
|
||||||
|
### Contributors
|
||||||
|
|
||||||
|
- [@njhearp](https://github.com/njhearp)
|
||||||
|
- [@11happy](https://github.com/11happy)
|
||||||
|
- [@hugovk](https://github.com/hugovk)
|
||||||
|
- [@Gankra](https://github.com/Gankra)
|
||||||
|
- [@ntBre](https://github.com/ntBre)
|
||||||
|
- [@pyscripter](https://github.com/pyscripter)
|
||||||
|
- [@danparizher](https://github.com/danparizher)
|
||||||
|
- [@MichaReiser](https://github.com/MichaReiser)
|
||||||
|
- [@henryiii](https://github.com/henryiii)
|
||||||
|
- [@charliecloudberry](https://github.com/charliecloudberry)
|
||||||
|
|
||||||
## 0.14.4
|
## 0.14.4
|
||||||
|
|
||||||
Released on 2025-11-06.
|
Released on 2025-11-06.
|
||||||
|
|||||||
59
Cargo.lock
generated
59
Cargo.lock
generated
@@ -642,7 +642,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
|
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -651,7 +651,7 @@ version = "3.0.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fde0e0ec90c9dfb3b4b1a0891a7dcd0e2bffde2f7efed5fe7c9bb00e5bfb915e"
|
checksum = "fde0e0ec90c9dfb3b4b1a0891a7dcd0e2bffde2f7efed5fe7c9bb00e5bfb915e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1016,7 +1016,7 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"option-ext",
|
"option-ext",
|
||||||
"redox_users",
|
"redox_users",
|
||||||
"windows-sys 0.60.2",
|
"windows-sys 0.61.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1108,7 +1108,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.61.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1238,9 +1238,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "get-size-derive2"
|
name = "get-size-derive2"
|
||||||
version = "0.7.1"
|
version = "0.7.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "46b134aa084df7c3a513a1035c52f623e4b3065dfaf3d905a4f28a2e79b5bb3f"
|
checksum = "ff47daa61505c85af126e9dd64af6a342a33dc0cccfe1be74ceadc7d352e6efd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"attribute-derive",
|
"attribute-derive",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -1249,13 +1249,14 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "get-size2"
|
name = "get-size2"
|
||||||
version = "0.7.1"
|
version = "0.7.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c0d51c9f2e956a517619ad9e7eaebc7a573f9c49b38152e12eade750f89156f9"
|
checksum = "ac7bb8710e1f09672102be7ddf39f764d8440ae74a9f4e30aaa4820dcdffa4af"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"compact_str",
|
"compact_str",
|
||||||
"get-size-derive2",
|
"get-size-derive2",
|
||||||
"hashbrown 0.16.0",
|
"hashbrown 0.16.0",
|
||||||
|
"indexmap",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1575,9 +1576,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indicatif"
|
name = "indicatif"
|
||||||
version = "0.18.2"
|
version = "0.18.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ade6dfcba0dfb62ad59e59e7241ec8912af34fd29e0e743e3db992bd278e8b65"
|
checksum = "9375e112e4b463ec1b1c6c011953545c65a30164fbab5b581df32b3abf0dcb88"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"console 0.16.1",
|
"console 0.16.1",
|
||||||
"portable-atomic",
|
"portable-atomic",
|
||||||
@@ -1698,7 +1699,7 @@ checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"hermit-abi",
|
"hermit-abi",
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1762,7 +1763,7 @@ dependencies = [
|
|||||||
"portable-atomic",
|
"portable-atomic",
|
||||||
"portable-atomic-util",
|
"portable-atomic-util",
|
||||||
"serde_core",
|
"serde_core",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.61.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2606,9 +2607,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quick-junit"
|
name = "quick-junit"
|
||||||
version = "0.5.1"
|
version = "0.5.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3ed1a693391a16317257103ad06a88c6529ac640846021da7c435a06fffdacd7"
|
checksum = "6ee9342d671fae8d66b3ae9fd7a9714dfd089c04d2a8b1ec0436ef77aee15e5f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
@@ -2621,9 +2622,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quick-xml"
|
name = "quick-xml"
|
||||||
version = "0.37.5"
|
version = "0.38.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "331e97a1af0bf59823e6eadffe373d7b27f485be8748f71471c662c1f269b7fb"
|
checksum = "b66c2058c55a409d601666cffe35f04333cf1013010882cec174a7467cd4e21c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
@@ -2858,7 +2859,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruff"
|
name = "ruff"
|
||||||
version = "0.14.4"
|
version = "0.14.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"argfile",
|
"argfile",
|
||||||
@@ -3004,6 +3005,7 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"similar",
|
"similar",
|
||||||
|
"supports-hyperlinks",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"thiserror 2.0.17",
|
"thiserror 2.0.17",
|
||||||
"tracing",
|
"tracing",
|
||||||
@@ -3115,7 +3117,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruff_linter"
|
name = "ruff_linter"
|
||||||
version = "0.14.4"
|
version = "0.14.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
@@ -3470,7 +3472,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruff_wasm"
|
name = "ruff_wasm"
|
||||||
version = "0.14.4"
|
version = "0.14.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"console_error_panic_hook",
|
"console_error_panic_hook",
|
||||||
"console_log",
|
"console_log",
|
||||||
@@ -3568,7 +3570,7 @@ dependencies = [
|
|||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys",
|
"linux-raw-sys",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.61.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3586,7 +3588,7 @@ checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "salsa"
|
name = "salsa"
|
||||||
version = "0.24.0"
|
version = "0.24.0"
|
||||||
source = "git+https://github.com/salsa-rs/salsa.git?rev=05a9af7f554b64b8aadc2eeb6f2caf73d0408d09#05a9af7f554b64b8aadc2eeb6f2caf73d0408d09"
|
source = "git+https://github.com/salsa-rs/salsa.git?rev=a885bb4c4c192741b8a17418fef81a71e33d111e#a885bb4c4c192741b8a17418fef81a71e33d111e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"boxcar",
|
"boxcar",
|
||||||
"compact_str",
|
"compact_str",
|
||||||
@@ -3610,12 +3612,12 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "salsa-macro-rules"
|
name = "salsa-macro-rules"
|
||||||
version = "0.24.0"
|
version = "0.24.0"
|
||||||
source = "git+https://github.com/salsa-rs/salsa.git?rev=05a9af7f554b64b8aadc2eeb6f2caf73d0408d09#05a9af7f554b64b8aadc2eeb6f2caf73d0408d09"
|
source = "git+https://github.com/salsa-rs/salsa.git?rev=a885bb4c4c192741b8a17418fef81a71e33d111e#a885bb4c4c192741b8a17418fef81a71e33d111e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "salsa-macros"
|
name = "salsa-macros"
|
||||||
version = "0.24.0"
|
version = "0.24.0"
|
||||||
source = "git+https://github.com/salsa-rs/salsa.git?rev=05a9af7f554b64b8aadc2eeb6f2caf73d0408d09#05a9af7f554b64b8aadc2eeb6f2caf73d0408d09"
|
source = "git+https://github.com/salsa-rs/salsa.git?rev=a885bb4c4c192741b8a17418fef81a71e33d111e#a885bb4c4c192741b8a17418fef81a71e33d111e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -3925,6 +3927,12 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "supports-hyperlinks"
|
||||||
|
version = "3.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "804f44ed3c63152de6a9f90acbea1a110441de43006ea51bcce8f436196a288b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.110"
|
version = "2.0.110"
|
||||||
@@ -3963,7 +3971,7 @@ dependencies = [
|
|||||||
"getrandom 0.3.4",
|
"getrandom 0.3.4",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rustix",
|
"rustix",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.61.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -4521,6 +4529,7 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"shellexpand",
|
"shellexpand",
|
||||||
|
"smallvec",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"thiserror 2.0.17",
|
"thiserror 2.0.17",
|
||||||
"tracing",
|
"tracing",
|
||||||
@@ -5011,7 +5020,7 @@ version = "0.1.11"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.61.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -146,7 +146,7 @@ regex-automata = { version = "0.4.9" }
|
|||||||
rustc-hash = { version = "2.0.0" }
|
rustc-hash = { version = "2.0.0" }
|
||||||
rustc-stable-hash = { version = "0.1.2" }
|
rustc-stable-hash = { version = "0.1.2" }
|
||||||
# When updating salsa, make sure to also update the revision in `fuzz/Cargo.toml`
|
# When updating salsa, make sure to also update the revision in `fuzz/Cargo.toml`
|
||||||
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "05a9af7f554b64b8aadc2eeb6f2caf73d0408d09", default-features = false, features = [
|
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "a885bb4c4c192741b8a17418fef81a71e33d111e", default-features = false, features = [
|
||||||
"compact_str",
|
"compact_str",
|
||||||
"macros",
|
"macros",
|
||||||
"salsa_unstable",
|
"salsa_unstable",
|
||||||
@@ -173,6 +173,7 @@ snapbox = { version = "0.6.0", features = [
|
|||||||
static_assertions = "1.1.0"
|
static_assertions = "1.1.0"
|
||||||
strum = { version = "0.27.0", features = ["strum_macros"] }
|
strum = { version = "0.27.0", features = ["strum_macros"] }
|
||||||
strum_macros = { version = "0.27.0" }
|
strum_macros = { version = "0.27.0" }
|
||||||
|
supports-hyperlinks = { version = "3.1.0" }
|
||||||
syn = { version = "2.0.55" }
|
syn = { version = "2.0.55" }
|
||||||
tempfile = { version = "3.9.0" }
|
tempfile = { version = "3.9.0" }
|
||||||
test-case = { version = "3.3.1" }
|
test-case = { version = "3.3.1" }
|
||||||
|
|||||||
@@ -147,8 +147,8 @@ curl -LsSf https://astral.sh/ruff/install.sh | sh
|
|||||||
powershell -c "irm https://astral.sh/ruff/install.ps1 | iex"
|
powershell -c "irm https://astral.sh/ruff/install.ps1 | iex"
|
||||||
|
|
||||||
# For a specific version.
|
# For a specific version.
|
||||||
curl -LsSf https://astral.sh/ruff/0.14.4/install.sh | sh
|
curl -LsSf https://astral.sh/ruff/0.14.6/install.sh | sh
|
||||||
powershell -c "irm https://astral.sh/ruff/0.14.4/install.ps1 | iex"
|
powershell -c "irm https://astral.sh/ruff/0.14.6/install.ps1 | iex"
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also install Ruff via [Homebrew](https://formulae.brew.sh/formula/ruff), [Conda](https://anaconda.org/conda-forge/ruff),
|
You can also install Ruff via [Homebrew](https://formulae.brew.sh/formula/ruff), [Conda](https://anaconda.org/conda-forge/ruff),
|
||||||
@@ -181,7 +181,7 @@ Ruff can also be used as a [pre-commit](https://pre-commit.com/) hook via [`ruff
|
|||||||
```yaml
|
```yaml
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
# Ruff version.
|
# Ruff version.
|
||||||
rev: v0.14.4
|
rev: v0.14.6
|
||||||
hooks:
|
hooks:
|
||||||
# Run the linter.
|
# Run the linter.
|
||||||
- id: ruff-check
|
- id: ruff-check
|
||||||
@@ -491,6 +491,7 @@ Ruff is used by a number of major open-source projects and companies, including:
|
|||||||
- [PyTorch](https://github.com/pytorch/pytorch)
|
- [PyTorch](https://github.com/pytorch/pytorch)
|
||||||
- [Pydantic](https://github.com/pydantic/pydantic)
|
- [Pydantic](https://github.com/pydantic/pydantic)
|
||||||
- [Pylint](https://github.com/PyCQA/pylint)
|
- [Pylint](https://github.com/PyCQA/pylint)
|
||||||
|
- [PyScripter](https://github.com/pyscripter/pyscripter)
|
||||||
- [PyVista](https://github.com/pyvista/pyvista)
|
- [PyVista](https://github.com/pyvista/pyvista)
|
||||||
- [Reflex](https://github.com/reflex-dev/reflex)
|
- [Reflex](https://github.com/reflex-dev/reflex)
|
||||||
- [River](https://github.com/online-ml/river)
|
- [River](https://github.com/online-ml/river)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ruff"
|
name = "ruff"
|
||||||
version = "0.14.4"
|
version = "0.14.6"
|
||||||
publish = true
|
publish = true
|
||||||
authors = { workspace = true }
|
authors = { workspace = true }
|
||||||
edition = { workspace = true }
|
edition = { workspace = true }
|
||||||
|
|||||||
@@ -167,6 +167,7 @@ pub enum AnalyzeCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, clap::Parser)]
|
#[derive(Clone, Debug, clap::Parser)]
|
||||||
|
#[expect(clippy::struct_excessive_bools)]
|
||||||
pub struct AnalyzeGraphCommand {
|
pub struct AnalyzeGraphCommand {
|
||||||
/// List of files or directories to include.
|
/// List of files or directories to include.
|
||||||
#[clap(help = "List of files or directories to include [default: .]")]
|
#[clap(help = "List of files or directories to include [default: .]")]
|
||||||
@@ -193,6 +194,12 @@ pub struct AnalyzeGraphCommand {
|
|||||||
/// Path to a virtual environment to use for resolving additional dependencies
|
/// Path to a virtual environment to use for resolving additional dependencies
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
python: Option<PathBuf>,
|
python: Option<PathBuf>,
|
||||||
|
/// Include imports that are only used for type checking (i.e., imports within `if TYPE_CHECKING:` blocks).
|
||||||
|
/// Use `--no-type-checking-imports` to exclude imports that are only used for type checking.
|
||||||
|
#[arg(long, overrides_with("no_type_checking_imports"))]
|
||||||
|
type_checking_imports: bool,
|
||||||
|
#[arg(long, overrides_with("type_checking_imports"), hide = true)]
|
||||||
|
no_type_checking_imports: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
// The `Parser` derive is for ruff_dev, for ruff `Args` would be sufficient
|
// The `Parser` derive is for ruff_dev, for ruff `Args` would be sufficient
|
||||||
@@ -839,6 +846,10 @@ impl AnalyzeGraphCommand {
|
|||||||
string_imports_min_dots: self.min_dots,
|
string_imports_min_dots: self.min_dots,
|
||||||
preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from),
|
preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from),
|
||||||
target_version: self.target_version.map(ast::PythonVersion::from),
|
target_version: self.target_version.map(ast::PythonVersion::from),
|
||||||
|
type_checking_imports: resolve_bool_arg(
|
||||||
|
self.type_checking_imports,
|
||||||
|
self.no_type_checking_imports,
|
||||||
|
),
|
||||||
..ExplicitConfigOverrides::default()
|
..ExplicitConfigOverrides::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1335,6 +1346,7 @@ struct ExplicitConfigOverrides {
|
|||||||
extension: Option<Vec<ExtensionPair>>,
|
extension: Option<Vec<ExtensionPair>>,
|
||||||
detect_string_imports: Option<bool>,
|
detect_string_imports: Option<bool>,
|
||||||
string_imports_min_dots: Option<usize>,
|
string_imports_min_dots: Option<usize>,
|
||||||
|
type_checking_imports: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigurationTransformer for ExplicitConfigOverrides {
|
impl ConfigurationTransformer for ExplicitConfigOverrides {
|
||||||
@@ -1425,6 +1437,9 @@ impl ConfigurationTransformer for ExplicitConfigOverrides {
|
|||||||
if let Some(string_imports_min_dots) = &self.string_imports_min_dots {
|
if let Some(string_imports_min_dots) = &self.string_imports_min_dots {
|
||||||
config.analyze.string_imports_min_dots = Some(*string_imports_min_dots);
|
config.analyze.string_imports_min_dots = Some(*string_imports_min_dots);
|
||||||
}
|
}
|
||||||
|
if let Some(type_checking_imports) = &self.type_checking_imports {
|
||||||
|
config.analyze.type_checking_imports = Some(*type_checking_imports);
|
||||||
|
}
|
||||||
|
|
||||||
config
|
config
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ pub(crate) fn analyze_graph(
|
|||||||
let settings = resolver.resolve(path);
|
let settings = resolver.resolve(path);
|
||||||
let string_imports = settings.analyze.string_imports;
|
let string_imports = settings.analyze.string_imports;
|
||||||
let include_dependencies = settings.analyze.include_dependencies.get(path).cloned();
|
let include_dependencies = settings.analyze.include_dependencies.get(path).cloned();
|
||||||
|
let type_checking_imports = settings.analyze.type_checking_imports;
|
||||||
|
|
||||||
// Skip excluded files.
|
// Skip excluded files.
|
||||||
if (settings.file_resolver.force_exclude || !resolved_file.is_root())
|
if (settings.file_resolver.force_exclude || !resolved_file.is_root())
|
||||||
@@ -167,6 +168,7 @@ pub(crate) fn analyze_graph(
|
|||||||
&path,
|
&path,
|
||||||
package.as_deref(),
|
package.as_deref(),
|
||||||
string_imports,
|
string_imports,
|
||||||
|
type_checking_imports,
|
||||||
)
|
)
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
warn!("Failed to generate import map for {path}: {err}");
|
warn!("Failed to generate import map for {path}: {err}");
|
||||||
|
|||||||
193
crates/ruff/tests/cli/analyze_graph.rs
Normal file
193
crates/ruff/tests/cli/analyze_graph.rs
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
use insta_cmd::assert_cmd_snapshot;
|
||||||
|
|
||||||
|
use crate::CliTest;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn type_checking_imports() -> anyhow::Result<()> {
|
||||||
|
let test = AnalyzeTest::with_files([
|
||||||
|
("ruff/__init__.py", ""),
|
||||||
|
(
|
||||||
|
"ruff/a.py",
|
||||||
|
r#"
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
import ruff.b
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import ruff.c
|
||||||
|
"#,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"ruff/b.py",
|
||||||
|
r#"
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ruff import c
|
||||||
|
"#,
|
||||||
|
),
|
||||||
|
("ruff/c.py", ""),
|
||||||
|
])?;
|
||||||
|
|
||||||
|
assert_cmd_snapshot!(test.command(), @r###"
|
||||||
|
success: true
|
||||||
|
exit_code: 0
|
||||||
|
----- stdout -----
|
||||||
|
{
|
||||||
|
"ruff/__init__.py": [],
|
||||||
|
"ruff/a.py": [
|
||||||
|
"ruff/b.py",
|
||||||
|
"ruff/c.py"
|
||||||
|
],
|
||||||
|
"ruff/b.py": [
|
||||||
|
"ruff/c.py"
|
||||||
|
],
|
||||||
|
"ruff/c.py": []
|
||||||
|
}
|
||||||
|
|
||||||
|
----- stderr -----
|
||||||
|
"###);
|
||||||
|
|
||||||
|
assert_cmd_snapshot!(
|
||||||
|
test.command()
|
||||||
|
.arg("--no-type-checking-imports"),
|
||||||
|
@r###"
|
||||||
|
success: true
|
||||||
|
exit_code: 0
|
||||||
|
----- stdout -----
|
||||||
|
{
|
||||||
|
"ruff/__init__.py": [],
|
||||||
|
"ruff/a.py": [
|
||||||
|
"ruff/b.py"
|
||||||
|
],
|
||||||
|
"ruff/b.py": [],
|
||||||
|
"ruff/c.py": []
|
||||||
|
}
|
||||||
|
|
||||||
|
----- stderr -----
|
||||||
|
"###
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn type_checking_imports_from_config() -> anyhow::Result<()> {
|
||||||
|
let test = AnalyzeTest::with_files([
|
||||||
|
("ruff/__init__.py", ""),
|
||||||
|
(
|
||||||
|
"ruff/a.py",
|
||||||
|
r#"
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
import ruff.b
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import ruff.c
|
||||||
|
"#,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"ruff/b.py",
|
||||||
|
r#"
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ruff import c
|
||||||
|
"#,
|
||||||
|
),
|
||||||
|
("ruff/c.py", ""),
|
||||||
|
(
|
||||||
|
"ruff.toml",
|
||||||
|
r#"
|
||||||
|
[analyze]
|
||||||
|
type-checking-imports = false
|
||||||
|
"#,
|
||||||
|
),
|
||||||
|
])?;
|
||||||
|
|
||||||
|
assert_cmd_snapshot!(test.command(), @r###"
|
||||||
|
success: true
|
||||||
|
exit_code: 0
|
||||||
|
----- stdout -----
|
||||||
|
{
|
||||||
|
"ruff/__init__.py": [],
|
||||||
|
"ruff/a.py": [
|
||||||
|
"ruff/b.py"
|
||||||
|
],
|
||||||
|
"ruff/b.py": [],
|
||||||
|
"ruff/c.py": []
|
||||||
|
}
|
||||||
|
|
||||||
|
----- stderr -----
|
||||||
|
"###);
|
||||||
|
|
||||||
|
test.write_file(
|
||||||
|
"ruff.toml",
|
||||||
|
r#"
|
||||||
|
[analyze]
|
||||||
|
type-checking-imports = true
|
||||||
|
"#,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
assert_cmd_snapshot!(test.command(), @r###"
|
||||||
|
success: true
|
||||||
|
exit_code: 0
|
||||||
|
----- stdout -----
|
||||||
|
{
|
||||||
|
"ruff/__init__.py": [],
|
||||||
|
"ruff/a.py": [
|
||||||
|
"ruff/b.py",
|
||||||
|
"ruff/c.py"
|
||||||
|
],
|
||||||
|
"ruff/b.py": [
|
||||||
|
"ruff/c.py"
|
||||||
|
],
|
||||||
|
"ruff/c.py": []
|
||||||
|
}
|
||||||
|
|
||||||
|
----- stderr -----
|
||||||
|
"###
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AnalyzeTest {
|
||||||
|
cli_test: CliTest,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AnalyzeTest {
|
||||||
|
pub(crate) fn new() -> anyhow::Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
cli_test: CliTest::with_settings(|_, mut settings| {
|
||||||
|
settings.add_filter(r#"\\\\"#, "/");
|
||||||
|
settings
|
||||||
|
})?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with_files<'a>(files: impl IntoIterator<Item = (&'a str, &'a str)>) -> anyhow::Result<Self> {
|
||||||
|
let case = Self::new()?;
|
||||||
|
case.write_files(files)?;
|
||||||
|
Ok(case)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(unused)]
|
||||||
|
fn with_file(path: impl AsRef<std::path::Path>, content: &str) -> anyhow::Result<Self> {
|
||||||
|
let fixture = Self::new()?;
|
||||||
|
fixture.write_file(path, content)?;
|
||||||
|
Ok(fixture)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn command(&self) -> Command {
|
||||||
|
let mut command = self.cli_test.command();
|
||||||
|
command.arg("analyze").arg("graph").arg("--preview");
|
||||||
|
command
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::Deref for AnalyzeTest {
|
||||||
|
type Target = CliTest;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.cli_test
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
|
mod analyze_graph;
|
||||||
mod format;
|
mod format;
|
||||||
mod lint;
|
mod lint;
|
||||||
|
|
||||||
@@ -62,9 +63,7 @@ impl CliTest {
|
|||||||
files: impl IntoIterator<Item = (&'a str, &'a str)>,
|
files: impl IntoIterator<Item = (&'a str, &'a str)>,
|
||||||
) -> anyhow::Result<Self> {
|
) -> anyhow::Result<Self> {
|
||||||
let case = Self::new()?;
|
let case = Self::new()?;
|
||||||
for file in files {
|
case.write_files(files)?;
|
||||||
case.write_file(file.0, file.1)?;
|
|
||||||
}
|
|
||||||
Ok(case)
|
Ok(case)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,6 +152,16 @@ impl CliTest {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn write_files<'a>(
|
||||||
|
&self,
|
||||||
|
files: impl IntoIterator<Item = (&'a str, &'a str)>,
|
||||||
|
) -> Result<()> {
|
||||||
|
for file in files {
|
||||||
|
self.write_file(file.0, file.1)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the path to the test directory root.
|
/// Returns the path to the test directory root.
|
||||||
pub(crate) fn root(&self) -> &Path {
|
pub(crate) fn root(&self) -> &Path {
|
||||||
&self.project_dir
|
&self.project_dir
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ info:
|
|||||||
- concise
|
- concise
|
||||||
- "--show-settings"
|
- "--show-settings"
|
||||||
- test.py
|
- test.py
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
success: true
|
success: true
|
||||||
exit_code: 0
|
exit_code: 0
|
||||||
@@ -284,5 +283,6 @@ analyze.target_version = 3.10
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ info:
|
|||||||
- UP007
|
- UP007
|
||||||
- test.py
|
- test.py
|
||||||
- "-"
|
- "-"
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
success: true
|
success: true
|
||||||
exit_code: 0
|
exit_code: 0
|
||||||
@@ -286,5 +285,6 @@ analyze.target_version = 3.11
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ info:
|
|||||||
- UP007
|
- UP007
|
||||||
- test.py
|
- test.py
|
||||||
- "-"
|
- "-"
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
success: true
|
success: true
|
||||||
exit_code: 0
|
exit_code: 0
|
||||||
@@ -288,5 +287,6 @@ analyze.target_version = 3.11
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ info:
|
|||||||
- py310
|
- py310
|
||||||
- test.py
|
- test.py
|
||||||
- "-"
|
- "-"
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
success: true
|
success: true
|
||||||
exit_code: 0
|
exit_code: 0
|
||||||
@@ -288,5 +287,6 @@ analyze.target_version = 3.10
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ info:
|
|||||||
- "--select"
|
- "--select"
|
||||||
- UP007
|
- UP007
|
||||||
- foo/test.py
|
- foo/test.py
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
success: true
|
success: true
|
||||||
exit_code: 0
|
exit_code: 0
|
||||||
@@ -285,5 +284,6 @@ analyze.target_version = 3.11
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ info:
|
|||||||
- "--select"
|
- "--select"
|
||||||
- UP007
|
- UP007
|
||||||
- foo/test.py
|
- foo/test.py
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
success: true
|
success: true
|
||||||
exit_code: 0
|
exit_code: 0
|
||||||
@@ -285,5 +284,6 @@ analyze.target_version = 3.10
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -283,5 +283,6 @@ analyze.target_version = 3.10
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -283,5 +283,6 @@ analyze.target_version = 3.10
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ info:
|
|||||||
- concise
|
- concise
|
||||||
- test.py
|
- test.py
|
||||||
- "--show-settings"
|
- "--show-settings"
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
success: true
|
success: true
|
||||||
exit_code: 0
|
exit_code: 0
|
||||||
@@ -284,5 +283,6 @@ analyze.target_version = 3.11
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -396,5 +396,6 @@ analyze.target_version = 3.7
|
|||||||
analyze.string_imports = disabled
|
analyze.string_imports = disabled
|
||||||
analyze.extension = ExtensionMapping({})
|
analyze.extension = ExtensionMapping({})
|
||||||
analyze.include_dependencies = {}
|
analyze.include_dependencies = {}
|
||||||
|
analyze.type_checking_imports = true
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
//! styling.
|
//! styling.
|
||||||
//!
|
//!
|
||||||
//! The above snippet has been built out of the following structure:
|
//! The above snippet has been built out of the following structure:
|
||||||
use crate::snippet;
|
use crate::{Id, snippet};
|
||||||
use std::cmp::{Reverse, max, min};
|
use std::cmp::{Reverse, max, min};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
@@ -189,6 +189,7 @@ impl DisplaySet<'_> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_annotation(
|
fn format_annotation(
|
||||||
&self,
|
&self,
|
||||||
line_offset: usize,
|
line_offset: usize,
|
||||||
@@ -199,11 +200,13 @@ impl DisplaySet<'_> {
|
|||||||
) -> fmt::Result {
|
) -> fmt::Result {
|
||||||
let hide_severity = annotation.annotation_type.is_none();
|
let hide_severity = annotation.annotation_type.is_none();
|
||||||
let color = get_annotation_style(&annotation.annotation_type, stylesheet);
|
let color = get_annotation_style(&annotation.annotation_type, stylesheet);
|
||||||
|
|
||||||
let formatted_len = if let Some(id) = &annotation.id {
|
let formatted_len = if let Some(id) = &annotation.id {
|
||||||
|
let id_len = id.id.len();
|
||||||
if hide_severity {
|
if hide_severity {
|
||||||
id.len()
|
id_len
|
||||||
} else {
|
} else {
|
||||||
2 + id.len() + annotation_type_len(&annotation.annotation_type)
|
2 + id_len + annotation_type_len(&annotation.annotation_type)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
annotation_type_len(&annotation.annotation_type)
|
annotation_type_len(&annotation.annotation_type)
|
||||||
@@ -256,9 +259,20 @@ impl DisplaySet<'_> {
|
|||||||
let annotation_type = annotation_type_str(&annotation.annotation_type);
|
let annotation_type = annotation_type_str(&annotation.annotation_type);
|
||||||
if let Some(id) = annotation.id {
|
if let Some(id) = annotation.id {
|
||||||
if hide_severity {
|
if hide_severity {
|
||||||
buffer.append(line_offset, &format!("{id} "), *stylesheet.error());
|
buffer.append(
|
||||||
|
line_offset,
|
||||||
|
&format!("{id} ", id = fmt_with_hyperlink(id.id, id.url, stylesheet)),
|
||||||
|
*stylesheet.error(),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
buffer.append(line_offset, &format!("{annotation_type}[{id}]"), *color);
|
buffer.append(
|
||||||
|
line_offset,
|
||||||
|
&format!(
|
||||||
|
"{annotation_type}[{id}]",
|
||||||
|
id = fmt_with_hyperlink(id.id, id.url, stylesheet)
|
||||||
|
),
|
||||||
|
*color,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
buffer.append(line_offset, annotation_type, *color);
|
buffer.append(line_offset, annotation_type, *color);
|
||||||
@@ -707,7 +721,7 @@ impl DisplaySet<'_> {
|
|||||||
let style =
|
let style =
|
||||||
get_annotation_style(&annotation.annotation_type, stylesheet);
|
get_annotation_style(&annotation.annotation_type, stylesheet);
|
||||||
let mut formatted_len = if let Some(id) = &annotation.annotation.id {
|
let mut formatted_len = if let Some(id) = &annotation.annotation.id {
|
||||||
2 + id.len()
|
2 + id.id.len()
|
||||||
+ annotation_type_len(&annotation.annotation.annotation_type)
|
+ annotation_type_len(&annotation.annotation.annotation_type)
|
||||||
} else {
|
} else {
|
||||||
annotation_type_len(&annotation.annotation.annotation_type)
|
annotation_type_len(&annotation.annotation.annotation_type)
|
||||||
@@ -724,7 +738,10 @@ impl DisplaySet<'_> {
|
|||||||
} else if formatted_len != 0 {
|
} else if formatted_len != 0 {
|
||||||
formatted_len += 2;
|
formatted_len += 2;
|
||||||
let id = match &annotation.annotation.id {
|
let id = match &annotation.annotation.id {
|
||||||
Some(id) => format!("[{id}]"),
|
Some(id) => format!(
|
||||||
|
"[{id}]",
|
||||||
|
id = fmt_with_hyperlink(&id.id, id.url, stylesheet)
|
||||||
|
),
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
};
|
};
|
||||||
buffer.puts(
|
buffer.puts(
|
||||||
@@ -827,7 +844,7 @@ impl DisplaySet<'_> {
|
|||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub(crate) struct Annotation<'a> {
|
pub(crate) struct Annotation<'a> {
|
||||||
pub(crate) annotation_type: DisplayAnnotationType,
|
pub(crate) annotation_type: DisplayAnnotationType,
|
||||||
pub(crate) id: Option<&'a str>,
|
pub(crate) id: Option<Id<'a>>,
|
||||||
pub(crate) label: Vec<DisplayTextFragment<'a>>,
|
pub(crate) label: Vec<DisplayTextFragment<'a>>,
|
||||||
pub(crate) is_fixable: bool,
|
pub(crate) is_fixable: bool,
|
||||||
}
|
}
|
||||||
@@ -1140,7 +1157,7 @@ fn format_message<'m>(
|
|||||||
|
|
||||||
fn format_title<'a>(
|
fn format_title<'a>(
|
||||||
level: crate::Level,
|
level: crate::Level,
|
||||||
id: Option<&'a str>,
|
id: Option<Id<'a>>,
|
||||||
label: &'a str,
|
label: &'a str,
|
||||||
is_fixable: bool,
|
is_fixable: bool,
|
||||||
) -> DisplayLine<'a> {
|
) -> DisplayLine<'a> {
|
||||||
@@ -1158,7 +1175,7 @@ fn format_title<'a>(
|
|||||||
|
|
||||||
fn format_footer<'a>(
|
fn format_footer<'a>(
|
||||||
level: crate::Level,
|
level: crate::Level,
|
||||||
id: Option<&'a str>,
|
id: Option<Id<'a>>,
|
||||||
label: &'a str,
|
label: &'a str,
|
||||||
) -> Vec<DisplayLine<'a>> {
|
) -> Vec<DisplayLine<'a>> {
|
||||||
let mut result = vec![];
|
let mut result = vec![];
|
||||||
@@ -1706,6 +1723,7 @@ fn format_body<'m>(
|
|||||||
annotation: Annotation {
|
annotation: Annotation {
|
||||||
annotation_type,
|
annotation_type,
|
||||||
id: None,
|
id: None,
|
||||||
|
|
||||||
label: format_label(annotation.label, None),
|
label: format_label(annotation.label, None),
|
||||||
is_fixable: false,
|
is_fixable: false,
|
||||||
},
|
},
|
||||||
@@ -1887,3 +1905,40 @@ fn char_width(c: char) -> Option<usize> {
|
|||||||
unicode_width::UnicodeWidthChar::width(c)
|
unicode_width::UnicodeWidthChar::width(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn fmt_with_hyperlink<'a, T>(
|
||||||
|
content: T,
|
||||||
|
url: Option<&'a str>,
|
||||||
|
stylesheet: &Stylesheet,
|
||||||
|
) -> impl std::fmt::Display + 'a
|
||||||
|
where
|
||||||
|
T: std::fmt::Display + 'a,
|
||||||
|
{
|
||||||
|
struct FmtHyperlink<'a, T> {
|
||||||
|
content: T,
|
||||||
|
url: Option<&'a str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> std::fmt::Display for FmtHyperlink<'_, T>
|
||||||
|
where
|
||||||
|
T: std::fmt::Display,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
if let Some(url) = self.url {
|
||||||
|
write!(f, "\x1B]8;;{url}\x1B\\")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.content.fmt(f)?;
|
||||||
|
|
||||||
|
if self.url.is_some() {
|
||||||
|
f.write_str("\x1B]8;;\x1B\\")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let url = if stylesheet.hyperlink { url } else { None };
|
||||||
|
|
||||||
|
FmtHyperlink { content, url }
|
||||||
|
}
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ impl Renderer {
|
|||||||
}
|
}
|
||||||
.effects(Effects::BOLD),
|
.effects(Effects::BOLD),
|
||||||
none: Style::new(),
|
none: Style::new(),
|
||||||
|
hyperlink: true,
|
||||||
},
|
},
|
||||||
..Self::plain()
|
..Self::plain()
|
||||||
}
|
}
|
||||||
@@ -154,6 +155,11 @@ impl Renderer {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn hyperlink(mut self, hyperlink: bool) -> Self {
|
||||||
|
self.stylesheet.hyperlink = hyperlink;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set the string used for when a long line is cut.
|
/// Set the string used for when a long line is cut.
|
||||||
///
|
///
|
||||||
/// The default is `...` (three `U+002E` characters).
|
/// The default is `...` (three `U+002E` characters).
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ pub(crate) struct Stylesheet {
|
|||||||
pub(crate) line_no: Style,
|
pub(crate) line_no: Style,
|
||||||
pub(crate) emphasis: Style,
|
pub(crate) emphasis: Style,
|
||||||
pub(crate) none: Style,
|
pub(crate) none: Style,
|
||||||
|
pub(crate) hyperlink: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Stylesheet {
|
impl Default for Stylesheet {
|
||||||
@@ -29,6 +30,7 @@ impl Stylesheet {
|
|||||||
line_no: Style::new(),
|
line_no: Style::new(),
|
||||||
emphasis: Style::new(),
|
emphasis: Style::new(),
|
||||||
none: Style::new(),
|
none: Style::new(),
|
||||||
|
hyperlink: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,13 +12,19 @@
|
|||||||
|
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||||
|
pub(crate) struct Id<'a> {
|
||||||
|
pub(crate) id: &'a str,
|
||||||
|
pub(crate) url: Option<&'a str>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Primary structure provided for formatting
|
/// Primary structure provided for formatting
|
||||||
///
|
///
|
||||||
/// See [`Level::title`] to create a [`Message`]
|
/// See [`Level::title`] to create a [`Message`]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Message<'a> {
|
pub struct Message<'a> {
|
||||||
pub(crate) level: Level,
|
pub(crate) level: Level,
|
||||||
pub(crate) id: Option<&'a str>,
|
pub(crate) id: Option<Id<'a>>,
|
||||||
pub(crate) title: &'a str,
|
pub(crate) title: &'a str,
|
||||||
pub(crate) snippets: Vec<Snippet<'a>>,
|
pub(crate) snippets: Vec<Snippet<'a>>,
|
||||||
pub(crate) footer: Vec<Message<'a>>,
|
pub(crate) footer: Vec<Message<'a>>,
|
||||||
@@ -28,7 +34,12 @@ pub struct Message<'a> {
|
|||||||
|
|
||||||
impl<'a> Message<'a> {
|
impl<'a> Message<'a> {
|
||||||
pub fn id(mut self, id: &'a str) -> Self {
|
pub fn id(mut self, id: &'a str) -> Self {
|
||||||
self.id = Some(id);
|
self.id = Some(Id { id, url: None });
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn id_with_url(mut self, id: &'a str, url: Option<&'a str>) -> Self {
|
||||||
|
self.id = Some(Id { id, url });
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -667,7 +667,7 @@ fn attrs(criterion: &mut Criterion) {
|
|||||||
max_dep_date: "2025-06-17",
|
max_dep_date: "2025-06-17",
|
||||||
python_version: PythonVersion::PY313,
|
python_version: PythonVersion::PY313,
|
||||||
},
|
},
|
||||||
110,
|
120,
|
||||||
);
|
);
|
||||||
|
|
||||||
bench_project(&benchmark, criterion);
|
bench_project(&benchmark, criterion);
|
||||||
|
|||||||
@@ -71,16 +71,13 @@ impl Display for Benchmark<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_project(db: &ProjectDatabase, max_diagnostics: usize) {
|
fn check_project(db: &ProjectDatabase, project_name: &str, max_diagnostics: usize) {
|
||||||
let result = db.check();
|
let result = db.check();
|
||||||
let diagnostics = result.len();
|
let diagnostics = result.len();
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
diagnostics > 1 && diagnostics <= max_diagnostics,
|
diagnostics > 1 && diagnostics <= max_diagnostics,
|
||||||
"Expected between {} and {} diagnostics but got {}",
|
"Expected between 1 and {max_diagnostics} diagnostics on project '{project_name}' but got {diagnostics}",
|
||||||
1,
|
|
||||||
max_diagnostics,
|
|
||||||
diagnostics
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +143,7 @@ static FREQTRADE: Benchmark = Benchmark::new(
|
|||||||
max_dep_date: "2025-06-17",
|
max_dep_date: "2025-06-17",
|
||||||
python_version: PythonVersion::PY312,
|
python_version: PythonVersion::PY312,
|
||||||
},
|
},
|
||||||
525,
|
600,
|
||||||
);
|
);
|
||||||
|
|
||||||
static PANDAS: Benchmark = Benchmark::new(
|
static PANDAS: Benchmark = Benchmark::new(
|
||||||
@@ -166,7 +163,7 @@ static PANDAS: Benchmark = Benchmark::new(
|
|||||||
max_dep_date: "2025-06-17",
|
max_dep_date: "2025-06-17",
|
||||||
python_version: PythonVersion::PY312,
|
python_version: PythonVersion::PY312,
|
||||||
},
|
},
|
||||||
3000,
|
4000,
|
||||||
);
|
);
|
||||||
|
|
||||||
static PYDANTIC: Benchmark = Benchmark::new(
|
static PYDANTIC: Benchmark = Benchmark::new(
|
||||||
@@ -184,7 +181,7 @@ static PYDANTIC: Benchmark = Benchmark::new(
|
|||||||
max_dep_date: "2025-06-17",
|
max_dep_date: "2025-06-17",
|
||||||
python_version: PythonVersion::PY39,
|
python_version: PythonVersion::PY39,
|
||||||
},
|
},
|
||||||
1000,
|
7000,
|
||||||
);
|
);
|
||||||
|
|
||||||
static SYMPY: Benchmark = Benchmark::new(
|
static SYMPY: Benchmark = Benchmark::new(
|
||||||
@@ -226,7 +223,7 @@ static STATIC_FRAME: Benchmark = Benchmark::new(
|
|||||||
max_dep_date: "2025-08-09",
|
max_dep_date: "2025-08-09",
|
||||||
python_version: PythonVersion::PY311,
|
python_version: PythonVersion::PY311,
|
||||||
},
|
},
|
||||||
800,
|
900,
|
||||||
);
|
);
|
||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
@@ -234,11 +231,11 @@ fn run_single_threaded(bencher: Bencher, benchmark: &Benchmark) {
|
|||||||
bencher
|
bencher
|
||||||
.with_inputs(|| benchmark.setup_iteration())
|
.with_inputs(|| benchmark.setup_iteration())
|
||||||
.bench_local_refs(|db| {
|
.bench_local_refs(|db| {
|
||||||
check_project(db, benchmark.max_diagnostics);
|
check_project(db, benchmark.project.name, benchmark.max_diagnostics);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench(args=[&ALTAIR, &FREQTRADE, &PYDANTIC, &TANJUN], sample_size=2, sample_count=3)]
|
#[bench(args=[&ALTAIR, &FREQTRADE, &TANJUN], sample_size=2, sample_count=3)]
|
||||||
fn small(bencher: Bencher, benchmark: &Benchmark) {
|
fn small(bencher: Bencher, benchmark: &Benchmark) {
|
||||||
run_single_threaded(bencher, benchmark);
|
run_single_threaded(bencher, benchmark);
|
||||||
}
|
}
|
||||||
@@ -248,12 +245,12 @@ fn medium(bencher: Bencher, benchmark: &Benchmark) {
|
|||||||
run_single_threaded(bencher, benchmark);
|
run_single_threaded(bencher, benchmark);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench(args=[&SYMPY], sample_size=1, sample_count=2)]
|
#[bench(args=[&SYMPY, &PYDANTIC], sample_size=1, sample_count=2)]
|
||||||
fn large(bencher: Bencher, benchmark: &Benchmark) {
|
fn large(bencher: Bencher, benchmark: &Benchmark) {
|
||||||
run_single_threaded(bencher, benchmark);
|
run_single_threaded(bencher, benchmark);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench(args=[&PYDANTIC], sample_size=3, sample_count=8)]
|
#[bench(args=[&ALTAIR], sample_size=3, sample_count=8)]
|
||||||
fn multithreaded(bencher: Bencher, benchmark: &Benchmark) {
|
fn multithreaded(bencher: Bencher, benchmark: &Benchmark) {
|
||||||
let thread_pool = ThreadPoolBuilder::new().build().unwrap();
|
let thread_pool = ThreadPoolBuilder::new().build().unwrap();
|
||||||
|
|
||||||
@@ -261,7 +258,7 @@ fn multithreaded(bencher: Bencher, benchmark: &Benchmark) {
|
|||||||
.with_inputs(|| benchmark.setup_iteration())
|
.with_inputs(|| benchmark.setup_iteration())
|
||||||
.bench_local_values(|db| {
|
.bench_local_values(|db| {
|
||||||
thread_pool.install(|| {
|
thread_pool.install(|| {
|
||||||
check_project(&db, benchmark.max_diagnostics);
|
check_project(&db, benchmark.project.name, benchmark.max_diagnostics);
|
||||||
db
|
db
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@@ -285,7 +282,7 @@ fn main() {
|
|||||||
// branch when looking up the ingredient index.
|
// branch when looking up the ingredient index.
|
||||||
{
|
{
|
||||||
let db = TANJUN.setup_iteration();
|
let db = TANJUN.setup_iteration();
|
||||||
check_project(&db, TANJUN.max_diagnostics);
|
check_project(&db, TANJUN.project.name, TANJUN.max_diagnostics);
|
||||||
}
|
}
|
||||||
|
|
||||||
divan::main();
|
divan::main();
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ schemars = { workspace = true, optional = true }
|
|||||||
serde = { workspace = true, optional = true }
|
serde = { workspace = true, optional = true }
|
||||||
serde_json = { workspace = true, optional = true }
|
serde_json = { workspace = true, optional = true }
|
||||||
similar = { workspace = true }
|
similar = { workspace = true }
|
||||||
|
supports-hyperlinks = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
tracing-subscriber = { workspace = true, optional = true }
|
tracing-subscriber = { workspace = true, optional = true }
|
||||||
|
|||||||
@@ -64,6 +64,8 @@ impl Diagnostic {
|
|||||||
id,
|
id,
|
||||||
severity,
|
severity,
|
||||||
message: message.into_diagnostic_message(),
|
message: message.into_diagnostic_message(),
|
||||||
|
custom_concise_message: None,
|
||||||
|
documentation_url: None,
|
||||||
annotations: vec![],
|
annotations: vec![],
|
||||||
subs: vec![],
|
subs: vec![],
|
||||||
fix: None,
|
fix: None,
|
||||||
@@ -213,6 +215,10 @@ impl Diagnostic {
|
|||||||
/// cases, just converting it to a string (or printing it) will do what
|
/// cases, just converting it to a string (or printing it) will do what
|
||||||
/// you want.
|
/// you want.
|
||||||
pub fn concise_message(&self) -> ConciseMessage<'_> {
|
pub fn concise_message(&self) -> ConciseMessage<'_> {
|
||||||
|
if let Some(custom_message) = &self.inner.custom_concise_message {
|
||||||
|
return ConciseMessage::Custom(custom_message.as_str());
|
||||||
|
}
|
||||||
|
|
||||||
let main = self.inner.message.as_str();
|
let main = self.inner.message.as_str();
|
||||||
let annotation = self
|
let annotation = self
|
||||||
.primary_annotation()
|
.primary_annotation()
|
||||||
@@ -226,6 +232,15 @@ impl Diagnostic {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set a custom message for the concise formatting of this diagnostic.
|
||||||
|
///
|
||||||
|
/// This overrides the default behavior of generating a concise message
|
||||||
|
/// from the main diagnostic message and the primary annotation.
|
||||||
|
pub fn set_concise_message(&mut self, message: impl IntoDiagnosticMessage) {
|
||||||
|
Arc::make_mut(&mut self.inner).custom_concise_message =
|
||||||
|
Some(message.into_diagnostic_message());
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the severity of this diagnostic.
|
/// Returns the severity of this diagnostic.
|
||||||
///
|
///
|
||||||
/// Note that this may be different than the severity of sub-diagnostics.
|
/// Note that this may be different than the severity of sub-diagnostics.
|
||||||
@@ -356,6 +371,14 @@ impl Diagnostic {
|
|||||||
.is_some_and(|fix| fix.applies(config.fix_applicability))
|
.is_some_and(|fix| fix.applies(config.fix_applicability))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn documentation_url(&self) -> Option<&str> {
|
||||||
|
self.inner.documentation_url.as_deref()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_documentation_url(&mut self, url: Option<String>) {
|
||||||
|
Arc::make_mut(&mut self.inner).documentation_url = url;
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the offset of the parent statement for this diagnostic if it exists.
|
/// Returns the offset of the parent statement for this diagnostic if it exists.
|
||||||
///
|
///
|
||||||
/// This is primarily used for checking noqa/secondary code suppressions.
|
/// This is primarily used for checking noqa/secondary code suppressions.
|
||||||
@@ -429,28 +452,6 @@ impl Diagnostic {
|
|||||||
.map(|sub| sub.inner.message.as_str())
|
.map(|sub| sub.inner.message.as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the URL for the rule documentation, if it exists.
|
|
||||||
pub fn to_ruff_url(&self) -> Option<String> {
|
|
||||||
match self.id() {
|
|
||||||
DiagnosticId::Panic
|
|
||||||
| DiagnosticId::Io
|
|
||||||
| DiagnosticId::InvalidSyntax
|
|
||||||
| DiagnosticId::RevealedType
|
|
||||||
| DiagnosticId::UnknownRule
|
|
||||||
| DiagnosticId::InvalidGlob
|
|
||||||
| DiagnosticId::EmptyInclude
|
|
||||||
| DiagnosticId::UnnecessaryOverridesSection
|
|
||||||
| DiagnosticId::UselessOverridesSection
|
|
||||||
| DiagnosticId::DeprecatedSetting
|
|
||||||
| DiagnosticId::Unformatted
|
|
||||||
| DiagnosticId::InvalidCliOption
|
|
||||||
| DiagnosticId::InternalError => None,
|
|
||||||
DiagnosticId::Lint(lint_name) => {
|
|
||||||
Some(format!("{}/rules/{lint_name}", env!("CARGO_PKG_HOMEPAGE")))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the filename for the message.
|
/// Returns the filename for the message.
|
||||||
///
|
///
|
||||||
/// Panics if the diagnostic has no primary span, or if its file is not a `SourceFile`.
|
/// Panics if the diagnostic has no primary span, or if its file is not a `SourceFile`.
|
||||||
@@ -530,8 +531,10 @@ impl Diagnostic {
|
|||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, get_size2::GetSize)]
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, get_size2::GetSize)]
|
||||||
struct DiagnosticInner {
|
struct DiagnosticInner {
|
||||||
id: DiagnosticId,
|
id: DiagnosticId,
|
||||||
|
documentation_url: Option<String>,
|
||||||
severity: Severity,
|
severity: Severity,
|
||||||
message: DiagnosticMessage,
|
message: DiagnosticMessage,
|
||||||
|
custom_concise_message: Option<DiagnosticMessage>,
|
||||||
annotations: Vec<Annotation>,
|
annotations: Vec<Annotation>,
|
||||||
subs: Vec<SubDiagnostic>,
|
subs: Vec<SubDiagnostic>,
|
||||||
fix: Option<Fix>,
|
fix: Option<Fix>,
|
||||||
@@ -1520,6 +1523,8 @@ pub enum ConciseMessage<'a> {
|
|||||||
/// This indicates that the diagnostic is probably using the old
|
/// This indicates that the diagnostic is probably using the old
|
||||||
/// model.
|
/// model.
|
||||||
Empty,
|
Empty,
|
||||||
|
/// A custom concise message has been provided.
|
||||||
|
Custom(&'a str),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for ConciseMessage<'_> {
|
impl std::fmt::Display for ConciseMessage<'_> {
|
||||||
@@ -1535,6 +1540,9 @@ impl std::fmt::Display for ConciseMessage<'_> {
|
|||||||
write!(f, "{main}: {annotation}")
|
write!(f, "{main}: {annotation}")
|
||||||
}
|
}
|
||||||
ConciseMessage::Empty => Ok(()),
|
ConciseMessage::Empty => Ok(()),
|
||||||
|
ConciseMessage::Custom(message) => {
|
||||||
|
write!(f, "{message}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -205,6 +205,7 @@ impl<'a> Resolved<'a> {
|
|||||||
struct ResolvedDiagnostic<'a> {
|
struct ResolvedDiagnostic<'a> {
|
||||||
level: AnnotateLevel,
|
level: AnnotateLevel,
|
||||||
id: Option<String>,
|
id: Option<String>,
|
||||||
|
documentation_url: Option<String>,
|
||||||
message: String,
|
message: String,
|
||||||
annotations: Vec<ResolvedAnnotation<'a>>,
|
annotations: Vec<ResolvedAnnotation<'a>>,
|
||||||
is_fixable: bool,
|
is_fixable: bool,
|
||||||
@@ -240,12 +241,12 @@ impl<'a> ResolvedDiagnostic<'a> {
|
|||||||
// `DisplaySet::format_annotation` for both cases, but this is a small hack to improve
|
// `DisplaySet::format_annotation` for both cases, but this is a small hack to improve
|
||||||
// the formatting of syntax errors for now. This should also be kept consistent with the
|
// the formatting of syntax errors for now. This should also be kept consistent with the
|
||||||
// concise formatting.
|
// concise formatting.
|
||||||
Some(diag.secondary_code().map_or_else(
|
diag.secondary_code().map_or_else(
|
||||||
|| format!("{id}:", id = diag.inner.id),
|
|| format!("{id}:", id = diag.inner.id),
|
||||||
|code| code.to_string(),
|
|code| code.to_string(),
|
||||||
))
|
)
|
||||||
} else {
|
} else {
|
||||||
Some(diag.inner.id.to_string())
|
diag.inner.id.to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let level = if config.hide_severity {
|
let level = if config.hide_severity {
|
||||||
@@ -256,7 +257,8 @@ impl<'a> ResolvedDiagnostic<'a> {
|
|||||||
|
|
||||||
ResolvedDiagnostic {
|
ResolvedDiagnostic {
|
||||||
level,
|
level,
|
||||||
id,
|
id: Some(id),
|
||||||
|
documentation_url: diag.documentation_url().map(ToString::to_string),
|
||||||
message: diag.inner.message.as_str().to_string(),
|
message: diag.inner.message.as_str().to_string(),
|
||||||
annotations,
|
annotations,
|
||||||
is_fixable: config.show_fix_status && diag.has_applicable_fix(config),
|
is_fixable: config.show_fix_status && diag.has_applicable_fix(config),
|
||||||
@@ -287,6 +289,7 @@ impl<'a> ResolvedDiagnostic<'a> {
|
|||||||
ResolvedDiagnostic {
|
ResolvedDiagnostic {
|
||||||
level: diag.inner.severity.to_annotate(),
|
level: diag.inner.severity.to_annotate(),
|
||||||
id: None,
|
id: None,
|
||||||
|
documentation_url: None,
|
||||||
message: diag.inner.message.as_str().to_string(),
|
message: diag.inner.message.as_str().to_string(),
|
||||||
annotations,
|
annotations,
|
||||||
is_fixable: false,
|
is_fixable: false,
|
||||||
@@ -385,6 +388,7 @@ impl<'a> ResolvedDiagnostic<'a> {
|
|||||||
RenderableDiagnostic {
|
RenderableDiagnostic {
|
||||||
level: self.level,
|
level: self.level,
|
||||||
id: self.id.as_deref(),
|
id: self.id.as_deref(),
|
||||||
|
documentation_url: self.documentation_url.as_deref(),
|
||||||
message: &self.message,
|
message: &self.message,
|
||||||
snippets_by_input,
|
snippets_by_input,
|
||||||
is_fixable: self.is_fixable,
|
is_fixable: self.is_fixable,
|
||||||
@@ -485,6 +489,7 @@ struct RenderableDiagnostic<'r> {
|
|||||||
/// An ID is always present for top-level diagnostics and always absent for
|
/// An ID is always present for top-level diagnostics and always absent for
|
||||||
/// sub-diagnostics.
|
/// sub-diagnostics.
|
||||||
id: Option<&'r str>,
|
id: Option<&'r str>,
|
||||||
|
documentation_url: Option<&'r str>,
|
||||||
/// The message emitted with the diagnostic, before any snippets are
|
/// The message emitted with the diagnostic, before any snippets are
|
||||||
/// rendered.
|
/// rendered.
|
||||||
message: &'r str,
|
message: &'r str,
|
||||||
@@ -519,7 +524,7 @@ impl RenderableDiagnostic<'_> {
|
|||||||
.is_fixable(self.is_fixable)
|
.is_fixable(self.is_fixable)
|
||||||
.lineno_offset(self.header_offset);
|
.lineno_offset(self.header_offset);
|
||||||
if let Some(id) = self.id {
|
if let Some(id) = self.id {
|
||||||
message = message.id(id);
|
message = message.id_with_url(id, self.documentation_url);
|
||||||
}
|
}
|
||||||
message.snippets(snippets)
|
message.snippets(snippets)
|
||||||
}
|
}
|
||||||
@@ -2876,6 +2881,12 @@ watermelon
|
|||||||
self.diag.help(message);
|
self.diag.help(message);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the documentation URL for the diagnostic.
|
||||||
|
pub(super) fn documentation_url(mut self, url: impl Into<String>) -> DiagnosticBuilder<'e> {
|
||||||
|
self.diag.set_documentation_url(Some(url.into()));
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A helper builder for tersely populating a `SubDiagnostic`.
|
/// A helper builder for tersely populating a `SubDiagnostic`.
|
||||||
@@ -2990,6 +3001,7 @@ def fibonacci(n):
|
|||||||
TextSize::from(10),
|
TextSize::from(10),
|
||||||
))))
|
))))
|
||||||
.noqa_offset(TextSize::from(7))
|
.noqa_offset(TextSize::from(7))
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/unused-import")
|
||||||
.build(),
|
.build(),
|
||||||
env.builder(
|
env.builder(
|
||||||
"unused-variable",
|
"unused-variable",
|
||||||
@@ -3004,11 +3016,13 @@ def fibonacci(n):
|
|||||||
TextSize::from(99),
|
TextSize::from(99),
|
||||||
)))
|
)))
|
||||||
.noqa_offset(TextSize::from(94))
|
.noqa_offset(TextSize::from(94))
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/unused-variable")
|
||||||
.build(),
|
.build(),
|
||||||
env.builder("undefined-name", Severity::Error, "Undefined name `a`")
|
env.builder("undefined-name", Severity::Error, "Undefined name `a`")
|
||||||
.primary("undef.py", "1:3", "1:4", "")
|
.primary("undef.py", "1:3", "1:4", "")
|
||||||
.secondary_code("F821")
|
.secondary_code("F821")
|
||||||
.noqa_offset(TextSize::from(3))
|
.noqa_offset(TextSize::from(3))
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/undefined-name")
|
||||||
.build(),
|
.build(),
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -3123,6 +3137,7 @@ if call(foo
|
|||||||
TextSize::from(19),
|
TextSize::from(19),
|
||||||
))))
|
))))
|
||||||
.noqa_offset(TextSize::from(16))
|
.noqa_offset(TextSize::from(16))
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/unused-import")
|
||||||
.build(),
|
.build(),
|
||||||
env.builder(
|
env.builder(
|
||||||
"unused-import",
|
"unused-import",
|
||||||
@@ -3137,6 +3152,7 @@ if call(foo
|
|||||||
TextSize::from(40),
|
TextSize::from(40),
|
||||||
))))
|
))))
|
||||||
.noqa_offset(TextSize::from(35))
|
.noqa_offset(TextSize::from(35))
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/unused-import")
|
||||||
.build(),
|
.build(),
|
||||||
env.builder(
|
env.builder(
|
||||||
"unused-variable",
|
"unused-variable",
|
||||||
@@ -3151,6 +3167,7 @@ if call(foo
|
|||||||
TextSize::from(104),
|
TextSize::from(104),
|
||||||
))))
|
))))
|
||||||
.noqa_offset(TextSize::from(98))
|
.noqa_offset(TextSize::from(98))
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/unused-variable")
|
||||||
.build(),
|
.build(),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::diagnostic::{
|
use crate::diagnostic::{
|
||||||
Diagnostic, DisplayDiagnosticConfig, Severity,
|
Diagnostic, DisplayDiagnosticConfig, Severity,
|
||||||
stylesheet::{DiagnosticStylesheet, fmt_styled},
|
stylesheet::{DiagnosticStylesheet, fmt_styled, fmt_with_hyperlink},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::FileResolver;
|
use super::FileResolver;
|
||||||
@@ -62,18 +62,29 @@ impl<'a> ConciseRenderer<'a> {
|
|||||||
}
|
}
|
||||||
write!(f, "{sep} ")?;
|
write!(f, "{sep} ")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.config.hide_severity {
|
if self.config.hide_severity {
|
||||||
if let Some(code) = diag.secondary_code() {
|
if let Some(code) = diag.secondary_code() {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{code} ",
|
"{code} ",
|
||||||
code = fmt_styled(code, stylesheet.secondary_code)
|
code = fmt_styled(
|
||||||
|
fmt_with_hyperlink(&code, diag.documentation_url(), &stylesheet),
|
||||||
|
stylesheet.secondary_code
|
||||||
|
)
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{id}: ",
|
"{id}: ",
|
||||||
id = fmt_styled(diag.inner.id.as_str(), stylesheet.secondary_code)
|
id = fmt_styled(
|
||||||
|
fmt_with_hyperlink(
|
||||||
|
&diag.inner.id,
|
||||||
|
diag.documentation_url(),
|
||||||
|
&stylesheet
|
||||||
|
),
|
||||||
|
stylesheet.secondary_code
|
||||||
|
)
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
if self.config.show_fix_status {
|
if self.config.show_fix_status {
|
||||||
@@ -93,7 +104,10 @@ impl<'a> ConciseRenderer<'a> {
|
|||||||
f,
|
f,
|
||||||
"{severity}[{id}] ",
|
"{severity}[{id}] ",
|
||||||
severity = fmt_styled(severity, severity_style),
|
severity = fmt_styled(severity, severity_style),
|
||||||
id = fmt_styled(diag.id(), stylesheet.emphasis)
|
id = fmt_styled(
|
||||||
|
fmt_with_hyperlink(&diag.id(), diag.documentation_url(), &stylesheet),
|
||||||
|
stylesheet.emphasis
|
||||||
|
)
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,8 @@ impl<'a> FullRenderer<'a> {
|
|||||||
.help(stylesheet.help)
|
.help(stylesheet.help)
|
||||||
.line_no(stylesheet.line_no)
|
.line_no(stylesheet.line_no)
|
||||||
.emphasis(stylesheet.emphasis)
|
.emphasis(stylesheet.emphasis)
|
||||||
.none(stylesheet.none);
|
.none(stylesheet.none)
|
||||||
|
.hyperlink(stylesheet.hyperlink);
|
||||||
|
|
||||||
for diag in diagnostics {
|
for diag in diagnostics {
|
||||||
let resolved = Resolved::new(self.resolver, diag, self.config);
|
let resolved = Resolved::new(self.resolver, diag, self.config);
|
||||||
@@ -703,52 +704,7 @@ print()
|
|||||||
env.show_fix_status(true);
|
env.show_fix_status(true);
|
||||||
env.fix_applicability(Applicability::DisplayOnly);
|
env.fix_applicability(Applicability::DisplayOnly);
|
||||||
|
|
||||||
insta::assert_snapshot!(env.render_diagnostics(&diagnostics), @r"
|
insta::assert_snapshot!(env.render_diagnostics(&diagnostics));
|
||||||
error[unused-import][*]: `os` imported but unused
|
|
||||||
--> notebook.ipynb:cell 1:2:8
|
|
||||||
|
|
|
||||||
1 | # cell 1
|
|
||||||
2 | import os
|
|
||||||
| ^^
|
|
||||||
|
|
|
||||||
help: Remove unused import: `os`
|
|
||||||
::: cell 1
|
|
||||||
1 | # cell 1
|
|
||||||
- import os
|
|
||||||
|
|
||||||
error[unused-import][*]: `math` imported but unused
|
|
||||||
--> notebook.ipynb:cell 2:2:8
|
|
||||||
|
|
|
||||||
1 | # cell 2
|
|
||||||
2 | import math
|
|
||||||
| ^^^^
|
|
||||||
3 |
|
|
||||||
4 | print('hello world')
|
|
||||||
|
|
|
||||||
help: Remove unused import: `math`
|
|
||||||
::: cell 2
|
|
||||||
1 | # cell 2
|
|
||||||
- import math
|
|
||||||
2 |
|
|
||||||
3 | print('hello world')
|
|
||||||
|
|
||||||
error[unused-variable][*]: Local variable `x` is assigned to but never used
|
|
||||||
--> notebook.ipynb:cell 3:4:5
|
|
||||||
|
|
|
||||||
2 | def foo():
|
|
||||||
3 | print()
|
|
||||||
4 | x = 1
|
|
||||||
| ^
|
|
||||||
|
|
|
||||||
help: Remove assignment to unused variable `x`
|
|
||||||
::: cell 3
|
|
||||||
1 | # cell 3
|
|
||||||
2 | def foo():
|
|
||||||
3 | print()
|
|
||||||
- x = 1
|
|
||||||
4 |
|
|
||||||
note: This is an unsafe fix and may change runtime behavior
|
|
||||||
");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -768,31 +724,7 @@ print()
|
|||||||
}
|
}
|
||||||
*fix = Fix::unsafe_edits(edits.remove(0), edits);
|
*fix = Fix::unsafe_edits(edits.remove(0), edits);
|
||||||
|
|
||||||
insta::assert_snapshot!(env.render(&diagnostic), @r"
|
insta::assert_snapshot!(env.render(&diagnostic));
|
||||||
error[unused-import][*]: `os` imported but unused
|
|
||||||
--> notebook.ipynb:cell 1:2:8
|
|
||||||
|
|
|
||||||
1 | # cell 1
|
|
||||||
2 | import os
|
|
||||||
| ^^
|
|
||||||
|
|
|
||||||
help: Remove unused import: `os`
|
|
||||||
::: cell 1
|
|
||||||
1 | # cell 1
|
|
||||||
- import os
|
|
||||||
::: cell 2
|
|
||||||
1 | # cell 2
|
|
||||||
- import math
|
|
||||||
2 |
|
|
||||||
3 | print('hello world')
|
|
||||||
::: cell 3
|
|
||||||
1 | # cell 3
|
|
||||||
2 | def foo():
|
|
||||||
3 | print()
|
|
||||||
- x = 1
|
|
||||||
4 |
|
|
||||||
note: This is an unsafe fix and may change runtime behavior
|
|
||||||
");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Carriage return (`\r`) is a valid line-ending in Python, so we should normalize this to a
|
/// Carriage return (`\r`) is a valid line-ending in Python, so we should normalize this to a
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ pub(super) fn diagnostic_to_json<'a>(
|
|||||||
if config.preview {
|
if config.preview {
|
||||||
JsonDiagnostic {
|
JsonDiagnostic {
|
||||||
code: diagnostic.secondary_code_or_id(),
|
code: diagnostic.secondary_code_or_id(),
|
||||||
url: diagnostic.to_ruff_url(),
|
url: diagnostic.documentation_url(),
|
||||||
message: diagnostic.body(),
|
message: diagnostic.body(),
|
||||||
fix,
|
fix,
|
||||||
cell: notebook_cell_index,
|
cell: notebook_cell_index,
|
||||||
@@ -112,7 +112,7 @@ pub(super) fn diagnostic_to_json<'a>(
|
|||||||
} else {
|
} else {
|
||||||
JsonDiagnostic {
|
JsonDiagnostic {
|
||||||
code: diagnostic.secondary_code_or_id(),
|
code: diagnostic.secondary_code_or_id(),
|
||||||
url: diagnostic.to_ruff_url(),
|
url: diagnostic.documentation_url(),
|
||||||
message: diagnostic.body(),
|
message: diagnostic.body(),
|
||||||
fix,
|
fix,
|
||||||
cell: notebook_cell_index,
|
cell: notebook_cell_index,
|
||||||
@@ -228,7 +228,7 @@ pub(crate) struct JsonDiagnostic<'a> {
|
|||||||
location: Option<JsonLocation>,
|
location: Option<JsonLocation>,
|
||||||
message: &'a str,
|
message: &'a str,
|
||||||
noqa_row: Option<OneIndexed>,
|
noqa_row: Option<OneIndexed>,
|
||||||
url: Option<String>,
|
url: Option<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -294,7 +294,10 @@ mod tests {
|
|||||||
env.format(DiagnosticFormat::Json);
|
env.format(DiagnosticFormat::Json);
|
||||||
env.preview(false);
|
env.preview(false);
|
||||||
|
|
||||||
let diag = env.err().build();
|
let diag = env
|
||||||
|
.err()
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/test-diagnostic")
|
||||||
|
.build();
|
||||||
|
|
||||||
insta::assert_snapshot!(
|
insta::assert_snapshot!(
|
||||||
env.render(&diag),
|
env.render(&diag),
|
||||||
@@ -328,7 +331,10 @@ mod tests {
|
|||||||
env.format(DiagnosticFormat::Json);
|
env.format(DiagnosticFormat::Json);
|
||||||
env.preview(true);
|
env.preview(true);
|
||||||
|
|
||||||
let diag = env.err().build();
|
let diag = env
|
||||||
|
.err()
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/test-diagnostic")
|
||||||
|
.build();
|
||||||
|
|
||||||
insta::assert_snapshot!(
|
insta::assert_snapshot!(
|
||||||
env.render(&diag),
|
env.render(&diag),
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ fn diagnostic_to_rdjson<'a>(
|
|||||||
value: diagnostic
|
value: diagnostic
|
||||||
.secondary_code()
|
.secondary_code()
|
||||||
.map_or_else(|| diagnostic.name(), |code| code.as_str()),
|
.map_or_else(|| diagnostic.name(), |code| code.as_str()),
|
||||||
url: diagnostic.to_ruff_url(),
|
url: diagnostic.documentation_url(),
|
||||||
},
|
},
|
||||||
suggestions: rdjson_suggestions(
|
suggestions: rdjson_suggestions(
|
||||||
edits,
|
edits,
|
||||||
@@ -182,7 +182,7 @@ impl RdjsonRange {
|
|||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct RdjsonCode<'a> {
|
struct RdjsonCode<'a> {
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
url: Option<String>,
|
url: Option<&'a str>,
|
||||||
value: &'a str,
|
value: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,7 +217,10 @@ mod tests {
|
|||||||
env.format(DiagnosticFormat::Rdjson);
|
env.format(DiagnosticFormat::Rdjson);
|
||||||
env.preview(false);
|
env.preview(false);
|
||||||
|
|
||||||
let diag = env.err().build();
|
let diag = env
|
||||||
|
.err()
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/test-diagnostic")
|
||||||
|
.build();
|
||||||
|
|
||||||
insta::assert_snapshot!(env.render(&diag));
|
insta::assert_snapshot!(env.render(&diag));
|
||||||
}
|
}
|
||||||
@@ -228,7 +231,10 @@ mod tests {
|
|||||||
env.format(DiagnosticFormat::Rdjson);
|
env.format(DiagnosticFormat::Rdjson);
|
||||||
env.preview(true);
|
env.preview(true);
|
||||||
|
|
||||||
let diag = env.err().build();
|
let diag = env
|
||||||
|
.err()
|
||||||
|
.documentation_url("https://docs.astral.sh/ruff/rules/test-diagnostic")
|
||||||
|
.build();
|
||||||
|
|
||||||
insta::assert_snapshot!(env.render(&diag));
|
insta::assert_snapshot!(env.render(&diag));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
---
|
||||||
|
source: crates/ruff_db/src/diagnostic/render/full.rs
|
||||||
|
expression: env.render_diagnostics(&diagnostics)
|
||||||
|
---
|
||||||
|
error[unused-import][*]: `os` imported but unused
|
||||||
|
--> notebook.ipynb:cell 1:2:8
|
||||||
|
|
|
||||||
|
1 | # cell 1
|
||||||
|
2 | import os
|
||||||
|
| ^^
|
||||||
|
|
|
||||||
|
help: Remove unused import: `os`
|
||||||
|
::: cell 1
|
||||||
|
1 | # cell 1
|
||||||
|
- import os
|
||||||
|
|
||||||
|
error[unused-import][*]: `math` imported but unused
|
||||||
|
--> notebook.ipynb:cell 2:2:8
|
||||||
|
|
|
||||||
|
1 | # cell 2
|
||||||
|
2 | import math
|
||||||
|
| ^^^^
|
||||||
|
3 |
|
||||||
|
4 | print('hello world')
|
||||||
|
|
|
||||||
|
help: Remove unused import: `math`
|
||||||
|
::: cell 2
|
||||||
|
1 | # cell 2
|
||||||
|
- import math
|
||||||
|
2 |
|
||||||
|
3 | print('hello world')
|
||||||
|
|
||||||
|
error[unused-variable][*]: Local variable `x` is assigned to but never used
|
||||||
|
--> notebook.ipynb:cell 3:4:5
|
||||||
|
|
|
||||||
|
2 | def foo():
|
||||||
|
3 | print()
|
||||||
|
4 | x = 1
|
||||||
|
| ^
|
||||||
|
|
|
||||||
|
help: Remove assignment to unused variable `x`
|
||||||
|
::: cell 3
|
||||||
|
1 | # cell 3
|
||||||
|
2 | def foo():
|
||||||
|
3 | print()
|
||||||
|
- x = 1
|
||||||
|
4 |
|
||||||
|
note: This is an unsafe fix and may change runtime behavior
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
---
|
||||||
|
source: crates/ruff_db/src/diagnostic/render/full.rs
|
||||||
|
expression: env.render(&diagnostic)
|
||||||
|
---
|
||||||
|
error[unused-import][*]: `os` imported but unused
|
||||||
|
--> notebook.ipynb:cell 1:2:8
|
||||||
|
|
|
||||||
|
1 | # cell 1
|
||||||
|
2 | import os
|
||||||
|
| ^^
|
||||||
|
|
|
||||||
|
help: Remove unused import: `os`
|
||||||
|
::: cell 1
|
||||||
|
1 | # cell 1
|
||||||
|
- import os
|
||||||
|
::: cell 2
|
||||||
|
1 | # cell 2
|
||||||
|
- import math
|
||||||
|
2 |
|
||||||
|
3 | print('hello world')
|
||||||
|
::: cell 3
|
||||||
|
1 | # cell 3
|
||||||
|
2 | def foo():
|
||||||
|
3 | print()
|
||||||
|
- x = 1
|
||||||
|
4 |
|
||||||
|
note: This is an unsafe fix and may change runtime behavior
|
||||||
@@ -31,6 +31,43 @@ where
|
|||||||
FmtStyled { content, style }
|
FmtStyled { content, style }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn fmt_with_hyperlink<'a, T>(
|
||||||
|
content: T,
|
||||||
|
url: Option<&'a str>,
|
||||||
|
stylesheet: &DiagnosticStylesheet,
|
||||||
|
) -> impl std::fmt::Display + 'a
|
||||||
|
where
|
||||||
|
T: std::fmt::Display + 'a,
|
||||||
|
{
|
||||||
|
struct FmtHyperlink<'a, T> {
|
||||||
|
content: T,
|
||||||
|
url: Option<&'a str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> std::fmt::Display for FmtHyperlink<'_, T>
|
||||||
|
where
|
||||||
|
T: std::fmt::Display,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
if let Some(url) = self.url {
|
||||||
|
write!(f, "\x1B]8;;{url}\x1B\\")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.content.fmt(f)?;
|
||||||
|
|
||||||
|
if self.url.is_some() {
|
||||||
|
f.write_str("\x1B]8;;\x1B\\")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let url = if stylesheet.hyperlink { url } else { None };
|
||||||
|
|
||||||
|
FmtHyperlink { content, url }
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct DiagnosticStylesheet {
|
pub struct DiagnosticStylesheet {
|
||||||
pub(crate) error: Style,
|
pub(crate) error: Style,
|
||||||
@@ -47,6 +84,7 @@ pub struct DiagnosticStylesheet {
|
|||||||
pub(crate) deletion: Style,
|
pub(crate) deletion: Style,
|
||||||
pub(crate) insertion_line_no: Style,
|
pub(crate) insertion_line_no: Style,
|
||||||
pub(crate) deletion_line_no: Style,
|
pub(crate) deletion_line_no: Style,
|
||||||
|
pub(crate) hyperlink: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DiagnosticStylesheet {
|
impl Default for DiagnosticStylesheet {
|
||||||
@@ -59,6 +97,8 @@ impl DiagnosticStylesheet {
|
|||||||
/// Default terminal styling
|
/// Default terminal styling
|
||||||
pub fn styled() -> Self {
|
pub fn styled() -> Self {
|
||||||
let bright_blue = AnsiColor::BrightBlue.on_default();
|
let bright_blue = AnsiColor::BrightBlue.on_default();
|
||||||
|
|
||||||
|
let hyperlink = supports_hyperlinks::supports_hyperlinks();
|
||||||
Self {
|
Self {
|
||||||
error: AnsiColor::BrightRed.on_default().effects(Effects::BOLD),
|
error: AnsiColor::BrightRed.on_default().effects(Effects::BOLD),
|
||||||
warning: AnsiColor::Yellow.on_default().effects(Effects::BOLD),
|
warning: AnsiColor::Yellow.on_default().effects(Effects::BOLD),
|
||||||
@@ -74,6 +114,7 @@ impl DiagnosticStylesheet {
|
|||||||
deletion: AnsiColor::Red.on_default(),
|
deletion: AnsiColor::Red.on_default(),
|
||||||
insertion_line_no: AnsiColor::Green.on_default().effects(Effects::BOLD),
|
insertion_line_no: AnsiColor::Green.on_default().effects(Effects::BOLD),
|
||||||
deletion_line_no: AnsiColor::Red.on_default().effects(Effects::BOLD),
|
deletion_line_no: AnsiColor::Red.on_default().effects(Effects::BOLD),
|
||||||
|
hyperlink,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +134,7 @@ impl DiagnosticStylesheet {
|
|||||||
deletion: Style::new(),
|
deletion: Style::new(),
|
||||||
insertion_line_no: Style::new(),
|
insertion_line_no: Style::new(),
|
||||||
deletion_line_no: Style::new(),
|
deletion_line_no: Style::new(),
|
||||||
|
hyperlink: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use ruff_source_file::LineIndex;
|
|||||||
|
|
||||||
use crate::Db;
|
use crate::Db;
|
||||||
use crate::files::{File, FilePath};
|
use crate::files::{File, FilePath};
|
||||||
|
use crate::system::System;
|
||||||
|
|
||||||
/// Reads the source text of a python text file (must be valid UTF8) or notebook.
|
/// Reads the source text of a python text file (must be valid UTF8) or notebook.
|
||||||
#[salsa::tracked(heap_size=ruff_memory_usage::heap_size)]
|
#[salsa::tracked(heap_size=ruff_memory_usage::heap_size)]
|
||||||
@@ -15,7 +16,7 @@ pub fn source_text(db: &dyn Db, file: File) -> SourceText {
|
|||||||
let _span = tracing::trace_span!("source_text", file = %path).entered();
|
let _span = tracing::trace_span!("source_text", file = %path).entered();
|
||||||
let mut read_error = None;
|
let mut read_error = None;
|
||||||
|
|
||||||
let kind = if is_notebook(file.path(db)) {
|
let kind = if is_notebook(db.system(), path) {
|
||||||
file.read_to_notebook(db)
|
file.read_to_notebook(db)
|
||||||
.unwrap_or_else(|error| {
|
.unwrap_or_else(|error| {
|
||||||
tracing::debug!("Failed to read notebook '{path}': {error}");
|
tracing::debug!("Failed to read notebook '{path}': {error}");
|
||||||
@@ -40,18 +41,17 @@ pub fn source_text(db: &dyn Db, file: File) -> SourceText {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_notebook(path: &FilePath) -> bool {
|
fn is_notebook(system: &dyn System, path: &FilePath) -> bool {
|
||||||
match path {
|
let source_type = match path {
|
||||||
FilePath::System(system) => system.extension().is_some_and(|extension| {
|
FilePath::System(path) => system.source_type(path),
|
||||||
PySourceType::try_from_extension(extension) == Some(PySourceType::Ipynb)
|
FilePath::SystemVirtual(system_virtual) => system.virtual_path_source_type(system_virtual),
|
||||||
}),
|
FilePath::Vendored(_) => return false,
|
||||||
FilePath::SystemVirtual(system_virtual) => {
|
};
|
||||||
system_virtual.extension().is_some_and(|extension| {
|
|
||||||
PySourceType::try_from_extension(extension) == Some(PySourceType::Ipynb)
|
let with_extension_fallback =
|
||||||
})
|
source_type.or_else(|| PySourceType::try_from_extension(path.extension()?));
|
||||||
}
|
|
||||||
FilePath::Vendored(_) => false,
|
with_extension_fallback == Some(PySourceType::Ipynb)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The source text of a file containing python code.
|
/// The source text of a file containing python code.
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ pub use os::OsSystem;
|
|||||||
|
|
||||||
use filetime::FileTime;
|
use filetime::FileTime;
|
||||||
use ruff_notebook::{Notebook, NotebookError};
|
use ruff_notebook::{Notebook, NotebookError};
|
||||||
|
use ruff_python_ast::PySourceType;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
@@ -16,12 +17,11 @@ use std::{fmt, io};
|
|||||||
pub use test::{DbWithTestSystem, DbWithWritableSystem, InMemorySystem, TestSystem};
|
pub use test::{DbWithTestSystem, DbWithWritableSystem, InMemorySystem, TestSystem};
|
||||||
use walk_directory::WalkDirectoryBuilder;
|
use walk_directory::WalkDirectoryBuilder;
|
||||||
|
|
||||||
use crate::file_revision::FileRevision;
|
|
||||||
|
|
||||||
pub use self::path::{
|
pub use self::path::{
|
||||||
DeduplicatedNestedPathsIter, SystemPath, SystemPathBuf, SystemVirtualPath,
|
DeduplicatedNestedPathsIter, SystemPath, SystemPathBuf, SystemVirtualPath,
|
||||||
SystemVirtualPathBuf, deduplicate_nested_paths,
|
SystemVirtualPathBuf, deduplicate_nested_paths,
|
||||||
};
|
};
|
||||||
|
use crate::file_revision::FileRevision;
|
||||||
|
|
||||||
mod memory_fs;
|
mod memory_fs;
|
||||||
#[cfg(feature = "os")]
|
#[cfg(feature = "os")]
|
||||||
@@ -66,6 +66,35 @@ pub trait System: Debug + Sync + Send {
|
|||||||
/// See [dunce::canonicalize] for more information.
|
/// See [dunce::canonicalize] for more information.
|
||||||
fn canonicalize_path(&self, path: &SystemPath) -> Result<SystemPathBuf>;
|
fn canonicalize_path(&self, path: &SystemPath) -> Result<SystemPathBuf>;
|
||||||
|
|
||||||
|
/// Returns the source type for `path` if known or `None`.
|
||||||
|
///
|
||||||
|
/// The default is to always return `None`, assuming the system
|
||||||
|
/// has no additional information and that the caller should
|
||||||
|
/// rely on the file extension instead.
|
||||||
|
///
|
||||||
|
/// This is primarily used for the LSP integration to respect
|
||||||
|
/// the chosen language (or the fact that it is a notebook) in
|
||||||
|
/// the editor.
|
||||||
|
fn source_type(&self, path: &SystemPath) -> Option<PySourceType> {
|
||||||
|
let _ = path;
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the source type for `path` if known or `None`.
|
||||||
|
///
|
||||||
|
/// The default is to always return `None`, assuming the system
|
||||||
|
/// has no additional information and that the caller should
|
||||||
|
/// rely on the file extension instead.
|
||||||
|
///
|
||||||
|
/// This is primarily used for the LSP integration to respect
|
||||||
|
/// the chosen language (or the fact that it is a notebook) in
|
||||||
|
/// the editor.
|
||||||
|
fn virtual_path_source_type(&self, path: &SystemVirtualPath) -> Option<PySourceType> {
|
||||||
|
let _ = path;
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// Reads the content of the file at `path` into a [`String`].
|
/// Reads the content of the file at `path` into a [`String`].
|
||||||
fn read_to_string(&self, path: &SystemPath) -> Result<String>;
|
fn read_to_string(&self, path: &SystemPath) -> Result<String>;
|
||||||
|
|
||||||
|
|||||||
@@ -14,14 +14,21 @@ pub(crate) struct Collector<'a> {
|
|||||||
string_imports: StringImports,
|
string_imports: StringImports,
|
||||||
/// The collected imports from the Python AST.
|
/// The collected imports from the Python AST.
|
||||||
imports: Vec<CollectedImport>,
|
imports: Vec<CollectedImport>,
|
||||||
|
/// Whether to detect type checking imports
|
||||||
|
type_checking_imports: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Collector<'a> {
|
impl<'a> Collector<'a> {
|
||||||
pub(crate) fn new(module_path: Option<&'a [String]>, string_imports: StringImports) -> Self {
|
pub(crate) fn new(
|
||||||
|
module_path: Option<&'a [String]>,
|
||||||
|
string_imports: StringImports,
|
||||||
|
type_checking_imports: bool,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
module_path,
|
module_path,
|
||||||
string_imports,
|
string_imports,
|
||||||
imports: Vec::new(),
|
imports: Vec::new(),
|
||||||
|
type_checking_imports,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,10 +98,25 @@ impl<'ast> SourceOrderVisitor<'ast> for Collector<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Stmt::If(ast::StmtIf {
|
||||||
|
test,
|
||||||
|
body,
|
||||||
|
elif_else_clauses,
|
||||||
|
range: _,
|
||||||
|
node_index: _,
|
||||||
|
}) => {
|
||||||
|
// Skip TYPE_CHECKING blocks if not requested
|
||||||
|
if self.type_checking_imports || !is_type_checking_condition(test) {
|
||||||
|
self.visit_body(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
for clause in elif_else_clauses {
|
||||||
|
self.visit_elif_else_clause(clause);
|
||||||
|
}
|
||||||
|
}
|
||||||
Stmt::FunctionDef(_)
|
Stmt::FunctionDef(_)
|
||||||
| Stmt::ClassDef(_)
|
| Stmt::ClassDef(_)
|
||||||
| Stmt::While(_)
|
| Stmt::While(_)
|
||||||
| Stmt::If(_)
|
|
||||||
| Stmt::With(_)
|
| Stmt::With(_)
|
||||||
| Stmt::Match(_)
|
| Stmt::Match(_)
|
||||||
| Stmt::Try(_)
|
| Stmt::Try(_)
|
||||||
@@ -152,6 +174,30 @@ impl<'ast> SourceOrderVisitor<'ast> for Collector<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check if an expression is a `TYPE_CHECKING` condition.
|
||||||
|
///
|
||||||
|
/// Returns `true` for:
|
||||||
|
/// - `TYPE_CHECKING`
|
||||||
|
/// - `typing.TYPE_CHECKING`
|
||||||
|
///
|
||||||
|
/// NOTE: Aliased `TYPE_CHECKING`, i.e. `import typing.TYPE_CHECKING as TC; if TC: ...`
|
||||||
|
/// will not be detected!
|
||||||
|
fn is_type_checking_condition(expr: &Expr) -> bool {
|
||||||
|
match expr {
|
||||||
|
// `if TYPE_CHECKING:`
|
||||||
|
Expr::Name(ast::ExprName { id, .. }) => id.as_str() == "TYPE_CHECKING",
|
||||||
|
// `if typing.TYPE_CHECKING:`
|
||||||
|
Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => {
|
||||||
|
attr.as_str() == "TYPE_CHECKING"
|
||||||
|
&& matches!(
|
||||||
|
value.as_ref(),
|
||||||
|
Expr::Name(ast::ExprName { id, .. }) if id.as_str() == "typing"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) enum CollectedImport {
|
pub(crate) enum CollectedImport {
|
||||||
/// The import was part of an `import` statement.
|
/// The import was part of an `import` statement.
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ impl ModuleImports {
|
|||||||
path: &SystemPath,
|
path: &SystemPath,
|
||||||
package: Option<&SystemPath>,
|
package: Option<&SystemPath>,
|
||||||
string_imports: StringImports,
|
string_imports: StringImports,
|
||||||
|
type_checking_imports: bool,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
// Parse the source code.
|
// Parse the source code.
|
||||||
let parsed = parse(source, ParseOptions::from(source_type))?;
|
let parsed = parse(source, ParseOptions::from(source_type))?;
|
||||||
@@ -38,8 +39,12 @@ impl ModuleImports {
|
|||||||
package.and_then(|package| to_module_path(package.as_std_path(), path.as_std_path()));
|
package.and_then(|package| to_module_path(package.as_std_path(), path.as_std_path()));
|
||||||
|
|
||||||
// Collect the imports.
|
// Collect the imports.
|
||||||
let imports =
|
let imports = Collector::new(
|
||||||
Collector::new(module_path.as_deref(), string_imports).collect(parsed.syntax());
|
module_path.as_deref(),
|
||||||
|
string_imports,
|
||||||
|
type_checking_imports,
|
||||||
|
)
|
||||||
|
.collect(parsed.syntax());
|
||||||
|
|
||||||
// Resolve the imports.
|
// Resolve the imports.
|
||||||
let mut resolved_imports = ModuleImports::default();
|
let mut resolved_imports = ModuleImports::default();
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use std::collections::BTreeMap;
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, CacheKey)]
|
#[derive(Debug, Clone, CacheKey)]
|
||||||
pub struct AnalyzeSettings {
|
pub struct AnalyzeSettings {
|
||||||
pub exclude: FilePatternSet,
|
pub exclude: FilePatternSet,
|
||||||
pub preview: PreviewMode,
|
pub preview: PreviewMode,
|
||||||
@@ -14,6 +14,21 @@ pub struct AnalyzeSettings {
|
|||||||
pub string_imports: StringImports,
|
pub string_imports: StringImports,
|
||||||
pub include_dependencies: BTreeMap<PathBuf, (PathBuf, Vec<String>)>,
|
pub include_dependencies: BTreeMap<PathBuf, (PathBuf, Vec<String>)>,
|
||||||
pub extension: ExtensionMapping,
|
pub extension: ExtensionMapping,
|
||||||
|
pub type_checking_imports: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for AnalyzeSettings {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
exclude: FilePatternSet::default(),
|
||||||
|
preview: PreviewMode::default(),
|
||||||
|
target_version: PythonVersion::default(),
|
||||||
|
string_imports: StringImports::default(),
|
||||||
|
include_dependencies: BTreeMap::default(),
|
||||||
|
extension: ExtensionMapping::default(),
|
||||||
|
type_checking_imports: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for AnalyzeSettings {
|
impl fmt::Display for AnalyzeSettings {
|
||||||
@@ -29,6 +44,7 @@ impl fmt::Display for AnalyzeSettings {
|
|||||||
self.string_imports,
|
self.string_imports,
|
||||||
self.extension | debug,
|
self.extension | debug,
|
||||||
self.include_dependencies | debug,
|
self.include_dependencies | debug,
|
||||||
|
self.type_checking_imports,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ruff_linter"
|
name = "ruff_linter"
|
||||||
version = "0.14.4"
|
version = "0.14.6"
|
||||||
publish = false
|
publish = false
|
||||||
authors = { workspace = true }
|
authors = { workspace = true }
|
||||||
edition = { workspace = true }
|
edition = { workspace = true }
|
||||||
|
|||||||
@@ -4,3 +4,31 @@ CommunityData("public", mpModel=0) # S508
|
|||||||
CommunityData("public", mpModel=1) # S508
|
CommunityData("public", mpModel=1) # S508
|
||||||
|
|
||||||
CommunityData("public", mpModel=2) # OK
|
CommunityData("public", mpModel=2) # OK
|
||||||
|
|
||||||
|
# New API paths
|
||||||
|
import pysnmp.hlapi.asyncio
|
||||||
|
import pysnmp.hlapi.v1arch
|
||||||
|
import pysnmp.hlapi.v1arch.asyncio
|
||||||
|
import pysnmp.hlapi.v1arch.asyncio.auth
|
||||||
|
import pysnmp.hlapi.v3arch
|
||||||
|
import pysnmp.hlapi.v3arch.asyncio
|
||||||
|
import pysnmp.hlapi.v3arch.asyncio.auth
|
||||||
|
import pysnmp.hlapi.auth
|
||||||
|
|
||||||
|
pysnmp.hlapi.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
pysnmp.hlapi.v1arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
pysnmp.hlapi.v1arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
pysnmp.hlapi.v1arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
pysnmp.hlapi.v3arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
pysnmp.hlapi.v3arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
pysnmp.hlapi.v3arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
pysnmp.hlapi.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
|
||||||
|
pysnmp.hlapi.asyncio.CommunityData("public", mpModel=2) # OK
|
||||||
|
pysnmp.hlapi.v1arch.asyncio.auth.CommunityData("public", mpModel=2) # OK
|
||||||
|
pysnmp.hlapi.v1arch.asyncio.CommunityData("public", mpModel=2) # OK
|
||||||
|
pysnmp.hlapi.v1arch.CommunityData("public", mpModel=2) # OK
|
||||||
|
pysnmp.hlapi.v3arch.asyncio.auth.CommunityData("public", mpModel=2) # OK
|
||||||
|
pysnmp.hlapi.v3arch.asyncio.CommunityData("public", mpModel=2) # OK
|
||||||
|
pysnmp.hlapi.v3arch.CommunityData("public", mpModel=2) # OK
|
||||||
|
pysnmp.hlapi.auth.CommunityData("public", mpModel=2) # OK
|
||||||
|
|||||||
@@ -5,3 +5,19 @@ insecure = UsmUserData("securityName") # S509
|
|||||||
auth_no_priv = UsmUserData("securityName", "authName") # S509
|
auth_no_priv = UsmUserData("securityName", "authName") # S509
|
||||||
|
|
||||||
less_insecure = UsmUserData("securityName", "authName", "privName") # OK
|
less_insecure = UsmUserData("securityName", "authName", "privName") # OK
|
||||||
|
|
||||||
|
# New API paths
|
||||||
|
import pysnmp.hlapi.asyncio
|
||||||
|
import pysnmp.hlapi.v3arch.asyncio
|
||||||
|
import pysnmp.hlapi.v3arch.asyncio.auth
|
||||||
|
import pysnmp.hlapi.auth
|
||||||
|
|
||||||
|
pysnmp.hlapi.asyncio.UsmUserData("user") # S509
|
||||||
|
pysnmp.hlapi.v3arch.asyncio.UsmUserData("user") # S509
|
||||||
|
pysnmp.hlapi.v3arch.asyncio.auth.UsmUserData("user") # S509
|
||||||
|
pysnmp.hlapi.auth.UsmUserData("user") # S509
|
||||||
|
|
||||||
|
pysnmp.hlapi.asyncio.UsmUserData("user", "authkey", "privkey") # OK
|
||||||
|
pysnmp.hlapi.v3arch.asyncio.UsmUserData("user", "authkey", "privkey") # OK
|
||||||
|
pysnmp.hlapi.v3arch.asyncio.auth.UsmUserData("user", "authkey", "privkey") # OK
|
||||||
|
pysnmp.hlapi.auth.UsmUserData("user", "authkey", "privkey") # OK
|
||||||
|
|||||||
@@ -46,7 +46,8 @@ def func():
|
|||||||
|
|
||||||
|
|
||||||
def func():
|
def func():
|
||||||
# OK (index doesn't start at 0
|
# SIM113
|
||||||
|
# https://github.com/astral-sh/ruff/pull/21395
|
||||||
idx = 10
|
idx = 10
|
||||||
for x in range(5):
|
for x in range(5):
|
||||||
g(x, idx)
|
g(x, idx)
|
||||||
|
|||||||
@@ -371,6 +371,61 @@ class Foo:
|
|||||||
"""
|
"""
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# DOC102 - Test case from issue #20959: comma-separated parameters
|
||||||
|
def leq(x: object, y: object) -> bool:
|
||||||
|
"""Compare two objects for loose equality.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
x1, x2 : object
|
||||||
|
Objects.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
bool
|
||||||
|
Whether the objects are identical or equal.
|
||||||
|
"""
|
||||||
|
return x is y or x == y
|
||||||
|
|
||||||
|
|
||||||
|
# OK - comma-separated parameters that match function signature
|
||||||
|
def compare_values(x1: int, x2: int) -> bool:
|
||||||
|
"""Compare two integer values.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
x1, x2 : int
|
||||||
|
Values to compare.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
bool
|
||||||
|
True if values are equal.
|
||||||
|
"""
|
||||||
|
return x1 == x2
|
||||||
|
|
||||||
|
|
||||||
|
# DOC102 - mixed comma-separated and regular parameters
|
||||||
|
def process_data(data, x1: str, x2: str) -> str:
|
||||||
|
"""Process data with multiple string parameters.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
data : list
|
||||||
|
Input data to process.
|
||||||
|
x1, x2 : str
|
||||||
|
String parameters for processing.
|
||||||
|
extra_param : str
|
||||||
|
Extra parameter not in signature.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
str
|
||||||
|
Processed result.
|
||||||
|
"""
|
||||||
|
return f"{x1}{x2}{len(data)}"
|
||||||
|
|
||||||
|
|
||||||
# OK
|
# OK
|
||||||
def baz(x: int) -> int:
|
def baz(x: int) -> int:
|
||||||
"""
|
"""
|
||||||
@@ -389,3 +444,21 @@ def baz(x: int) -> int:
|
|||||||
int
|
int
|
||||||
"""
|
"""
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
# OK - comma-separated parameters without type annotations
|
||||||
|
def add_numbers(a, b):
|
||||||
|
"""
|
||||||
|
Adds two numbers and returns the result.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
a, b
|
||||||
|
The numbers to add.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
int
|
||||||
|
The sum of the two numbers.
|
||||||
|
"""
|
||||||
|
return a + b
|
||||||
|
|||||||
@@ -83,6 +83,37 @@ def calculate_speed(distance: float, time: float) -> float:
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
# DOC502 regression for Sphinx directive after Raises (issue #18959)
|
||||||
|
def foo():
|
||||||
|
"""First line.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError:
|
||||||
|
some text
|
||||||
|
|
||||||
|
.. versionadded:: 0.7.0
|
||||||
|
The ``init_kwargs`` argument.
|
||||||
|
"""
|
||||||
|
raise ValueError
|
||||||
|
|
||||||
|
|
||||||
|
# DOC502 regression for following section with colons
|
||||||
|
def example_with_following_section():
|
||||||
|
"""Summary.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The resulting expression.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If the unit is not valid.
|
||||||
|
|
||||||
|
Relation to `time_range_lookup`:
|
||||||
|
- Handles the "start of" modifier.
|
||||||
|
- Example: "start of month" → `DATETRUNC()`.
|
||||||
|
"""
|
||||||
|
raise ValueError
|
||||||
|
|
||||||
|
|
||||||
# This should NOT trigger DOC502 because OSError is explicitly re-raised
|
# This should NOT trigger DOC502 because OSError is explicitly re-raised
|
||||||
def f():
|
def f():
|
||||||
"""Do nothing.
|
"""Do nothing.
|
||||||
|
|||||||
@@ -117,3 +117,33 @@ def calculate_speed(distance: float, time: float) -> float:
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
print("Not a number? Shame on you!")
|
print("Not a number? Shame on you!")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
# DOC502 regression for Sphinx directive after Raises (issue #18959)
|
||||||
|
def foo():
|
||||||
|
"""First line.
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
ValueError
|
||||||
|
some text
|
||||||
|
|
||||||
|
.. versionadded:: 0.7.0
|
||||||
|
The ``init_kwargs`` argument.
|
||||||
|
"""
|
||||||
|
raise ValueError
|
||||||
|
|
||||||
|
# Make sure we don't bail out on a Sphinx directive in the description of one
|
||||||
|
# of the exceptions
|
||||||
|
def foo():
|
||||||
|
"""First line.
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
ValueError
|
||||||
|
some text
|
||||||
|
.. math:: e^{xception}
|
||||||
|
ZeroDivisionError
|
||||||
|
Will not be raised, DOC502
|
||||||
|
"""
|
||||||
|
raise ValueError
|
||||||
|
|||||||
@@ -153,3 +153,12 @@ data = {"price": 100}
|
|||||||
|
|
||||||
with open("test.json", "wb") as f:
|
with open("test.json", "wb") as f:
|
||||||
f.write(json.dumps(data, indent=4).encode("utf-8"))
|
f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
|
|
||||||
|
# See: https://github.com/astral-sh/ruff/issues/21381
|
||||||
|
with open("tmp_path/pyproject.toml", "w") as f:
|
||||||
|
f.write(dedent(
|
||||||
|
"""
|
||||||
|
[project]
|
||||||
|
other = 1.234
|
||||||
|
""",
|
||||||
|
))
|
||||||
|
|||||||
@@ -132,3 +132,9 @@ class AWithQuotes:
|
|||||||
final_variable: 'Final[list[int]]' = []
|
final_variable: 'Final[list[int]]' = []
|
||||||
class_variable_without_subscript: 'ClassVar' = []
|
class_variable_without_subscript: 'ClassVar' = []
|
||||||
final_variable_without_subscript: 'Final' = []
|
final_variable_without_subscript: 'Final' = []
|
||||||
|
|
||||||
|
|
||||||
|
# Reassignment of a ClassVar should not trigger RUF012
|
||||||
|
class P:
|
||||||
|
class_variable: ClassVar[list] = [10, 20, 30, 40, 50]
|
||||||
|
class_variable = [*class_variable[0::1], *class_variable[2::3]]
|
||||||
|
|||||||
@@ -16,3 +16,19 @@ logging.warning("%s", str(**{"object": b"\xf0\x9f\x9a\xa8", "encoding": "utf-8"}
|
|||||||
# str() with single keyword argument - should be flagged (equivalent to str("!"))
|
# str() with single keyword argument - should be flagged (equivalent to str("!"))
|
||||||
logging.warning("%s", str(object="!"))
|
logging.warning("%s", str(object="!"))
|
||||||
|
|
||||||
|
|
||||||
|
# Complex conversion specifiers that make oct() and hex() necessary
|
||||||
|
# These should NOT be flagged because the behavior differs between %s and %#o/%#x
|
||||||
|
# https://github.com/astral-sh/ruff/issues/21458
|
||||||
|
|
||||||
|
# %06s with oct() - zero-pad flag with width (should NOT be flagged)
|
||||||
|
logging.warning("%06s", oct(123))
|
||||||
|
|
||||||
|
# % s with oct() - blank sign flag (should NOT be flagged)
|
||||||
|
logging.warning("% s", oct(123))
|
||||||
|
|
||||||
|
# %+s with oct() - sign char flag (should NOT be flagged)
|
||||||
|
logging.warning("%+s", oct(123))
|
||||||
|
|
||||||
|
# %.3s with hex() - precision (should NOT be flagged)
|
||||||
|
logging.warning("%.3s", hex(123))
|
||||||
|
|||||||
@@ -860,23 +860,17 @@ impl SemanticSyntaxContext for Checker<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_bound_parameter(&self, name: &str) -> bool {
|
fn is_bound_parameter(&self, name: &str) -> bool {
|
||||||
for scope in self.semantic.current_scopes() {
|
match self.semantic.current_scope().kind {
|
||||||
match scope.kind {
|
ScopeKind::Function(ast::StmtFunctionDef { parameters, .. }) => {
|
||||||
ScopeKind::Class(_) => return false,
|
parameters.includes(name)
|
||||||
ScopeKind::Function(ast::StmtFunctionDef { parameters, .. })
|
|
||||||
| ScopeKind::Lambda(ast::ExprLambda {
|
|
||||||
parameters: Some(parameters),
|
|
||||||
..
|
|
||||||
}) => return parameters.includes(name),
|
|
||||||
ScopeKind::Lambda(_)
|
|
||||||
| ScopeKind::Generator { .. }
|
|
||||||
| ScopeKind::Module
|
|
||||||
| ScopeKind::Type
|
|
||||||
| ScopeKind::DunderClassCell => {}
|
|
||||||
}
|
}
|
||||||
|
ScopeKind::Class(_)
|
||||||
|
| ScopeKind::Lambda(_)
|
||||||
|
| ScopeKind::Generator { .. }
|
||||||
|
| ScopeKind::Module
|
||||||
|
| ScopeKind::Type
|
||||||
|
| ScopeKind::DunderClassCell => false,
|
||||||
}
|
}
|
||||||
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ impl<'a> Importer<'a> {
|
|||||||
.into_edit(&required_import)
|
.into_edit(&required_import)
|
||||||
} else {
|
} else {
|
||||||
// Insert at the start of the file.
|
// Insert at the start of the file.
|
||||||
Insertion::start_of_file(self.python_ast, self.source, self.stylist)
|
Insertion::start_of_file(self.python_ast, self.source, self.stylist, None)
|
||||||
.into_edit(&required_import)
|
.into_edit(&required_import)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ impl<'a> Importer<'a> {
|
|||||||
Insertion::end_of_statement(stmt, self.source, self.stylist)
|
Insertion::end_of_statement(stmt, self.source, self.stylist)
|
||||||
} else {
|
} else {
|
||||||
// Insert at the start of the file.
|
// Insert at the start of the file.
|
||||||
Insertion::start_of_file(self.python_ast, self.source, self.stylist)
|
Insertion::start_of_file(self.python_ast, self.source, self.stylist, None)
|
||||||
};
|
};
|
||||||
let add_import_edit = insertion.into_edit(&content);
|
let add_import_edit = insertion.into_edit(&content);
|
||||||
|
|
||||||
@@ -498,7 +498,7 @@ impl<'a> Importer<'a> {
|
|||||||
Insertion::end_of_statement(stmt, self.source, self.stylist)
|
Insertion::end_of_statement(stmt, self.source, self.stylist)
|
||||||
} else {
|
} else {
|
||||||
// Insert at the start of the file.
|
// Insert at the start of the file.
|
||||||
Insertion::start_of_file(self.python_ast, self.source, self.stylist)
|
Insertion::start_of_file(self.python_ast, self.source, self.stylist, None)
|
||||||
};
|
};
|
||||||
if insertion.is_inline() {
|
if insertion.is_inline() {
|
||||||
Err(anyhow::anyhow!(
|
Err(anyhow::anyhow!(
|
||||||
|
|||||||
@@ -125,6 +125,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
diagnostic.set_secondary_code(SecondaryCode::new(rule.noqa_code().to_string()));
|
diagnostic.set_secondary_code(SecondaryCode::new(rule.noqa_code().to_string()));
|
||||||
|
diagnostic.set_documentation_url(rule.url());
|
||||||
|
|
||||||
diagnostic
|
diagnostic
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -269,3 +269,13 @@ pub(crate) const fn is_typing_extensions_str_alias_enabled(settings: &LinterSett
|
|||||||
pub(crate) const fn is_extended_i18n_function_matching_enabled(settings: &LinterSettings) -> bool {
|
pub(crate) const fn is_extended_i18n_function_matching_enabled(settings: &LinterSettings) -> bool {
|
||||||
settings.preview.is_enabled()
|
settings.preview.is_enabled()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/astral-sh/ruff/pull/21374
|
||||||
|
pub(crate) const fn is_extended_snmp_api_path_detection_enabled(settings: &LinterSettings) -> bool {
|
||||||
|
settings.preview.is_enabled()
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://github.com/astral-sh/ruff/pull/21395
|
||||||
|
pub(crate) const fn is_enumerate_for_loop_int_index_enabled(settings: &LinterSettings) -> bool {
|
||||||
|
settings.preview.is_enabled()
|
||||||
|
}
|
||||||
|
|||||||
@@ -104,6 +104,8 @@ mod tests {
|
|||||||
#[test_case(Rule::SuspiciousURLOpenUsage, Path::new("S310.py"))]
|
#[test_case(Rule::SuspiciousURLOpenUsage, Path::new("S310.py"))]
|
||||||
#[test_case(Rule::SuspiciousNonCryptographicRandomUsage, Path::new("S311.py"))]
|
#[test_case(Rule::SuspiciousNonCryptographicRandomUsage, Path::new("S311.py"))]
|
||||||
#[test_case(Rule::SuspiciousTelnetUsage, Path::new("S312.py"))]
|
#[test_case(Rule::SuspiciousTelnetUsage, Path::new("S312.py"))]
|
||||||
|
#[test_case(Rule::SnmpInsecureVersion, Path::new("S508.py"))]
|
||||||
|
#[test_case(Rule::SnmpWeakCryptography, Path::new("S509.py"))]
|
||||||
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
|
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
|
||||||
let snapshot = format!(
|
let snapshot = format!(
|
||||||
"preview__{}_{}",
|
"preview__{}_{}",
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use ruff_text_size::Ranged;
|
|||||||
|
|
||||||
use crate::Violation;
|
use crate::Violation;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
use crate::preview::is_extended_snmp_api_path_detection_enabled;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for uses of SNMPv1 or SNMPv2.
|
/// Checks for uses of SNMPv1 or SNMPv2.
|
||||||
@@ -47,10 +48,17 @@ pub(crate) fn snmp_insecure_version(checker: &Checker, call: &ast::ExprCall) {
|
|||||||
.semantic()
|
.semantic()
|
||||||
.resolve_qualified_name(&call.func)
|
.resolve_qualified_name(&call.func)
|
||||||
.is_some_and(|qualified_name| {
|
.is_some_and(|qualified_name| {
|
||||||
matches!(
|
if is_extended_snmp_api_path_detection_enabled(checker.settings()) {
|
||||||
qualified_name.segments(),
|
matches!(
|
||||||
["pysnmp", "hlapi", "CommunityData"]
|
qualified_name.segments(),
|
||||||
)
|
["pysnmp", "hlapi", .., "CommunityData"]
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
matches!(
|
||||||
|
qualified_name.segments(),
|
||||||
|
["pysnmp", "hlapi", "CommunityData"]
|
||||||
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
if let Some(keyword) = call.arguments.find_keyword("mpModel") {
|
if let Some(keyword) = call.arguments.find_keyword("mpModel") {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use ruff_text_size::Ranged;
|
|||||||
|
|
||||||
use crate::Violation;
|
use crate::Violation;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
use crate::preview::is_extended_snmp_api_path_detection_enabled;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for uses of the SNMPv3 protocol without encryption.
|
/// Checks for uses of the SNMPv3 protocol without encryption.
|
||||||
@@ -47,10 +48,17 @@ pub(crate) fn snmp_weak_cryptography(checker: &Checker, call: &ast::ExprCall) {
|
|||||||
.semantic()
|
.semantic()
|
||||||
.resolve_qualified_name(&call.func)
|
.resolve_qualified_name(&call.func)
|
||||||
.is_some_and(|qualified_name| {
|
.is_some_and(|qualified_name| {
|
||||||
matches!(
|
if is_extended_snmp_api_path_detection_enabled(checker.settings()) {
|
||||||
qualified_name.segments(),
|
matches!(
|
||||||
["pysnmp", "hlapi", "UsmUserData"]
|
qualified_name.segments(),
|
||||||
)
|
["pysnmp", "hlapi", .., "UsmUserData"]
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
matches!(
|
||||||
|
qualified_name.segments(),
|
||||||
|
["pysnmp", "hlapi", "UsmUserData"]
|
||||||
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
checker.report_diagnostic(SnmpWeakCryptography, call.func.range());
|
checker.report_diagnostic(SnmpWeakCryptography, call.func.range());
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||||
|
---
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:3:25
|
||||||
|
|
|
||||||
|
1 | from pysnmp.hlapi import CommunityData
|
||||||
|
2 |
|
||||||
|
3 | CommunityData("public", mpModel=0) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
4 | CommunityData("public", mpModel=1) # S508
|
||||||
|
|
|
||||||
|
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:4:25
|
||||||
|
|
|
||||||
|
3 | CommunityData("public", mpModel=0) # S508
|
||||||
|
4 | CommunityData("public", mpModel=1) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
5 |
|
||||||
|
6 | CommunityData("public", mpModel=2) # OK
|
||||||
|
|
|
||||||
|
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:18:46
|
||||||
|
|
|
||||||
|
16 | import pysnmp.hlapi.auth
|
||||||
|
17 |
|
||||||
|
18 | pysnmp.hlapi.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
19 | pysnmp.hlapi.v1arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
20 | pysnmp.hlapi.v1arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
|
|
||||||
|
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:19:58
|
||||||
|
|
|
||||||
|
18 | pysnmp.hlapi.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
19 | pysnmp.hlapi.v1arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
20 | pysnmp.hlapi.v1arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
21 | pysnmp.hlapi.v1arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
|
|
||||||
|
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:20:53
|
||||||
|
|
|
||||||
|
18 | pysnmp.hlapi.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
19 | pysnmp.hlapi.v1arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
20 | pysnmp.hlapi.v1arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
21 | pysnmp.hlapi.v1arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
22 | pysnmp.hlapi.v3arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
|
|
||||||
|
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:21:45
|
||||||
|
|
|
||||||
|
19 | pysnmp.hlapi.v1arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
20 | pysnmp.hlapi.v1arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
21 | pysnmp.hlapi.v1arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
22 | pysnmp.hlapi.v3arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
23 | pysnmp.hlapi.v3arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
|
|
||||||
|
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:22:58
|
||||||
|
|
|
||||||
|
20 | pysnmp.hlapi.v1arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
21 | pysnmp.hlapi.v1arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
22 | pysnmp.hlapi.v3arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
23 | pysnmp.hlapi.v3arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
24 | pysnmp.hlapi.v3arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
|
|
||||||
|
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:23:53
|
||||||
|
|
|
||||||
|
21 | pysnmp.hlapi.v1arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
22 | pysnmp.hlapi.v3arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
23 | pysnmp.hlapi.v3arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
24 | pysnmp.hlapi.v3arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
25 | pysnmp.hlapi.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
|
|
||||||
|
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:24:45
|
||||||
|
|
|
||||||
|
22 | pysnmp.hlapi.v3arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
23 | pysnmp.hlapi.v3arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
24 | pysnmp.hlapi.v3arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
25 | pysnmp.hlapi.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
|
|
||||||
|
|
||||||
|
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||||
|
--> S508.py:25:43
|
||||||
|
|
|
||||||
|
23 | pysnmp.hlapi.v3arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||||
|
24 | pysnmp.hlapi.v3arch.CommunityData("public", mpModel=0) # S508
|
||||||
|
25 | pysnmp.hlapi.auth.CommunityData("public", mpModel=0) # S508
|
||||||
|
| ^^^^^^^^^
|
||||||
|
26 |
|
||||||
|
27 | pysnmp.hlapi.asyncio.CommunityData("public", mpModel=2) # OK
|
||||||
|
|
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||||
|
---
|
||||||
|
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||||
|
--> S509.py:4:12
|
||||||
|
|
|
||||||
|
4 | insecure = UsmUserData("securityName") # S509
|
||||||
|
| ^^^^^^^^^^^
|
||||||
|
5 | auth_no_priv = UsmUserData("securityName", "authName") # S509
|
||||||
|
|
|
||||||
|
|
||||||
|
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||||
|
--> S509.py:5:16
|
||||||
|
|
|
||||||
|
4 | insecure = UsmUserData("securityName") # S509
|
||||||
|
5 | auth_no_priv = UsmUserData("securityName", "authName") # S509
|
||||||
|
| ^^^^^^^^^^^
|
||||||
|
6 |
|
||||||
|
7 | less_insecure = UsmUserData("securityName", "authName", "privName") # OK
|
||||||
|
|
|
||||||
|
|
||||||
|
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||||
|
--> S509.py:15:1
|
||||||
|
|
|
||||||
|
13 | import pysnmp.hlapi.auth
|
||||||
|
14 |
|
||||||
|
15 | pysnmp.hlapi.asyncio.UsmUserData("user") # S509
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
16 | pysnmp.hlapi.v3arch.asyncio.UsmUserData("user") # S509
|
||||||
|
17 | pysnmp.hlapi.v3arch.asyncio.auth.UsmUserData("user") # S509
|
||||||
|
|
|
||||||
|
|
||||||
|
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||||
|
--> S509.py:16:1
|
||||||
|
|
|
||||||
|
15 | pysnmp.hlapi.asyncio.UsmUserData("user") # S509
|
||||||
|
16 | pysnmp.hlapi.v3arch.asyncio.UsmUserData("user") # S509
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
17 | pysnmp.hlapi.v3arch.asyncio.auth.UsmUserData("user") # S509
|
||||||
|
18 | pysnmp.hlapi.auth.UsmUserData("user") # S509
|
||||||
|
|
|
||||||
|
|
||||||
|
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||||
|
--> S509.py:17:1
|
||||||
|
|
|
||||||
|
15 | pysnmp.hlapi.asyncio.UsmUserData("user") # S509
|
||||||
|
16 | pysnmp.hlapi.v3arch.asyncio.UsmUserData("user") # S509
|
||||||
|
17 | pysnmp.hlapi.v3arch.asyncio.auth.UsmUserData("user") # S509
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
18 | pysnmp.hlapi.auth.UsmUserData("user") # S509
|
||||||
|
|
|
||||||
|
|
||||||
|
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||||
|
--> S509.py:18:1
|
||||||
|
|
|
||||||
|
16 | pysnmp.hlapi.v3arch.asyncio.UsmUserData("user") # S509
|
||||||
|
17 | pysnmp.hlapi.v3arch.asyncio.auth.UsmUserData("user") # S509
|
||||||
|
18 | pysnmp.hlapi.auth.UsmUserData("user") # S509
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
19 |
|
||||||
|
20 | pysnmp.hlapi.asyncio.UsmUserData("user", "authkey", "privkey") # OK
|
||||||
|
|
|
||||||
@@ -61,6 +61,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test_case(Rule::SplitStaticString, Path::new("SIM905.py"))]
|
#[test_case(Rule::SplitStaticString, Path::new("SIM905.py"))]
|
||||||
#[test_case(Rule::DictGetWithNoneDefault, Path::new("SIM910.py"))]
|
#[test_case(Rule::DictGetWithNoneDefault, Path::new("SIM910.py"))]
|
||||||
|
#[test_case(Rule::EnumerateForLoop, Path::new("SIM113.py"))]
|
||||||
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
|
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
|
||||||
let snapshot = format!(
|
let snapshot = format!(
|
||||||
"preview__{}_{}",
|
"preview__{}_{}",
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
use crate::preview::is_enumerate_for_loop_int_index_enabled;
|
||||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||||
use ruff_python_ast::statement_visitor::{StatementVisitor, walk_stmt};
|
use ruff_python_ast::statement_visitor::{StatementVisitor, walk_stmt};
|
||||||
use ruff_python_ast::{self as ast, Expr, Int, Number, Operator, Stmt};
|
use ruff_python_ast::{self as ast, Expr, Int, Number, Operator, Stmt};
|
||||||
|
use ruff_python_semantic::analyze::type_inference::{NumberLike, PythonType, ResolvedPythonType};
|
||||||
use ruff_python_semantic::analyze::typing;
|
use ruff_python_semantic::analyze::typing;
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
@@ -11,6 +13,9 @@ use crate::checkers::ast::Checker;
|
|||||||
/// Checks for `for` loops with explicit loop-index variables that can be replaced
|
/// Checks for `for` loops with explicit loop-index variables that can be replaced
|
||||||
/// with `enumerate()`.
|
/// with `enumerate()`.
|
||||||
///
|
///
|
||||||
|
/// In [preview], this rule checks for index variables initialized with any integer rather than only
|
||||||
|
/// a literal zero.
|
||||||
|
///
|
||||||
/// ## Why is this bad?
|
/// ## Why is this bad?
|
||||||
/// When iterating over a sequence, it's often desirable to keep track of the
|
/// When iterating over a sequence, it's often desirable to keep track of the
|
||||||
/// index of each element alongside the element itself. Prefer the `enumerate`
|
/// index of each element alongside the element itself. Prefer the `enumerate`
|
||||||
@@ -35,6 +40,8 @@ use crate::checkers::ast::Checker;
|
|||||||
///
|
///
|
||||||
/// ## References
|
/// ## References
|
||||||
/// - [Python documentation: `enumerate`](https://docs.python.org/3/library/functions.html#enumerate)
|
/// - [Python documentation: `enumerate`](https://docs.python.org/3/library/functions.html#enumerate)
|
||||||
|
///
|
||||||
|
/// [preview]: https://docs.astral.sh/ruff/preview/
|
||||||
#[derive(ViolationMetadata)]
|
#[derive(ViolationMetadata)]
|
||||||
#[violation_metadata(stable_since = "v0.2.0")]
|
#[violation_metadata(stable_since = "v0.2.0")]
|
||||||
pub(crate) struct EnumerateForLoop {
|
pub(crate) struct EnumerateForLoop {
|
||||||
@@ -82,17 +89,21 @@ pub(crate) fn enumerate_for_loop(checker: &Checker, for_stmt: &ast::StmtFor) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that the index variable was initialized to 0.
|
// Ensure that the index variable was initialized to 0 (or instance of `int` if preview is enabled).
|
||||||
let Some(value) = typing::find_binding_value(binding, checker.semantic()) else {
|
let Some(value) = typing::find_binding_value(binding, checker.semantic()) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
if !matches!(
|
if !(matches!(
|
||||||
value,
|
value,
|
||||||
Expr::NumberLiteral(ast::ExprNumberLiteral {
|
Expr::NumberLiteral(ast::ExprNumberLiteral {
|
||||||
value: Number::Int(Int::ZERO),
|
value: Number::Int(Int::ZERO),
|
||||||
..
|
..
|
||||||
})
|
})
|
||||||
) {
|
) || matches!(
|
||||||
|
ResolvedPythonType::from(value),
|
||||||
|
ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer))
|
||||||
|
) && is_enumerate_for_loop_int_index_enabled(checker.settings()))
|
||||||
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
|
||||||
|
---
|
||||||
|
SIM113 Use `enumerate()` for index variable `idx` in `for` loop
|
||||||
|
--> SIM113.py:6:9
|
||||||
|
|
|
||||||
|
4 | for x in range(5):
|
||||||
|
5 | g(x, idx)
|
||||||
|
6 | idx += 1
|
||||||
|
| ^^^^^^^^
|
||||||
|
7 | h(x)
|
||||||
|
|
|
||||||
|
|
||||||
|
SIM113 Use `enumerate()` for index variable `idx` in `for` loop
|
||||||
|
--> SIM113.py:17:9
|
||||||
|
|
|
||||||
|
15 | if g(x):
|
||||||
|
16 | break
|
||||||
|
17 | idx += 1
|
||||||
|
| ^^^^^^^^
|
||||||
|
18 | sum += h(x, idx)
|
||||||
|
|
|
||||||
|
|
||||||
|
SIM113 Use `enumerate()` for index variable `idx` in `for` loop
|
||||||
|
--> SIM113.py:27:9
|
||||||
|
|
|
||||||
|
25 | g(x)
|
||||||
|
26 | h(x, y)
|
||||||
|
27 | idx += 1
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
|
||||||
|
SIM113 Use `enumerate()` for index variable `idx` in `for` loop
|
||||||
|
--> SIM113.py:36:9
|
||||||
|
|
|
||||||
|
34 | for x in range(5):
|
||||||
|
35 | sum += h(x, idx)
|
||||||
|
36 | idx += 1
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
|
||||||
|
SIM113 Use `enumerate()` for index variable `idx` in `for` loop
|
||||||
|
--> SIM113.py:44:9
|
||||||
|
|
|
||||||
|
42 | for x in range(5):
|
||||||
|
43 | g(x, idx)
|
||||||
|
44 | idx += 1
|
||||||
|
| ^^^^^^^^
|
||||||
|
45 | h(x)
|
||||||
|
|
|
||||||
|
|
||||||
|
SIM113 Use `enumerate()` for index variable `idx` in `for` loop
|
||||||
|
--> SIM113.py:54:9
|
||||||
|
|
|
||||||
|
52 | for x in range(5):
|
||||||
|
53 | g(x, idx)
|
||||||
|
54 | idx += 1
|
||||||
|
| ^^^^^^^^
|
||||||
|
55 | h(x)
|
||||||
|
|
|
||||||
@@ -661,19 +661,31 @@ fn parse_parameters_numpy(content: &str, content_start: TextSize) -> Vec<Paramet
|
|||||||
.is_some_and(|first_char| !first_char.is_whitespace())
|
.is_some_and(|first_char| !first_char.is_whitespace())
|
||||||
{
|
{
|
||||||
if let Some(before_colon) = entry.split(':').next() {
|
if let Some(before_colon) = entry.split(':').next() {
|
||||||
let param = before_colon.trim_end();
|
let param_line = before_colon.trim_end();
|
||||||
let param_name = param.trim_start_matches('*');
|
|
||||||
if is_identifier(param_name) {
|
|
||||||
let param_start = line_start + indentation.text_len();
|
|
||||||
let param_end = param_start + param.text_len();
|
|
||||||
|
|
||||||
entries.push(ParameterEntry {
|
// Split on commas to handle comma-separated parameters
|
||||||
name: param_name,
|
let mut current_offset = TextSize::from(0);
|
||||||
range: TextRange::new(
|
for param_part in param_line.split(',') {
|
||||||
content_start + param_start,
|
let param_part_trimmed = param_part.trim();
|
||||||
content_start + param_end,
|
let param_name = param_part_trimmed.trim_start_matches('*');
|
||||||
),
|
if is_identifier(param_name) {
|
||||||
});
|
// Calculate the position of this specific parameter part within the line
|
||||||
|
// Account for leading whitespace that gets trimmed
|
||||||
|
let param_start_in_line = current_offset
|
||||||
|
+ (param_part.text_len() - param_part_trimmed.text_len());
|
||||||
|
let param_start =
|
||||||
|
line_start + indentation.text_len() + param_start_in_line;
|
||||||
|
|
||||||
|
entries.push(ParameterEntry {
|
||||||
|
name: param_name,
|
||||||
|
range: TextRange::at(
|
||||||
|
content_start + param_start,
|
||||||
|
param_part_trimmed.text_len(),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Update offset for next iteration: add the part length plus comma length
|
||||||
|
current_offset = current_offset + param_part.text_len() + ','.text_len();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -710,12 +722,30 @@ fn parse_raises(content: &str, style: Option<SectionStyle>) -> Vec<QualifiedName
|
|||||||
/// ```
|
/// ```
|
||||||
fn parse_raises_google(content: &str) -> Vec<QualifiedName<'_>> {
|
fn parse_raises_google(content: &str) -> Vec<QualifiedName<'_>> {
|
||||||
let mut entries: Vec<QualifiedName> = Vec::new();
|
let mut entries: Vec<QualifiedName> = Vec::new();
|
||||||
for potential in content.lines() {
|
let mut lines = content.lines().peekable();
|
||||||
let Some(colon_idx) = potential.find(':') else {
|
let Some(first) = lines.peek() else {
|
||||||
continue;
|
return entries;
|
||||||
};
|
};
|
||||||
let entry = potential[..colon_idx].trim();
|
let indentation = &first[..first.len() - first.trim_start().len()];
|
||||||
entries.push(QualifiedName::user_defined(entry));
|
for potential in lines {
|
||||||
|
if let Some(entry) = potential.strip_prefix(indentation) {
|
||||||
|
if let Some(first_char) = entry.chars().next() {
|
||||||
|
if !first_char.is_whitespace() {
|
||||||
|
if let Some(colon_idx) = entry.find(':') {
|
||||||
|
let entry = entry[..colon_idx].trim();
|
||||||
|
if !entry.is_empty() {
|
||||||
|
entries.push(QualifiedName::user_defined(entry));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If we can't strip the expected indentation, check if this is a dedented line
|
||||||
|
// (not blank) - if so, break early as we've reached the end of this section
|
||||||
|
if !potential.trim().is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
entries
|
entries
|
||||||
}
|
}
|
||||||
@@ -739,6 +769,12 @@ fn parse_raises_numpy(content: &str) -> Vec<QualifiedName<'_>> {
|
|||||||
let indentation = &dashes[..dashes.len() - dashes.trim_start().len()];
|
let indentation = &dashes[..dashes.len() - dashes.trim_start().len()];
|
||||||
for potential in lines {
|
for potential in lines {
|
||||||
if let Some(entry) = potential.strip_prefix(indentation) {
|
if let Some(entry) = potential.strip_prefix(indentation) {
|
||||||
|
// Check for Sphinx directives (lines starting with ..) - these indicate the end of the
|
||||||
|
// section. In numpy-style, exceptions are dedented to the same level as sphinx
|
||||||
|
// directives.
|
||||||
|
if entry.starts_with("..") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
if let Some(first_char) = entry.chars().next() {
|
if let Some(first_char) = entry.chars().next() {
|
||||||
if !first_char.is_whitespace() {
|
if !first_char.is_whitespace() {
|
||||||
entries.push(QualifiedName::user_defined(entry.trim_end()));
|
entries.push(QualifiedName::user_defined(entry.trim_end()));
|
||||||
|
|||||||
@@ -95,3 +95,23 @@ DOC502 Raised exception is not explicitly raised: `DivisionByZero`
|
|||||||
82 | return distance / time
|
82 | return distance / time
|
||||||
|
|
|
|
||||||
help: Remove `DivisionByZero` from the docstring
|
help: Remove `DivisionByZero` from the docstring
|
||||||
|
|
||||||
|
DOC502 Raised exception is not explicitly raised: `ZeroDivisionError`
|
||||||
|
--> DOC502_numpy.py:139:5
|
||||||
|
|
|
||||||
|
137 | # of the exceptions
|
||||||
|
138 | def foo():
|
||||||
|
139 | / """First line.
|
||||||
|
140 | |
|
||||||
|
141 | | Raises
|
||||||
|
142 | | ------
|
||||||
|
143 | | ValueError
|
||||||
|
144 | | some text
|
||||||
|
145 | | .. math:: e^{xception}
|
||||||
|
146 | | ZeroDivisionError
|
||||||
|
147 | | Will not be raised, DOC502
|
||||||
|
148 | | """
|
||||||
|
| |_______^
|
||||||
|
149 | raise ValueError
|
||||||
|
|
|
||||||
|
help: Remove `ZeroDivisionError` from the docstring
|
||||||
|
|||||||
@@ -187,3 +187,36 @@ DOC102 Documented parameter `a` is not in the function's signature
|
|||||||
302 | b
|
302 | b
|
||||||
|
|
|
|
||||||
help: Remove the extraneous parameter from the docstring
|
help: Remove the extraneous parameter from the docstring
|
||||||
|
|
||||||
|
DOC102 Documented parameter `x1` is not in the function's signature
|
||||||
|
--> DOC102_numpy.py:380:5
|
||||||
|
|
|
||||||
|
378 | Parameters
|
||||||
|
379 | ----------
|
||||||
|
380 | x1, x2 : object
|
||||||
|
| ^^
|
||||||
|
381 | Objects.
|
||||||
|
|
|
||||||
|
help: Remove the extraneous parameter from the docstring
|
||||||
|
|
||||||
|
DOC102 Documented parameter `x2` is not in the function's signature
|
||||||
|
--> DOC102_numpy.py:380:9
|
||||||
|
|
|
||||||
|
378 | Parameters
|
||||||
|
379 | ----------
|
||||||
|
380 | x1, x2 : object
|
||||||
|
| ^^
|
||||||
|
381 | Objects.
|
||||||
|
|
|
||||||
|
help: Remove the extraneous parameter from the docstring
|
||||||
|
|
||||||
|
DOC102 Documented parameter `extra_param` is not in the function's signature
|
||||||
|
--> DOC102_numpy.py:418:5
|
||||||
|
|
|
||||||
|
416 | x1, x2 : str
|
||||||
|
417 | String parameters for processing.
|
||||||
|
418 | extra_param : str
|
||||||
|
| ^^^^^^^^^^^
|
||||||
|
419 | Extra parameter not in signature.
|
||||||
|
|
|
||||||
|
help: Remove the extraneous parameter from the docstring
|
||||||
|
|||||||
@@ -766,11 +766,12 @@ pub(crate) fn deprecated_import(checker: &Checker, import_from_stmt: &StmtImport
|
|||||||
}
|
}
|
||||||
|
|
||||||
for operation in fixer.with_renames() {
|
for operation in fixer.with_renames() {
|
||||||
checker.report_diagnostic(
|
let mut diagnostic = checker.report_diagnostic(
|
||||||
DeprecatedImport {
|
DeprecatedImport {
|
||||||
deprecation: Deprecation::WithRename(operation),
|
deprecation: Deprecation::WithRename(operation),
|
||||||
},
|
},
|
||||||
import_from_stmt.range(),
|
import_from_stmt.range(),
|
||||||
);
|
);
|
||||||
|
diagnostic.add_primary_tag(ruff_db::diagnostic::DiagnosticTag::Deprecated);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,17 +2,15 @@ use ruff_diagnostics::{Applicability, Edit, Fix};
|
|||||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||||
use ruff_python_ast::{
|
use ruff_python_ast::{
|
||||||
self as ast, Expr, Stmt,
|
self as ast, Expr, Stmt,
|
||||||
relocate::relocate_expr,
|
|
||||||
visitor::{self, Visitor},
|
visitor::{self, Visitor},
|
||||||
};
|
};
|
||||||
use ruff_python_codegen::Generator;
|
use ruff_text_size::Ranged;
|
||||||
use ruff_text_size::{Ranged, TextRange};
|
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::fix::snippet::SourceCodeSnippet;
|
use crate::fix::snippet::SourceCodeSnippet;
|
||||||
use crate::importer::ImportRequest;
|
use crate::importer::ImportRequest;
|
||||||
use crate::rules::refurb::helpers::{FileOpen, find_file_opens};
|
use crate::rules::refurb::helpers::{FileOpen, find_file_opens};
|
||||||
use crate::{FixAvailability, Violation};
|
use crate::{FixAvailability, Locator, Violation};
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for uses of `open` and `write` that can be replaced by `pathlib`
|
/// Checks for uses of `open` and `write` that can be replaced by `pathlib`
|
||||||
@@ -129,7 +127,7 @@ impl<'a> Visitor<'a> for WriteMatcher<'a, '_> {
|
|||||||
let open = self.candidates.remove(open);
|
let open = self.candidates.remove(open);
|
||||||
|
|
||||||
if self.loop_counter == 0 {
|
if self.loop_counter == 0 {
|
||||||
let suggestion = make_suggestion(&open, content, self.checker.generator());
|
let suggestion = make_suggestion(&open, content, self.checker.locator());
|
||||||
|
|
||||||
let mut diagnostic = self.checker.report_diagnostic(
|
let mut diagnostic = self.checker.report_diagnostic(
|
||||||
WriteWholeFile {
|
WriteWholeFile {
|
||||||
@@ -172,27 +170,21 @@ fn match_write_call(expr: &Expr) -> Option<(&Expr, &Expr)> {
|
|||||||
Some((&*attr.value, call.arguments.args.first()?))
|
Some((&*attr.value, call.arguments.args.first()?))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_suggestion(open: &FileOpen<'_>, arg: &Expr, generator: Generator) -> String {
|
fn make_suggestion(open: &FileOpen<'_>, arg: &Expr, locator: &Locator) -> String {
|
||||||
let name = ast::ExprName {
|
let method_name = open.mode.pathlib_method();
|
||||||
id: open.mode.pathlib_method(),
|
let arg_code = locator.slice(arg.range());
|
||||||
ctx: ast::ExprContext::Load,
|
|
||||||
range: TextRange::default(),
|
if open.keywords.is_empty() {
|
||||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
format!("{method_name}({arg_code})")
|
||||||
};
|
} else {
|
||||||
let mut arg = arg.clone();
|
format!(
|
||||||
relocate_expr(&mut arg, TextRange::default());
|
"{method_name}({arg_code}, {})",
|
||||||
let call = ast::ExprCall {
|
itertools::join(
|
||||||
func: Box::new(name.into()),
|
open.keywords.iter().map(|kw| locator.slice(kw.range())),
|
||||||
arguments: ast::Arguments {
|
", "
|
||||||
args: Box::new([arg]),
|
)
|
||||||
keywords: open.keywords.iter().copied().cloned().collect(),
|
)
|
||||||
range: TextRange::default(),
|
}
|
||||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
|
||||||
},
|
|
||||||
range: TextRange::default(),
|
|
||||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
|
||||||
};
|
|
||||||
generator.expr(&call.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_fix(
|
fn generate_fix(
|
||||||
|
|||||||
@@ -279,3 +279,34 @@ help: Replace with `Path("test.json")....`
|
|||||||
- with open("test.json", "wb") as f:
|
- with open("test.json", "wb") as f:
|
||||||
- f.write(json.dumps(data, indent=4).encode("utf-8"))
|
- f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
155 + pathlib.Path("test.json").write_bytes(json.dumps(data, indent=4).encode("utf-8"))
|
155 + pathlib.Path("test.json").write_bytes(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
|
156 |
|
||||||
|
157 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||||
|
158 | with open("tmp_path/pyproject.toml", "w") as f:
|
||||||
|
|
||||||
|
FURB103 [*] `open` and `write` should be replaced by `Path("tmp_path/pyproject.toml")....`
|
||||||
|
--> FURB103.py:158:6
|
||||||
|
|
|
||||||
|
157 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||||
|
158 | with open("tmp_path/pyproject.toml", "w") as f:
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
159 | f.write(dedent(
|
||||||
|
160 | """
|
||||||
|
|
|
||||||
|
help: Replace with `Path("tmp_path/pyproject.toml")....`
|
||||||
|
148 |
|
||||||
|
149 | # See: https://github.com/astral-sh/ruff/issues/20785
|
||||||
|
150 | import json
|
||||||
|
151 + import pathlib
|
||||||
|
152 |
|
||||||
|
153 | data = {"price": 100}
|
||||||
|
154 |
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
156 | f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
|
157 |
|
||||||
|
158 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||||
|
- with open("tmp_path/pyproject.toml", "w") as f:
|
||||||
|
- f.write(dedent(
|
||||||
|
159 + pathlib.Path("tmp_path/pyproject.toml").write_text(dedent(
|
||||||
|
160 | """
|
||||||
|
161 | [project]
|
||||||
|
162 | other = 1.234
|
||||||
|
|||||||
@@ -209,3 +209,34 @@ help: Replace with `Path("test.json")....`
|
|||||||
- with open("test.json", "wb") as f:
|
- with open("test.json", "wb") as f:
|
||||||
- f.write(json.dumps(data, indent=4).encode("utf-8"))
|
- f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
155 + pathlib.Path("test.json").write_bytes(json.dumps(data, indent=4).encode("utf-8"))
|
155 + pathlib.Path("test.json").write_bytes(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
|
156 |
|
||||||
|
157 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||||
|
158 | with open("tmp_path/pyproject.toml", "w") as f:
|
||||||
|
|
||||||
|
FURB103 [*] `open` and `write` should be replaced by `Path("tmp_path/pyproject.toml")....`
|
||||||
|
--> FURB103.py:158:6
|
||||||
|
|
|
||||||
|
157 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||||
|
158 | with open("tmp_path/pyproject.toml", "w") as f:
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
159 | f.write(dedent(
|
||||||
|
160 | """
|
||||||
|
|
|
||||||
|
help: Replace with `Path("tmp_path/pyproject.toml")....`
|
||||||
|
148 |
|
||||||
|
149 | # See: https://github.com/astral-sh/ruff/issues/20785
|
||||||
|
150 | import json
|
||||||
|
151 + import pathlib
|
||||||
|
152 |
|
||||||
|
153 | data = {"price": 100}
|
||||||
|
154 |
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
156 | f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
|
157 |
|
||||||
|
158 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||||
|
- with open("tmp_path/pyproject.toml", "w") as f:
|
||||||
|
- f.write(dedent(
|
||||||
|
159 + pathlib.Path("tmp_path/pyproject.toml").write_text(dedent(
|
||||||
|
160 | """
|
||||||
|
161 | [project]
|
||||||
|
162 | other = 1.234
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||||
use ruff_python_ast::{self as ast, Expr};
|
use ruff_python_ast::{self as ast, Expr};
|
||||||
use ruff_python_literal::cformat::{CFormatPart, CFormatString, CFormatType};
|
use ruff_python_literal::cformat::{
|
||||||
|
CConversionFlags, CFormatPart, CFormatSpec, CFormatString, CFormatType,
|
||||||
|
};
|
||||||
use ruff_python_literal::format::FormatConversion;
|
use ruff_python_literal::format::FormatConversion;
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
@@ -195,7 +197,8 @@ pub(crate) fn logging_eager_conversion(checker: &Checker, call: &ast::ExprCall)
|
|||||||
}
|
}
|
||||||
// %s with oct() - suggest using %#o instead
|
// %s with oct() - suggest using %#o instead
|
||||||
FormatConversion::Str
|
FormatConversion::Str
|
||||||
if checker.semantic().match_builtin_expr(func.as_ref(), "oct") =>
|
if checker.semantic().match_builtin_expr(func.as_ref(), "oct")
|
||||||
|
&& !has_complex_conversion_specifier(spec) =>
|
||||||
{
|
{
|
||||||
checker.report_diagnostic(
|
checker.report_diagnostic(
|
||||||
LoggingEagerConversion {
|
LoggingEagerConversion {
|
||||||
@@ -207,7 +210,8 @@ pub(crate) fn logging_eager_conversion(checker: &Checker, call: &ast::ExprCall)
|
|||||||
}
|
}
|
||||||
// %s with hex() - suggest using %#x instead
|
// %s with hex() - suggest using %#x instead
|
||||||
FormatConversion::Str
|
FormatConversion::Str
|
||||||
if checker.semantic().match_builtin_expr(func.as_ref(), "hex") =>
|
if checker.semantic().match_builtin_expr(func.as_ref(), "hex")
|
||||||
|
&& !has_complex_conversion_specifier(spec) =>
|
||||||
{
|
{
|
||||||
checker.report_diagnostic(
|
checker.report_diagnostic(
|
||||||
LoggingEagerConversion {
|
LoggingEagerConversion {
|
||||||
@@ -222,3 +226,23 @@ pub(crate) fn logging_eager_conversion(checker: &Checker, call: &ast::ExprCall)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check if a conversion specifier has complex flags or precision that make `oct()` or `hex()` necessary.
|
||||||
|
///
|
||||||
|
/// Returns `true` if any of these conditions are met:
|
||||||
|
/// - Flag `0` (zero-pad) is used, flag `-` (left-adjust) is not used, and minimum width is specified
|
||||||
|
/// - Flag ` ` (blank sign) is used
|
||||||
|
/// - Flag `+` (sign char) is used
|
||||||
|
/// - Precision is specified
|
||||||
|
fn has_complex_conversion_specifier(spec: &CFormatSpec) -> bool {
|
||||||
|
if spec.flags.intersects(CConversionFlags::ZERO_PAD)
|
||||||
|
&& !spec.flags.intersects(CConversionFlags::LEFT_ADJUST)
|
||||||
|
&& spec.min_field_width.is_some()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
spec.flags
|
||||||
|
.intersects(CConversionFlags::BLANK_SIGN | CConversionFlags::SIGN_CHAR)
|
||||||
|
|| spec.precision.is_some()
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use ruff_python_ast::{self as ast, Stmt};
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||||
|
use ruff_python_ast::{self as ast, Stmt};
|
||||||
use ruff_python_semantic::analyze::typing::{is_immutable_annotation, is_mutable_expr};
|
use ruff_python_semantic::analyze::typing::{is_immutable_annotation, is_mutable_expr};
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
@@ -96,6 +97,9 @@ impl Violation for MutableClassDefault {
|
|||||||
|
|
||||||
/// RUF012
|
/// RUF012
|
||||||
pub(crate) fn mutable_class_default(checker: &Checker, class_def: &ast::StmtClassDef) {
|
pub(crate) fn mutable_class_default(checker: &Checker, class_def: &ast::StmtClassDef) {
|
||||||
|
// Collect any `ClassVar`s we find in case they get reassigned later.
|
||||||
|
let mut class_var_targets = FxHashSet::default();
|
||||||
|
|
||||||
for statement in &class_def.body {
|
for statement in &class_def.body {
|
||||||
match statement {
|
match statement {
|
||||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||||
@@ -104,6 +108,12 @@ pub(crate) fn mutable_class_default(checker: &Checker, class_def: &ast::StmtClas
|
|||||||
value: Some(value),
|
value: Some(value),
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
|
if let ast::Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
|
||||||
|
if is_class_var_annotation(annotation, checker.semantic()) {
|
||||||
|
class_var_targets.insert(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !is_special_attribute(target)
|
if !is_special_attribute(target)
|
||||||
&& is_mutable_expr(value, checker.semantic())
|
&& is_mutable_expr(value, checker.semantic())
|
||||||
&& !is_class_var_annotation(annotation, checker.semantic())
|
&& !is_class_var_annotation(annotation, checker.semantic())
|
||||||
@@ -123,8 +133,12 @@ pub(crate) fn mutable_class_default(checker: &Checker, class_def: &ast::StmtClas
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::Assign(ast::StmtAssign { value, targets, .. }) => {
|
Stmt::Assign(ast::StmtAssign { value, targets, .. }) => {
|
||||||
if !targets.iter().all(is_special_attribute)
|
if !targets.iter().all(|target| {
|
||||||
&& is_mutable_expr(value, checker.semantic())
|
is_special_attribute(target)
|
||||||
|
|| target
|
||||||
|
.as_name_expr()
|
||||||
|
.is_some_and(|name| class_var_targets.contains(&name.id))
|
||||||
|
}) && is_mutable_expr(value, checker.semantic())
|
||||||
{
|
{
|
||||||
// Avoid, e.g., Pydantic and msgspec models, which end up copying defaults on instance creation.
|
// Avoid, e.g., Pydantic and msgspec models, which end up copying defaults on instance creation.
|
||||||
if has_default_copy_semantics(class_def, checker.semantic()) {
|
if has_default_copy_semantics(class_def, checker.semantic()) {
|
||||||
|
|||||||
@@ -294,19 +294,33 @@ impl CellOffsets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the given range contains a cell boundary.
|
/// Returns `true` if the given range contains a cell boundary.
|
||||||
|
///
|
||||||
|
/// A range starting at the cell boundary isn't considered to contain the cell boundary
|
||||||
|
/// as it starts right after it. A range starting before a cell boundary
|
||||||
|
/// and ending exactly at the boundary is considered to contain the cell boundary.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// Cell 1:
|
||||||
|
///
|
||||||
|
/// ```py
|
||||||
|
/// import c
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Cell 2:
|
||||||
|
///
|
||||||
|
/// ```py
|
||||||
|
/// import os
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// The range `import c`..`import os`, contains a cell boundary because it starts before cell 2 and ends in cell 2 (`end=cell2_boundary`).
|
||||||
|
/// The `import os` contains no cell boundary because it starts at the start of cell 2 (at the cell boundary) but doesn't cross into another cell.
|
||||||
pub fn has_cell_boundary(&self, range: TextRange) -> bool {
|
pub fn has_cell_boundary(&self, range: TextRange) -> bool {
|
||||||
self.binary_search_by(|offset| {
|
let after_range_start = self.partition_point(|offset| *offset <= range.start());
|
||||||
if range.start() <= *offset {
|
let Some(boundary) = self.get(after_range_start).copied() else {
|
||||||
if range.end() < *offset {
|
return false;
|
||||||
std::cmp::Ordering::Greater
|
};
|
||||||
} else {
|
|
||||||
std::cmp::Ordering::Equal
|
range.contains_inclusive(boundary)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
std::cmp::Ordering::Less
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.is_ok()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator over [`TextRange`]s covered by each cell.
|
/// Returns an iterator over [`TextRange`]s covered by each cell.
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ impl NotebookIndex {
|
|||||||
|
|
||||||
/// Returns an iterator over the starting rows of each cell (1-based).
|
/// Returns an iterator over the starting rows of each cell (1-based).
|
||||||
///
|
///
|
||||||
/// This yields one entry per Python cell (skipping over Makrdown cell).
|
/// This yields one entry per Python cell (skipping over Markdown cell).
|
||||||
pub fn iter(&self) -> impl Iterator<Item = CellStart> + '_ {
|
pub fn iter(&self) -> impl Iterator<Item = CellStart> + '_ {
|
||||||
self.cell_starts.iter().copied()
|
self.cell_starts.iter().copied()
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ impl NotebookIndex {
|
|||||||
/// Translates the given [`LineColumn`] based on the indexing table.
|
/// Translates the given [`LineColumn`] based on the indexing table.
|
||||||
///
|
///
|
||||||
/// This will translate the row/column in the concatenated source code
|
/// This will translate the row/column in the concatenated source code
|
||||||
/// to the row/column in the Jupyter Notebook.
|
/// to the row/column in the Jupyter Notebook cell.
|
||||||
pub fn translate_line_column(&self, source_location: &LineColumn) -> LineColumn {
|
pub fn translate_line_column(&self, source_location: &LineColumn) -> LineColumn {
|
||||||
LineColumn {
|
LineColumn {
|
||||||
line: self
|
line: self
|
||||||
@@ -60,7 +60,7 @@ impl NotebookIndex {
|
|||||||
/// Translates the given [`SourceLocation`] based on the indexing table.
|
/// Translates the given [`SourceLocation`] based on the indexing table.
|
||||||
///
|
///
|
||||||
/// This will translate the line/character in the concatenated source code
|
/// This will translate the line/character in the concatenated source code
|
||||||
/// to the line/character in the Jupyter Notebook.
|
/// to the line/character in the Jupyter Notebook cell.
|
||||||
pub fn translate_source_location(&self, source_location: &SourceLocation) -> SourceLocation {
|
pub fn translate_source_location(&self, source_location: &SourceLocation) -> SourceLocation {
|
||||||
SourceLocation {
|
SourceLocation {
|
||||||
line: self
|
line: self
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ use thiserror::Error;
|
|||||||
|
|
||||||
use ruff_diagnostics::{SourceMap, SourceMarker};
|
use ruff_diagnostics::{SourceMap, SourceMarker};
|
||||||
use ruff_source_file::{NewlineWithTrailingNewline, OneIndexed, UniversalNewlineIterator};
|
use ruff_source_file::{NewlineWithTrailingNewline, OneIndexed, UniversalNewlineIterator};
|
||||||
use ruff_text_size::TextSize;
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
|
|
||||||
use crate::cell::CellOffsets;
|
use crate::cell::CellOffsets;
|
||||||
use crate::index::NotebookIndex;
|
use crate::index::NotebookIndex;
|
||||||
@@ -294,7 +294,7 @@ impl Notebook {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build and return the [`JupyterIndex`].
|
/// Build and return the [`NotebookIndex`].
|
||||||
///
|
///
|
||||||
/// ## Notes
|
/// ## Notes
|
||||||
///
|
///
|
||||||
@@ -388,6 +388,21 @@ impl Notebook {
|
|||||||
&self.cell_offsets
|
&self.cell_offsets
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the start offset of the cell at index `cell` in the concatenated
|
||||||
|
/// text document.
|
||||||
|
pub fn cell_offset(&self, cell: OneIndexed) -> Option<TextSize> {
|
||||||
|
self.cell_offsets.get(cell.to_zero_indexed()).copied()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the text range in the concatenated document of the cell
|
||||||
|
/// with index `cell`.
|
||||||
|
pub fn cell_range(&self, cell: OneIndexed) -> Option<TextRange> {
|
||||||
|
let start = self.cell_offsets.get(cell.to_zero_indexed()).copied()?;
|
||||||
|
let end = self.cell_offsets.get(cell.to_zero_indexed() + 1).copied()?;
|
||||||
|
|
||||||
|
Some(TextRange::new(start, end))
|
||||||
|
}
|
||||||
|
|
||||||
/// Return `true` if the notebook has a trailing newline, `false` otherwise.
|
/// Return `true` if the notebook has a trailing newline, `false` otherwise.
|
||||||
pub fn trailing_newline(&self) -> bool {
|
pub fn trailing_newline(&self) -> bool {
|
||||||
self.trailing_newline
|
self.trailing_newline
|
||||||
|
|||||||
@@ -169,3 +169,53 @@ result = (
|
|||||||
# dangling before dot
|
# dangling before dot
|
||||||
.b # trailing end-of-line
|
.b # trailing end-of-line
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Regression test for https://github.com/astral-sh/ruff/issues/19350
|
||||||
|
variable = (
|
||||||
|
(something) # a comment
|
||||||
|
.first_method("some string")
|
||||||
|
)
|
||||||
|
|
||||||
|
variable = (
|
||||||
|
something # a commentdddddddddddddddddddddddddddddd
|
||||||
|
.first_method("some string")
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
(something) # a commentdddddddddddddddddddddddddddddd
|
||||||
|
.first_method("some string")
|
||||||
|
): pass
|
||||||
|
|
||||||
|
variable = (
|
||||||
|
(something # a comment
|
||||||
|
).first_method("some string")
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
(something # a comment
|
||||||
|
).first_method("some string") # second comment
|
||||||
|
): pass
|
||||||
|
|
||||||
|
variable = ( # 1
|
||||||
|
# 2
|
||||||
|
(something) # 3
|
||||||
|
# 4
|
||||||
|
.first_method("some string") # 5
|
||||||
|
# 6
|
||||||
|
) # 7
|
||||||
|
|
||||||
|
|
||||||
|
if (
|
||||||
|
(something
|
||||||
|
# trailing own line on value
|
||||||
|
)
|
||||||
|
.first_method("some string")
|
||||||
|
): ...
|
||||||
|
|
||||||
|
variable = (
|
||||||
|
(something
|
||||||
|
# 1
|
||||||
|
) # 2
|
||||||
|
.first_method("some string")
|
||||||
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"preview": "disabled"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"preview": "enabled"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
@@ -125,13 +125,6 @@ lambda a, /, c: a
|
|||||||
*x: x
|
*x: x
|
||||||
)
|
)
|
||||||
|
|
||||||
(
|
|
||||||
lambda
|
|
||||||
# comment
|
|
||||||
*x,
|
|
||||||
**y: x
|
|
||||||
)
|
|
||||||
|
|
||||||
(
|
(
|
||||||
lambda
|
lambda
|
||||||
# comment 1
|
# comment 1
|
||||||
@@ -142,17 +135,6 @@ lambda a, /, c: a
|
|||||||
x
|
x
|
||||||
)
|
)
|
||||||
|
|
||||||
(
|
|
||||||
lambda
|
|
||||||
# comment 1
|
|
||||||
*
|
|
||||||
# comment 2
|
|
||||||
x,
|
|
||||||
**y:
|
|
||||||
# comment 3
|
|
||||||
x
|
|
||||||
)
|
|
||||||
|
|
||||||
(
|
(
|
||||||
lambda # comment 1
|
lambda # comment 1
|
||||||
* # comment 2
|
* # comment 2
|
||||||
@@ -160,14 +142,6 @@ lambda a, /, c: a
|
|||||||
x
|
x
|
||||||
)
|
)
|
||||||
|
|
||||||
(
|
|
||||||
lambda # comment 1
|
|
||||||
* # comment 2
|
|
||||||
x,
|
|
||||||
y: # comment 3
|
|
||||||
x
|
|
||||||
)
|
|
||||||
|
|
||||||
lambda *x\
|
lambda *x\
|
||||||
:x
|
:x
|
||||||
|
|
||||||
@@ -222,17 +196,6 @@ lambda: ( # comment
|
|||||||
x
|
x
|
||||||
)
|
)
|
||||||
|
|
||||||
(
|
|
||||||
lambda # 1
|
|
||||||
# 2
|
|
||||||
x, # 3
|
|
||||||
# 4
|
|
||||||
y
|
|
||||||
: # 5
|
|
||||||
# 6
|
|
||||||
x
|
|
||||||
)
|
|
||||||
|
|
||||||
(
|
(
|
||||||
lambda
|
lambda
|
||||||
x,
|
x,
|
||||||
@@ -241,71 +204,6 @@ lambda: ( # comment
|
|||||||
z
|
z
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Leading
|
|
||||||
lambda x: (
|
|
||||||
lambda y: lambda z: x
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ y
|
|
||||||
+ z # Trailing
|
|
||||||
) # Trailing
|
|
||||||
|
|
||||||
|
|
||||||
# Leading
|
|
||||||
lambda x: lambda y: lambda z: [
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
y,
|
|
||||||
z
|
|
||||||
] # Trailing
|
|
||||||
# Trailing
|
|
||||||
|
|
||||||
lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d
|
lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d
|
||||||
|
|
||||||
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
|
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
|
||||||
|
|||||||
@@ -193,3 +193,58 @@ def foo():
|
|||||||
not (aaaaaaaaaaaaaaaaaaaaa[bbbbbbbb, ccccccc]) and dddddddddd < eeeeeeeeeeeeeee
|
not (aaaaaaaaaaaaaaaaaaaaa[bbbbbbbb, ccccccc]) and dddddddddd < eeeeeeeeeeeeeee
|
||||||
):
|
):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Regression tests for https://github.com/astral-sh/ruff/issues/19226
|
||||||
|
if '' and (not #
|
||||||
|
0):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if '' and (not #
|
||||||
|
(0)
|
||||||
|
):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if '' and (not
|
||||||
|
( #
|
||||||
|
0
|
||||||
|
)):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if (
|
||||||
|
not
|
||||||
|
# comment
|
||||||
|
(a)):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not ( # comment
|
||||||
|
a):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not (
|
||||||
|
# comment
|
||||||
|
(a)):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not (
|
||||||
|
# comment
|
||||||
|
a):
|
||||||
|
pass
|
||||||
|
|
||||||
|
not (# comment
|
||||||
|
(a))
|
||||||
|
|
||||||
|
(-#comment
|
||||||
|
(a))
|
||||||
|
|
||||||
|
if ( # a
|
||||||
|
# b
|
||||||
|
not # c
|
||||||
|
# d
|
||||||
|
( # e
|
||||||
|
# f
|
||||||
|
a # g
|
||||||
|
# h
|
||||||
|
) # i
|
||||||
|
# j
|
||||||
|
):
|
||||||
|
pass
|
||||||
|
|||||||
149
crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_skip/compound_one_liners.py
vendored
Normal file
149
crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_skip/compound_one_liners.py
vendored
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
# Test cases for fmt: skip on compound statements that fit on one line
|
||||||
|
|
||||||
|
# Basic single-line compound statements
|
||||||
|
def simple_func(): return "hello" # fmt: skip
|
||||||
|
if True: print("condition met") # fmt: skip
|
||||||
|
for i in range(5): print(i) # fmt: skip
|
||||||
|
while x < 10: x += 1 # fmt: skip
|
||||||
|
|
||||||
|
# With expressions that would normally trigger formatting
|
||||||
|
def long_params(a, b, c, d, e, f, g): return a + b + c + d + e + f + g # fmt: skip
|
||||||
|
if some_very_long_condition_that_might_wrap: do_something_else_that_is_long() # fmt: skip
|
||||||
|
|
||||||
|
# Nested compound statements (outer should be preserved)
|
||||||
|
if True:
|
||||||
|
for i in range(10): print(i) # fmt: skip
|
||||||
|
|
||||||
|
# Multiple statements in body (should not apply - multiline)
|
||||||
|
if True:
|
||||||
|
x = 1
|
||||||
|
y = 2 # fmt: skip
|
||||||
|
|
||||||
|
# With decorators - decorated function on one line
|
||||||
|
@overload
|
||||||
|
def decorated_func(x: int) -> str: return str(x) # fmt: skip
|
||||||
|
|
||||||
|
@property
|
||||||
|
def prop_method(self): return self._value # fmt: skip
|
||||||
|
|
||||||
|
# Class definitions on one line
|
||||||
|
class SimpleClass: pass # fmt: skip
|
||||||
|
class GenericClass(Generic[T]): pass # fmt: skip
|
||||||
|
|
||||||
|
# Try/except blocks
|
||||||
|
try: risky_operation() # fmt: skip
|
||||||
|
except ValueError: handle_error() # fmt: skip
|
||||||
|
except: handle_any_error() # fmt: skip
|
||||||
|
else: success_case() # fmt: skip
|
||||||
|
finally: cleanup() # fmt: skip
|
||||||
|
|
||||||
|
# Match statements (Python 3.10+)
|
||||||
|
match value:
|
||||||
|
case 1: print("one") # fmt: skip
|
||||||
|
case _: print("other") # fmt: skip
|
||||||
|
|
||||||
|
# With statements
|
||||||
|
with open("file.txt") as f: content = f.read() # fmt: skip
|
||||||
|
with context_manager() as cm: result = cm.process() # fmt: skip
|
||||||
|
|
||||||
|
# Async variants
|
||||||
|
async def async_func(): return await some_call() # fmt: skip
|
||||||
|
async for item in async_iterator(): await process(item) # fmt: skip
|
||||||
|
async with async_context() as ctx: await ctx.work() # fmt: skip
|
||||||
|
|
||||||
|
# Complex expressions that would normally format
|
||||||
|
def complex_expr(): return [x for x in range(100) if x % 2 == 0 and x > 50] # fmt: skip
|
||||||
|
if condition_a and condition_b or (condition_c and not condition_d): execute_complex_logic() # fmt: skip
|
||||||
|
|
||||||
|
# Edge case: comment positioning
|
||||||
|
def func_with_comment(): # some comment
|
||||||
|
return "value" # fmt: skip
|
||||||
|
|
||||||
|
# Edge case: multiple fmt: skip (only last one should matter)
|
||||||
|
def multiple_skip(): return "test" # fmt: skip # fmt: skip
|
||||||
|
|
||||||
|
# Should NOT be affected (already multiline)
|
||||||
|
def multiline_func():
|
||||||
|
return "this should format normally"
|
||||||
|
|
||||||
|
if long_condition_that_spans \
|
||||||
|
and continues_on_next_line:
|
||||||
|
print("multiline condition")
|
||||||
|
|
||||||
|
# Mix of skipped and non-skipped
|
||||||
|
for i in range(10): print(f"item {i}") # fmt: skip
|
||||||
|
for j in range(5):
|
||||||
|
print(f"formatted item {j}")
|
||||||
|
|
||||||
|
# With trailing comma that would normally be removed
|
||||||
|
def trailing_comma_func(a, b, c,): return a + b + c # fmt: skip
|
||||||
|
|
||||||
|
# Dictionary/list comprehensions
|
||||||
|
def dict_comp(): return {k: v for k, v in items.items() if v is not None} # fmt: skip
|
||||||
|
def list_comp(): return [x * 2 for x in numbers if x > threshold_value] # fmt: skip
|
||||||
|
|
||||||
|
# Lambda in one-liner
|
||||||
|
def with_lambda(): return lambda x, y, z: x + y + z if all([x, y, z]) else None # fmt: skip
|
||||||
|
|
||||||
|
# String formatting that would normally be reformatted
|
||||||
|
def format_string(): return f"Hello {name}, you have {count} items in your cart totaling ${total:.2f}" # fmt: skip
|
||||||
|
|
||||||
|
# loop else clauses
|
||||||
|
for i in range(2): print(i) # fmt: skip
|
||||||
|
else: print("this") # fmt: skip
|
||||||
|
|
||||||
|
|
||||||
|
while foo(): print(i) # fmt: skip
|
||||||
|
else: print("this") # fmt: skip
|
||||||
|
|
||||||
|
# again but only the first skip
|
||||||
|
for i in range(2): print(i) # fmt: skip
|
||||||
|
else: print("this")
|
||||||
|
|
||||||
|
|
||||||
|
while foo(): print(i) # fmt: skip
|
||||||
|
else: print("this")
|
||||||
|
|
||||||
|
# again but only the second skip
|
||||||
|
for i in range(2): print(i)
|
||||||
|
else: print("this") # fmt: skip
|
||||||
|
|
||||||
|
|
||||||
|
while foo(): print(i)
|
||||||
|
else: print("this") # fmt: skip
|
||||||
|
|
||||||
|
# multiple statements in body
|
||||||
|
if True: print("this"); print("that") # fmt: skip
|
||||||
|
|
||||||
|
# Examples with more comments
|
||||||
|
|
||||||
|
try: risky_operation() # fmt: skip
|
||||||
|
# leading 1
|
||||||
|
except ValueError: handle_error() # fmt: skip
|
||||||
|
# leading 2
|
||||||
|
except: handle_any_error() # fmt: skip
|
||||||
|
# leading 3
|
||||||
|
else: success_case() # fmt: skip
|
||||||
|
# leading 4
|
||||||
|
finally: cleanup() # fmt: skip
|
||||||
|
# trailing
|
||||||
|
|
||||||
|
# multi-line before colon (should remain as is)
|
||||||
|
if (
|
||||||
|
long_condition
|
||||||
|
): a + b # fmt: skip
|
||||||
|
|
||||||
|
# over-indented comment example
|
||||||
|
# See https://github.com/astral-sh/ruff/pull/20633#issuecomment-3453288910
|
||||||
|
# and https://github.com/astral-sh/ruff/pull/21185
|
||||||
|
|
||||||
|
for x in it: foo()
|
||||||
|
# comment
|
||||||
|
else: bar() # fmt: skip
|
||||||
|
|
||||||
|
|
||||||
|
if this(
|
||||||
|
'is a long',
|
||||||
|
# commented
|
||||||
|
'condition'
|
||||||
|
): with_a_skip # fmt: skip
|
||||||
@@ -294,3 +294,39 @@ if parent_body:
|
|||||||
# d
|
# d
|
||||||
# e
|
# e
|
||||||
#f
|
#f
|
||||||
|
|
||||||
|
# Compare behavior with `while`/`else` comment placement
|
||||||
|
|
||||||
|
if True: pass
|
||||||
|
# 1
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# 2
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True: pass
|
||||||
|
# 3
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True: pass
|
||||||
|
# 4
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# 5
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
first;second
|
||||||
|
# 6
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|||||||
@@ -28,3 +28,37 @@ while (
|
|||||||
and anotherCondition or aThirdCondition # trailing third condition
|
and anotherCondition or aThirdCondition # trailing third condition
|
||||||
): # comment
|
): # comment
|
||||||
print("Do something")
|
print("Do something")
|
||||||
|
|
||||||
|
while True: pass
|
||||||
|
# 1
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# 2
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True: pass
|
||||||
|
# 3
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True: pass
|
||||||
|
# 4
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# 5
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True:
|
||||||
|
first;second
|
||||||
|
# 6
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|||||||
@@ -1042,4 +1042,33 @@ else: # trailing comment
|
|||||||
|
|
||||||
assert_debug_snapshot!(comments.debug(test_case.source_code));
|
assert_debug_snapshot!(comments.debug(test_case.source_code));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn while_else_indented_comment_between_branches() {
|
||||||
|
let source = r"while True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
";
|
||||||
|
let test_case = CommentsTestCase::from_code(source);
|
||||||
|
|
||||||
|
let comments = test_case.to_comments();
|
||||||
|
|
||||||
|
assert_debug_snapshot!(comments.debug(test_case.source_code));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn while_else_very_indented_comment_between_branches() {
|
||||||
|
let source = r"while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
";
|
||||||
|
let test_case = CommentsTestCase::from_code(source);
|
||||||
|
|
||||||
|
let comments = test_case.to_comments();
|
||||||
|
|
||||||
|
assert_debug_snapshot!(comments.debug(test_case.source_code));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use ruff_python_trivia::{
|
|||||||
find_only_token_in_range, first_non_trivia_token, indentation_at_offset,
|
find_only_token_in_range, first_non_trivia_token, indentation_at_offset,
|
||||||
};
|
};
|
||||||
use ruff_source_file::LineRanges;
|
use ruff_source_file::LineRanges;
|
||||||
use ruff_text_size::{Ranged, TextLen, TextRange};
|
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
|
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
|
||||||
@@ -602,9 +602,35 @@ fn handle_own_line_comment_between_branches<'a>(
|
|||||||
// following branch or if it a trailing comment of the previous body's last statement.
|
// following branch or if it a trailing comment of the previous body's last statement.
|
||||||
let comment_indentation = comment_indentation_after(preceding, comment.range(), source);
|
let comment_indentation = comment_indentation_after(preceding, comment.range(), source);
|
||||||
|
|
||||||
let preceding_indentation = indentation(source, &preceding)
|
let preceding_indentation = indentation(source, &preceding).map_or_else(
|
||||||
.unwrap_or_default()
|
// If `indentation` returns `None`, then there is leading
|
||||||
.text_len();
|
// content before the preceding node. In this case, we
|
||||||
|
// always treat the comment as being less-indented than the
|
||||||
|
// preceding. For example:
|
||||||
|
//
|
||||||
|
// ```python
|
||||||
|
// if True: pass
|
||||||
|
// # leading on `else`
|
||||||
|
// else:
|
||||||
|
// pass
|
||||||
|
// ```
|
||||||
|
// Note we even do this if the comment is very indented
|
||||||
|
// (which matches `black`'s behavior as of 2025.11.11)
|
||||||
|
//
|
||||||
|
// ```python
|
||||||
|
// if True: pass
|
||||||
|
// # leading on `else`
|
||||||
|
// else:
|
||||||
|
// pass
|
||||||
|
// ```
|
||||||
|
|| {
|
||||||
|
comment_indentation
|
||||||
|
// This can be any positive number - we just
|
||||||
|
// want to hit the `Less` branch below
|
||||||
|
+ TextSize::new(1)
|
||||||
|
},
|
||||||
|
ruff_text_size::TextLen::text_len,
|
||||||
|
);
|
||||||
|
|
||||||
// Compare to the last statement in the body
|
// Compare to the last statement in the body
|
||||||
match comment_indentation.cmp(&preceding_indentation) {
|
match comment_indentation.cmp(&preceding_indentation) {
|
||||||
@@ -678,8 +704,41 @@ fn handle_own_line_comment_after_branch<'a>(
|
|||||||
preceding: AnyNodeRef<'a>,
|
preceding: AnyNodeRef<'a>,
|
||||||
source: &str,
|
source: &str,
|
||||||
) -> CommentPlacement<'a> {
|
) -> CommentPlacement<'a> {
|
||||||
let Some(last_child) = preceding.last_child_in_body() else {
|
// If the preceding node has a body, we want the last child - e.g.
|
||||||
return CommentPlacement::Default(comment);
|
//
|
||||||
|
// ```python
|
||||||
|
// if True:
|
||||||
|
// def foo():
|
||||||
|
// something
|
||||||
|
// last_child
|
||||||
|
// # comment
|
||||||
|
// else:
|
||||||
|
// pass
|
||||||
|
// ```
|
||||||
|
//
|
||||||
|
// Otherwise, the preceding node may be the last statement in the body
|
||||||
|
// of the preceding branch, in which case we can take it as our
|
||||||
|
// `last_child` here - e.g.
|
||||||
|
//
|
||||||
|
// ```python
|
||||||
|
// if True:
|
||||||
|
// something
|
||||||
|
// last_child
|
||||||
|
// # comment
|
||||||
|
// else:
|
||||||
|
// pass
|
||||||
|
// ```
|
||||||
|
let last_child = match preceding.last_child_in_body() {
|
||||||
|
Some(last) => last,
|
||||||
|
None if comment.following_node().is_some_and(|following| {
|
||||||
|
following.is_first_statement_in_alternate_body(comment.enclosing_node())
|
||||||
|
}) =>
|
||||||
|
{
|
||||||
|
preceding
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return CommentPlacement::Default(comment);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// We only care about the length because indentations with mixed spaces and tabs are only valid if
|
// We only care about the length because indentations with mixed spaces and tabs are only valid if
|
||||||
@@ -1831,9 +1890,11 @@ fn handle_lambda_comment<'a>(
|
|||||||
CommentPlacement::Default(comment)
|
CommentPlacement::Default(comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Move comment between a unary op and its operand before the unary op by marking them as trailing.
|
/// Move an end-of-line comment between a unary op and its operand after the operand by marking
|
||||||
|
/// it as dangling.
|
||||||
///
|
///
|
||||||
/// For example, given:
|
/// For example, given:
|
||||||
|
///
|
||||||
/// ```python
|
/// ```python
|
||||||
/// (
|
/// (
|
||||||
/// not # comment
|
/// not # comment
|
||||||
@@ -1841,8 +1902,13 @@ fn handle_lambda_comment<'a>(
|
|||||||
/// )
|
/// )
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// The `# comment` will be attached as a dangling comment on the enclosing node, to ensure that
|
/// the `# comment` will be attached as a dangling comment on the unary op and formatted as:
|
||||||
/// it remains on the same line as the operator.
|
///
|
||||||
|
/// ```python
|
||||||
|
/// (
|
||||||
|
/// not True # comment
|
||||||
|
/// )
|
||||||
|
/// ```
|
||||||
fn handle_unary_op_comment<'a>(
|
fn handle_unary_op_comment<'a>(
|
||||||
comment: DecoratedComment<'a>,
|
comment: DecoratedComment<'a>,
|
||||||
unary_op: &'a ast::ExprUnaryOp,
|
unary_op: &'a ast::ExprUnaryOp,
|
||||||
@@ -1864,8 +1930,8 @@ fn handle_unary_op_comment<'a>(
|
|||||||
let up_to = tokenizer
|
let up_to = tokenizer
|
||||||
.find(|token| token.kind == SimpleTokenKind::LParen)
|
.find(|token| token.kind == SimpleTokenKind::LParen)
|
||||||
.map_or(unary_op.operand.start(), |lparen| lparen.start());
|
.map_or(unary_op.operand.start(), |lparen| lparen.start());
|
||||||
if comment.end() < up_to {
|
if comment.end() < up_to && comment.line_position().is_end_of_line() {
|
||||||
CommentPlacement::leading(unary_op, comment)
|
CommentPlacement::dangling(unary_op, comment)
|
||||||
} else {
|
} else {
|
||||||
CommentPlacement::Default(comment)
|
CommentPlacement::Default(comment)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
source: crates/ruff_python_formatter/src/comments/mod.rs
|
||||||
|
expression: comments.debug(test_case.source_code)
|
||||||
|
---
|
||||||
|
{
|
||||||
|
Node {
|
||||||
|
kind: StmtWhile,
|
||||||
|
range: 0..45,
|
||||||
|
source: `while True: pass⏎`,
|
||||||
|
}: {
|
||||||
|
"leading": [],
|
||||||
|
"dangling": [
|
||||||
|
SourceComment {
|
||||||
|
text: "# comment",
|
||||||
|
position: OwnLine,
|
||||||
|
formatted: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"trailing": [],
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
source: crates/ruff_python_formatter/src/comments/mod.rs
|
||||||
|
expression: comments.debug(test_case.source_code)
|
||||||
|
---
|
||||||
|
{
|
||||||
|
Node {
|
||||||
|
kind: StmtPass,
|
||||||
|
range: 16..20,
|
||||||
|
source: `pass`,
|
||||||
|
}: {
|
||||||
|
"leading": [],
|
||||||
|
"dangling": [],
|
||||||
|
"trailing": [
|
||||||
|
SourceComment {
|
||||||
|
text: "# comment",
|
||||||
|
position: OwnLine,
|
||||||
|
formatted: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -179,7 +179,22 @@ impl NeedsParentheses for ExprAttribute {
|
|||||||
context.comments().ranges(),
|
context.comments().ranges(),
|
||||||
context.source(),
|
context.source(),
|
||||||
) {
|
) {
|
||||||
OptionalParentheses::Never
|
// We have to avoid creating syntax errors like
|
||||||
|
// ```python
|
||||||
|
// variable = (something) # trailing
|
||||||
|
// .my_attribute
|
||||||
|
// ```
|
||||||
|
// See https://github.com/astral-sh/ruff/issues/19350
|
||||||
|
if context
|
||||||
|
.comments()
|
||||||
|
.trailing(self.value.as_ref())
|
||||||
|
.iter()
|
||||||
|
.any(|comment| comment.line_position().is_end_of_line())
|
||||||
|
{
|
||||||
|
OptionalParentheses::Multiline
|
||||||
|
} else {
|
||||||
|
OptionalParentheses::Never
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.value.needs_parentheses(self.into(), context)
|
self.value.needs_parentheses(self.into(), context)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ use ruff_python_ast::ExprLambda;
|
|||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::comments::dangling_comments;
|
use crate::comments::dangling_comments;
|
||||||
use crate::comments::leading_comments;
|
|
||||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
use crate::other::parameters::ParametersParentheses;
|
use crate::other::parameters::ParametersParentheses;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
@@ -34,45 +33,24 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
|
|||||||
|
|
||||||
if dangling_before_parameters.is_empty() {
|
if dangling_before_parameters.is_empty() {
|
||||||
write!(f, [space()])?;
|
write!(f, [space()])?;
|
||||||
|
} else {
|
||||||
|
write!(f, [dangling_comments(dangling_before_parameters)])?;
|
||||||
}
|
}
|
||||||
|
|
||||||
group(&format_with(|f: &mut PyFormatter| {
|
write!(
|
||||||
if f.context().node_level().is_parenthesized()
|
f,
|
||||||
&& (parameters.len() > 1 || !dangling_before_parameters.is_empty())
|
[parameters
|
||||||
{
|
.format()
|
||||||
let end_of_line_start = dangling_before_parameters
|
.with_options(ParametersParentheses::Never)]
|
||||||
.partition_point(|comment| comment.line_position().is_end_of_line());
|
)?;
|
||||||
let (same_line_comments, own_line_comments) =
|
|
||||||
dangling_before_parameters.split_at(end_of_line_start);
|
|
||||||
|
|
||||||
dangling_comments(same_line_comments).fmt(f)?;
|
write!(f, [token(":")])?;
|
||||||
|
|
||||||
write![
|
if dangling_after_parameters.is_empty() {
|
||||||
f,
|
write!(f, [space()])?;
|
||||||
[
|
} else {
|
||||||
soft_line_break(),
|
write!(f, [dangling_comments(dangling_after_parameters)])?;
|
||||||
leading_comments(own_line_comments),
|
}
|
||||||
parameters
|
|
||||||
.format()
|
|
||||||
.with_options(ParametersParentheses::Never),
|
|
||||||
]
|
|
||||||
]
|
|
||||||
} else {
|
|
||||||
parameters
|
|
||||||
.format()
|
|
||||||
.with_options(ParametersParentheses::Never)
|
|
||||||
.fmt(f)
|
|
||||||
}?;
|
|
||||||
|
|
||||||
write!(f, [token(":")])?;
|
|
||||||
|
|
||||||
if dangling_after_parameters.is_empty() {
|
|
||||||
write!(f, [space()])
|
|
||||||
} else {
|
|
||||||
write!(f, [dangling_comments(dangling_after_parameters)])
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
.fmt(f)?;
|
|
||||||
} else {
|
} else {
|
||||||
write!(f, [token(":")])?;
|
write!(f, [token(":")])?;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
use ruff_python_ast::AnyNodeRef;
|
use ruff_python_ast::AnyNodeRef;
|
||||||
use ruff_python_ast::ExprUnaryOp;
|
use ruff_python_ast::ExprUnaryOp;
|
||||||
use ruff_python_ast::UnaryOp;
|
use ruff_python_ast::UnaryOp;
|
||||||
|
use ruff_python_ast::parenthesize::parenthesized_range;
|
||||||
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::trailing_comments;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
@@ -39,20 +41,25 @@ impl FormatNodeRule<ExprUnaryOp> for FormatExprUnaryOp {
|
|||||||
// ```
|
// ```
|
||||||
trailing_comments(dangling).fmt(f)?;
|
trailing_comments(dangling).fmt(f)?;
|
||||||
|
|
||||||
// Insert a line break if the operand has comments but itself is not parenthesized.
|
// Insert a line break if the operand has comments but itself is not parenthesized or if the
|
||||||
|
// operand is parenthesized but has a leading comment before the parentheses.
|
||||||
// ```python
|
// ```python
|
||||||
// if (
|
// if (
|
||||||
// not
|
// not
|
||||||
// # comment
|
// # comment
|
||||||
// a)
|
// a):
|
||||||
|
// pass
|
||||||
|
//
|
||||||
|
// if 1 and (
|
||||||
|
// not
|
||||||
|
// # comment
|
||||||
|
// (
|
||||||
|
// a
|
||||||
|
// )
|
||||||
|
// ):
|
||||||
|
// pass
|
||||||
// ```
|
// ```
|
||||||
if comments.has_leading(operand.as_ref())
|
if needs_line_break(item, f.context()) {
|
||||||
&& !is_expression_parenthesized(
|
|
||||||
operand.as_ref().into(),
|
|
||||||
f.context().comments().ranges(),
|
|
||||||
f.context().source(),
|
|
||||||
)
|
|
||||||
{
|
|
||||||
hard_line_break().fmt(f)?;
|
hard_line_break().fmt(f)?;
|
||||||
} else if op.is_not() {
|
} else if op.is_not() {
|
||||||
space().fmt(f)?;
|
space().fmt(f)?;
|
||||||
@@ -76,17 +83,51 @@ impl NeedsParentheses for ExprUnaryOp {
|
|||||||
context: &PyFormatContext,
|
context: &PyFormatContext,
|
||||||
) -> OptionalParentheses {
|
) -> OptionalParentheses {
|
||||||
if parent.is_expr_await() {
|
if parent.is_expr_await() {
|
||||||
OptionalParentheses::Always
|
return OptionalParentheses::Always;
|
||||||
} else if is_expression_parenthesized(
|
}
|
||||||
|
|
||||||
|
if needs_line_break(self, context) {
|
||||||
|
return OptionalParentheses::Always;
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_expression_parenthesized(
|
||||||
self.operand.as_ref().into(),
|
self.operand.as_ref().into(),
|
||||||
context.comments().ranges(),
|
context.comments().ranges(),
|
||||||
context.source(),
|
context.source(),
|
||||||
) {
|
) {
|
||||||
OptionalParentheses::Never
|
return OptionalParentheses::Never;
|
||||||
} else if context.comments().has(self.operand.as_ref()) {
|
|
||||||
OptionalParentheses::Always
|
|
||||||
} else {
|
|
||||||
self.operand.needs_parentheses(self.into(), context)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if context.comments().has(self.operand.as_ref()) {
|
||||||
|
return OptionalParentheses::Always;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.operand.needs_parentheses(self.into(), context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the unary operator will have a hard line break between the operator and its
|
||||||
|
/// operand and thus requires parentheses.
|
||||||
|
fn needs_line_break(item: &ExprUnaryOp, context: &PyFormatContext) -> bool {
|
||||||
|
let comments = context.comments();
|
||||||
|
let parenthesized_operand_range = parenthesized_range(
|
||||||
|
item.operand.as_ref().into(),
|
||||||
|
item.into(),
|
||||||
|
comments.ranges(),
|
||||||
|
context.source(),
|
||||||
|
);
|
||||||
|
let leading_operand_comments = comments.leading(item.operand.as_ref());
|
||||||
|
let has_leading_comments_before_parens = parenthesized_operand_range.is_some_and(|range| {
|
||||||
|
leading_operand_comments
|
||||||
|
.iter()
|
||||||
|
.any(|comment| comment.start() < range.start())
|
||||||
|
});
|
||||||
|
|
||||||
|
!leading_operand_comments.is_empty()
|
||||||
|
&& !is_expression_parenthesized(
|
||||||
|
item.operand.as_ref().into(),
|
||||||
|
context.comments().ranges(),
|
||||||
|
context.source(),
|
||||||
|
)
|
||||||
|
|| has_leading_comments_before_parens
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use crate::expression::maybe_parenthesize_expression;
|
|||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::preview::is_remove_parens_around_except_types_enabled;
|
use crate::preview::is_remove_parens_around_except_types_enabled;
|
||||||
use crate::statement::clause::{ClauseHeader, clause_body, clause_header};
|
use crate::statement::clause::{ClauseHeader, clause};
|
||||||
use crate::statement::suite::SuiteKind;
|
use crate::statement::suite::SuiteKind;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Default)]
|
#[derive(Copy, Clone, Default)]
|
||||||
@@ -55,77 +55,68 @@ impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHan
|
|||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[
|
[clause(
|
||||||
clause_header(
|
ClauseHeader::ExceptHandler(item),
|
||||||
ClauseHeader::ExceptHandler(item),
|
&format_with(|f: &mut PyFormatter| {
|
||||||
dangling_comments,
|
write!(
|
||||||
&format_with(|f: &mut PyFormatter| {
|
f,
|
||||||
write!(
|
[
|
||||||
f,
|
token("except"),
|
||||||
[
|
match except_handler_kind {
|
||||||
token("except"),
|
ExceptHandlerKind::Regular => None,
|
||||||
match except_handler_kind {
|
ExceptHandlerKind::Starred => Some(token("*")),
|
||||||
ExceptHandlerKind::Regular => None,
|
}
|
||||||
ExceptHandlerKind::Starred => Some(token("*")),
|
]
|
||||||
}
|
)?;
|
||||||
]
|
|
||||||
)?;
|
|
||||||
|
|
||||||
match type_.as_deref() {
|
match type_.as_deref() {
|
||||||
// For tuples of exception types without an `as` name and on 3.14+, the
|
// For tuples of exception types without an `as` name and on 3.14+, the
|
||||||
// parentheses are optional.
|
// parentheses are optional.
|
||||||
//
|
//
|
||||||
// ```py
|
// ```py
|
||||||
// try:
|
// try:
|
||||||
// ...
|
// ...
|
||||||
// except BaseException, Exception: # Ok
|
// except BaseException, Exception: # Ok
|
||||||
// ...
|
// ...
|
||||||
// ```
|
// ```
|
||||||
Some(Expr::Tuple(tuple))
|
Some(Expr::Tuple(tuple))
|
||||||
if f.options().target_version() >= PythonVersion::PY314
|
if f.options().target_version() >= PythonVersion::PY314
|
||||||
&& is_remove_parens_around_except_types_enabled(
|
&& is_remove_parens_around_except_types_enabled(f.context())
|
||||||
f.context(),
|
&& name.is_none() =>
|
||||||
)
|
{
|
||||||
&& name.is_none() =>
|
write!(
|
||||||
{
|
f,
|
||||||
write!(
|
[
|
||||||
f,
|
space(),
|
||||||
[
|
tuple.format().with_options(TupleParentheses::NeverPreserve)
|
||||||
space(),
|
]
|
||||||
tuple
|
)?;
|
||||||
.format()
|
|
||||||
.with_options(TupleParentheses::NeverPreserve)
|
|
||||||
]
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
Some(type_) => {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
[
|
|
||||||
space(),
|
|
||||||
maybe_parenthesize_expression(
|
|
||||||
type_,
|
|
||||||
item,
|
|
||||||
Parenthesize::IfBreaks
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)?;
|
|
||||||
if let Some(name) = name {
|
|
||||||
write!(f, [space(), token("as"), space(), name.format()])?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
Some(type_) => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
[
|
||||||
|
space(),
|
||||||
|
maybe_parenthesize_expression(
|
||||||
|
type_,
|
||||||
|
item,
|
||||||
|
Parenthesize::IfBreaks
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)?;
|
||||||
|
if let Some(name) = name {
|
||||||
|
write!(f, [space(), token("as"), space(), name.format()])?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}),
|
}),
|
||||||
),
|
dangling_comments,
|
||||||
clause_body(
|
body,
|
||||||
body,
|
SuiteKind::other(self.last_suite_in_statement),
|
||||||
SuiteKind::other(self.last_suite_in_statement),
|
)]
|
||||||
dangling_comments
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use crate::expression::maybe_parenthesize_expression;
|
|||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::pattern::maybe_parenthesize_pattern;
|
use crate::pattern::maybe_parenthesize_pattern;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::statement::clause::{ClauseHeader, clause_body, clause_header};
|
use crate::statement::clause::{ClauseHeader, clause};
|
||||||
use crate::statement::suite::SuiteKind;
|
use crate::statement::suite::SuiteKind;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -46,23 +46,18 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
|||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[
|
[clause(
|
||||||
clause_header(
|
ClauseHeader::MatchCase(item),
|
||||||
ClauseHeader::MatchCase(item),
|
&format_args![
|
||||||
dangling_item_comments,
|
token("case"),
|
||||||
&format_args![
|
space(),
|
||||||
token("case"),
|
maybe_parenthesize_pattern(pattern, item),
|
||||||
space(),
|
format_guard
|
||||||
maybe_parenthesize_pattern(pattern, item),
|
],
|
||||||
format_guard
|
dangling_item_comments,
|
||||||
],
|
body,
|
||||||
),
|
SuiteKind::other(self.last_suite_in_statement),
|
||||||
clause_body(
|
)]
|
||||||
body,
|
|
||||||
SuiteKind::other(self.last_suite_in_statement),
|
|
||||||
dangling_item_comments
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -241,7 +241,7 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||||||
let num_parameters = item.len();
|
let num_parameters = item.len();
|
||||||
|
|
||||||
if self.parentheses == ParametersParentheses::Never {
|
if self.parentheses == ParametersParentheses::Never {
|
||||||
write!(f, [format_inner, dangling_comments(dangling)])
|
write!(f, [group(&format_inner), dangling_comments(dangling)])
|
||||||
} else if num_parameters == 0 {
|
} else if num_parameters == 0 {
|
||||||
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
|
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
|
||||||
// No parameters, format any dangling comments between `()`
|
// No parameters, format any dangling comments between `()`
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ use ruff_python_ast::{
|
|||||||
StmtIf, StmtMatch, StmtTry, StmtWhile, StmtWith, Suite,
|
StmtIf, StmtMatch, StmtTry, StmtWhile, StmtWith, Suite,
|
||||||
};
|
};
|
||||||
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
|
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
|
||||||
|
use ruff_source_file::LineRanges;
|
||||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||||
|
|
||||||
use crate::comments::{SourceComment, leading_alternate_branch_comments, trailing_comments};
|
use crate::comments::{SourceComment, leading_alternate_branch_comments, trailing_comments};
|
||||||
use crate::statement::suite::{SuiteKind, as_only_an_ellipsis};
|
use crate::statement::suite::{SuiteKind, as_only_an_ellipsis};
|
||||||
use crate::verbatim::write_suppressed_clause_header;
|
use crate::verbatim::{verbatim_text, write_suppressed_clause_header};
|
||||||
use crate::{has_skip_comment, prelude::*};
|
use crate::{has_skip_comment, prelude::*};
|
||||||
|
|
||||||
/// The header of a compound statement clause.
|
/// The header of a compound statement clause.
|
||||||
@@ -36,7 +37,41 @@ pub(crate) enum ClauseHeader<'a> {
|
|||||||
OrElse(ElseClause<'a>),
|
OrElse(ElseClause<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClauseHeader<'_> {
|
impl<'a> ClauseHeader<'a> {
|
||||||
|
/// Returns the last child in the clause body immediately following this clause header.
|
||||||
|
///
|
||||||
|
/// For most clauses, this is the last statement in
|
||||||
|
/// the primary body. For clauses like `try`, it specifically returns the last child
|
||||||
|
/// in the `try` body, not the `except`/`else`/`finally` clauses.
|
||||||
|
///
|
||||||
|
/// This is similar to [`ruff_python_ast::AnyNodeRef::last_child_in_body`]
|
||||||
|
/// but restricted to the clause.
|
||||||
|
pub(crate) fn last_child_in_clause(self) -> Option<AnyNodeRef<'a>> {
|
||||||
|
match self {
|
||||||
|
ClauseHeader::Class(StmtClassDef { body, .. })
|
||||||
|
| ClauseHeader::Function(StmtFunctionDef { body, .. })
|
||||||
|
| ClauseHeader::If(StmtIf { body, .. })
|
||||||
|
| ClauseHeader::ElifElse(ElifElseClause { body, .. })
|
||||||
|
| ClauseHeader::Try(StmtTry { body, .. })
|
||||||
|
| ClauseHeader::MatchCase(MatchCase { body, .. })
|
||||||
|
| ClauseHeader::For(StmtFor { body, .. })
|
||||||
|
| ClauseHeader::While(StmtWhile { body, .. })
|
||||||
|
| ClauseHeader::With(StmtWith { body, .. })
|
||||||
|
| ClauseHeader::ExceptHandler(ExceptHandlerExceptHandler { body, .. })
|
||||||
|
| ClauseHeader::OrElse(
|
||||||
|
ElseClause::Try(StmtTry { orelse: body, .. })
|
||||||
|
| ElseClause::For(StmtFor { orelse: body, .. })
|
||||||
|
| ElseClause::While(StmtWhile { orelse: body, .. }),
|
||||||
|
)
|
||||||
|
| ClauseHeader::TryFinally(StmtTry {
|
||||||
|
finalbody: body, ..
|
||||||
|
}) => body.last().map(AnyNodeRef::from),
|
||||||
|
ClauseHeader::Match(StmtMatch { cases, .. }) => cases
|
||||||
|
.last()
|
||||||
|
.and_then(|case| case.body.last().map(AnyNodeRef::from)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The range from the clause keyword up to and including the final colon.
|
/// The range from the clause keyword up to and including the final colon.
|
||||||
pub(crate) fn range(self, source: &str) -> FormatResult<TextRange> {
|
pub(crate) fn range(self, source: &str) -> FormatResult<TextRange> {
|
||||||
let keyword_range = self.first_keyword_range(source)?;
|
let keyword_range = self.first_keyword_range(source)?;
|
||||||
@@ -338,6 +373,28 @@ impl ClauseHeader<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> From<ClauseHeader<'a>> for AnyNodeRef<'a> {
|
||||||
|
fn from(value: ClauseHeader<'a>) -> Self {
|
||||||
|
match value {
|
||||||
|
ClauseHeader::Class(stmt_class_def) => stmt_class_def.into(),
|
||||||
|
ClauseHeader::Function(stmt_function_def) => stmt_function_def.into(),
|
||||||
|
ClauseHeader::If(stmt_if) => stmt_if.into(),
|
||||||
|
ClauseHeader::ElifElse(elif_else_clause) => elif_else_clause.into(),
|
||||||
|
ClauseHeader::Try(stmt_try) => stmt_try.into(),
|
||||||
|
ClauseHeader::ExceptHandler(except_handler_except_handler) => {
|
||||||
|
except_handler_except_handler.into()
|
||||||
|
}
|
||||||
|
ClauseHeader::TryFinally(stmt_try) => stmt_try.into(),
|
||||||
|
ClauseHeader::Match(stmt_match) => stmt_match.into(),
|
||||||
|
ClauseHeader::MatchCase(match_case) => match_case.into(),
|
||||||
|
ClauseHeader::For(stmt_for) => stmt_for.into(),
|
||||||
|
ClauseHeader::While(stmt_while) => stmt_while.into(),
|
||||||
|
ClauseHeader::With(stmt_with) => stmt_with.into(),
|
||||||
|
ClauseHeader::OrElse(else_clause) => else_clause.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub(crate) enum ElseClause<'a> {
|
pub(crate) enum ElseClause<'a> {
|
||||||
Try(&'a StmtTry),
|
Try(&'a StmtTry),
|
||||||
@@ -345,6 +402,16 @@ pub(crate) enum ElseClause<'a> {
|
|||||||
While(&'a StmtWhile),
|
While(&'a StmtWhile),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> From<ElseClause<'a>> for AnyNodeRef<'a> {
|
||||||
|
fn from(value: ElseClause<'a>) -> Self {
|
||||||
|
match value {
|
||||||
|
ElseClause::Try(stmt_try) => stmt_try.into(),
|
||||||
|
ElseClause::For(stmt_for) => stmt_for.into(),
|
||||||
|
ElseClause::While(stmt_while) => stmt_while.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) struct FormatClauseHeader<'a, 'ast> {
|
pub(crate) struct FormatClauseHeader<'a, 'ast> {
|
||||||
header: ClauseHeader<'a>,
|
header: ClauseHeader<'a>,
|
||||||
/// How to format the clause header
|
/// How to format the clause header
|
||||||
@@ -378,22 +445,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FormatClauseHeader<'a, '_> {
|
|
||||||
/// Sets the leading comments that precede an alternate branch.
|
|
||||||
#[must_use]
|
|
||||||
pub(crate) fn with_leading_comments<N>(
|
|
||||||
mut self,
|
|
||||||
comments: &'a [SourceComment],
|
|
||||||
last_node: Option<N>,
|
|
||||||
) -> Self
|
|
||||||
where
|
|
||||||
N: Into<AnyNodeRef<'a>>,
|
|
||||||
{
|
|
||||||
self.leading_comments = Some((comments, last_node.map(Into::into)));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'ast> Format<PyFormatContext<'ast>> for FormatClauseHeader<'_, 'ast> {
|
impl<'ast> Format<PyFormatContext<'ast>> for FormatClauseHeader<'_, 'ast> {
|
||||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
|
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
|
||||||
if let Some((leading_comments, last_node)) = self.leading_comments {
|
if let Some((leading_comments, last_node)) = self.leading_comments {
|
||||||
@@ -423,13 +474,13 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatClauseHeader<'_, 'ast> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct FormatClauseBody<'a> {
|
struct FormatClauseBody<'a> {
|
||||||
body: &'a Suite,
|
body: &'a Suite,
|
||||||
kind: SuiteKind,
|
kind: SuiteKind,
|
||||||
trailing_comments: &'a [SourceComment],
|
trailing_comments: &'a [SourceComment],
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn clause_body<'a>(
|
fn clause_body<'a>(
|
||||||
body: &'a Suite,
|
body: &'a Suite,
|
||||||
kind: SuiteKind,
|
kind: SuiteKind,
|
||||||
trailing_comments: &'a [SourceComment],
|
trailing_comments: &'a [SourceComment],
|
||||||
@@ -465,6 +516,84 @@ impl Format<PyFormatContext<'_>> for FormatClauseBody<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) struct FormatClause<'a, 'ast> {
|
||||||
|
header: ClauseHeader<'a>,
|
||||||
|
/// How to format the clause header
|
||||||
|
header_formatter: Argument<'a, PyFormatContext<'ast>>,
|
||||||
|
/// Leading comments coming before the branch, together with the previous node, if any. Only relevant
|
||||||
|
/// for alternate branches.
|
||||||
|
leading_comments: Option<(&'a [SourceComment], Option<AnyNodeRef<'a>>)>,
|
||||||
|
/// The trailing comments coming after the colon.
|
||||||
|
trailing_colon_comment: &'a [SourceComment],
|
||||||
|
body: &'a Suite,
|
||||||
|
kind: SuiteKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'ast> FormatClause<'a, 'ast> {
|
||||||
|
/// Sets the leading comments that precede an alternate branch.
|
||||||
|
#[must_use]
|
||||||
|
pub(crate) fn with_leading_comments<N>(
|
||||||
|
mut self,
|
||||||
|
comments: &'a [SourceComment],
|
||||||
|
last_node: Option<N>,
|
||||||
|
) -> Self
|
||||||
|
where
|
||||||
|
N: Into<AnyNodeRef<'a>>,
|
||||||
|
{
|
||||||
|
self.leading_comments = Some((comments, last_node.map(Into::into)));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clause_header(&self) -> FormatClauseHeader<'a, 'ast> {
|
||||||
|
FormatClauseHeader {
|
||||||
|
header: self.header,
|
||||||
|
formatter: self.header_formatter,
|
||||||
|
leading_comments: self.leading_comments,
|
||||||
|
trailing_colon_comment: self.trailing_colon_comment,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clause_body(&self) -> FormatClauseBody<'a> {
|
||||||
|
clause_body(self.body, self.kind, self.trailing_colon_comment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Formats a clause, handling the case where the compound
|
||||||
|
/// statement lies on a single line with `# fmt: skip` and
|
||||||
|
/// should be suppressed.
|
||||||
|
pub(crate) fn clause<'a, 'ast, Content>(
|
||||||
|
header: ClauseHeader<'a>,
|
||||||
|
header_formatter: &'a Content,
|
||||||
|
trailing_colon_comment: &'a [SourceComment],
|
||||||
|
body: &'a Suite,
|
||||||
|
kind: SuiteKind,
|
||||||
|
) -> FormatClause<'a, 'ast>
|
||||||
|
where
|
||||||
|
Content: Format<PyFormatContext<'ast>>,
|
||||||
|
{
|
||||||
|
FormatClause {
|
||||||
|
header,
|
||||||
|
header_formatter: Argument::new(header_formatter),
|
||||||
|
leading_comments: None,
|
||||||
|
trailing_colon_comment,
|
||||||
|
body,
|
||||||
|
kind,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ast> Format<PyFormatContext<'ast>> for FormatClause<'_, 'ast> {
|
||||||
|
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
|
||||||
|
match should_suppress_clause(self, f)? {
|
||||||
|
SuppressClauseHeader::Yes {
|
||||||
|
last_child_in_clause,
|
||||||
|
} => write_suppressed_clause(self, f, last_child_in_clause),
|
||||||
|
SuppressClauseHeader::No => {
|
||||||
|
write!(f, [self.clause_header(), self.clause_body()])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Finds the range of `keyword` starting the search at `start_position`.
|
/// Finds the range of `keyword` starting the search at `start_position`.
|
||||||
///
|
///
|
||||||
/// If the start position is at the end of the previous statement, the
|
/// If the start position is at the end of the previous statement, the
|
||||||
@@ -587,3 +716,96 @@ fn colon_range(after_keyword_or_condition: TextSize, source: &str) -> FormatResu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn should_suppress_clause<'a>(
|
||||||
|
clause: &FormatClause<'a, '_>,
|
||||||
|
f: &mut Formatter<PyFormatContext<'_>>,
|
||||||
|
) -> FormatResult<SuppressClauseHeader<'a>> {
|
||||||
|
let source = f.context().source();
|
||||||
|
|
||||||
|
let Some(last_child_in_clause) = clause.header.last_child_in_clause() else {
|
||||||
|
return Ok(SuppressClauseHeader::No);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Early return if we don't have a skip comment
|
||||||
|
// to avoid computing header range in the common case
|
||||||
|
if !has_skip_comment(
|
||||||
|
f.context().comments().trailing(last_child_in_clause),
|
||||||
|
source,
|
||||||
|
) {
|
||||||
|
return Ok(SuppressClauseHeader::No);
|
||||||
|
}
|
||||||
|
|
||||||
|
let clause_start = clause.header.range(source)?.end();
|
||||||
|
|
||||||
|
let clause_range = TextRange::new(clause_start, last_child_in_clause.end());
|
||||||
|
|
||||||
|
// Only applies to clauses on a single line
|
||||||
|
if source.contains_line_break(clause_range) {
|
||||||
|
return Ok(SuppressClauseHeader::No);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(SuppressClauseHeader::Yes {
|
||||||
|
last_child_in_clause,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cold]
|
||||||
|
fn write_suppressed_clause(
|
||||||
|
clause: &FormatClause,
|
||||||
|
f: &mut Formatter<PyFormatContext<'_>>,
|
||||||
|
last_child_in_clause: AnyNodeRef,
|
||||||
|
) -> FormatResult<()> {
|
||||||
|
if let Some((leading_comments, last_node)) = clause.leading_comments {
|
||||||
|
leading_alternate_branch_comments(leading_comments, last_node).fmt(f)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let header = clause.header;
|
||||||
|
let clause_start = header.first_keyword_range(f.context().source())?.start();
|
||||||
|
|
||||||
|
let comments = f.context().comments().clone();
|
||||||
|
|
||||||
|
let clause_end = last_child_in_clause.end();
|
||||||
|
|
||||||
|
// Write the outer comments and format the node as verbatim
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
[
|
||||||
|
source_position(clause_start),
|
||||||
|
verbatim_text(TextRange::new(clause_start, clause_end)),
|
||||||
|
source_position(clause_end),
|
||||||
|
trailing_comments(comments.trailing(last_child_in_clause)),
|
||||||
|
hard_line_break()
|
||||||
|
]
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// We mark comments in the header as formatted as in
|
||||||
|
// the implementation of [`write_suppressed_clause_header`].
|
||||||
|
//
|
||||||
|
// Note that the header may be multi-line and contain
|
||||||
|
// various comments since we only require that the range
|
||||||
|
// starting at the _colon_ and ending at the `# fmt: skip`
|
||||||
|
// fits on one line.
|
||||||
|
header.visit(&mut |child| {
|
||||||
|
for comment in comments.leading_trailing(child) {
|
||||||
|
comment.mark_formatted();
|
||||||
|
}
|
||||||
|
comments.mark_verbatim_node_comments_formatted(child);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Similarly we mark the comments in the body as formatted.
|
||||||
|
// Note that the trailing comments for the last child in the
|
||||||
|
// body have already been handled above.
|
||||||
|
for stmt in clause.body {
|
||||||
|
comments.mark_verbatim_node_comments_formatted(stmt.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
enum SuppressClauseHeader<'a> {
|
||||||
|
No,
|
||||||
|
Yes {
|
||||||
|
last_child_in_clause: AnyNodeRef<'a>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user