Compare commits
51 Commits
alex/relat
...
amy/ruffen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f0fa410a02 | ||
|
|
06440dc5ba | ||
|
|
64fd7e900d | ||
|
|
c920cf8cdb | ||
|
|
bb757b5a79 | ||
|
|
1f49e8ef51 | ||
|
|
701f5134ab | ||
|
|
eea9ad8352 | ||
|
|
eeac2bd3ee | ||
|
|
7319c37f4e | ||
|
|
805503c19a | ||
|
|
68a2f6c57d | ||
|
|
abaa735e1d | ||
|
|
c02d164357 | ||
|
|
88aa3f82f0 | ||
|
|
30902497db | ||
|
|
3ad99fb1f4 | ||
|
|
d0ff59cfe5 | ||
|
|
952193e0c6 | ||
|
|
4cba2e8f91 | ||
|
|
1a7f53022a | ||
|
|
266a7bc4c5 | ||
|
|
3b7a5e4de8 | ||
|
|
93039d055d | ||
|
|
3b61da0da3 | ||
|
|
5933cc0101 | ||
|
|
2190fcebe0 | ||
|
|
df9d6886d4 | ||
|
|
5133fa4516 | ||
|
|
21c5cfe236 | ||
|
|
f97da18267 | ||
|
|
bc191f59b9 | ||
|
|
00f86c39e0 | ||
|
|
2ec29b7418 | ||
|
|
ab1ac254d9 | ||
|
|
01de8bef3e | ||
|
|
b59f6eb5e9 | ||
|
|
9ca78bdf76 | ||
|
|
d65542c05e | ||
|
|
98728b2c98 | ||
|
|
924b2972f2 | ||
|
|
d035744959 | ||
|
|
ce059c4857 | ||
|
|
acbc83d6d2 | ||
|
|
a9e5246786 | ||
|
|
8b8b174e4f | ||
|
|
28fa02129b | ||
|
|
a10e42294b | ||
|
|
12a4ca003f | ||
|
|
60f7ec90ef | ||
|
|
922d964bcb |
10
.github/CODEOWNERS
vendored
10
.github/CODEOWNERS
vendored
@@ -20,9 +20,11 @@
|
||||
# ty
|
||||
/crates/ty* @carljm @MichaReiser @AlexWaygood @sharkdp @dcreager
|
||||
/crates/ruff_db/ @carljm @MichaReiser @sharkdp @dcreager
|
||||
/crates/ty_project/ @carljm @MichaReiser @sharkdp @dcreager
|
||||
/crates/ty_server/ @carljm @MichaReiser @sharkdp @dcreager
|
||||
/crates/ty_project/ @carljm @MichaReiser @sharkdp @dcreager @Gankra
|
||||
/crates/ty_ide/ @carljm @MichaReiser @AlexWaygood @sharkdp @dcreager @Gankra
|
||||
/crates/ty_server/ @carljm @MichaReiser @sharkdp @dcreager @Gankra
|
||||
/crates/ty/ @carljm @MichaReiser @sharkdp @dcreager
|
||||
/crates/ty_wasm/ @carljm @MichaReiser @sharkdp @dcreager
|
||||
/crates/ty_wasm/ @carljm @MichaReiser @sharkdp @dcreager @Gankra
|
||||
/scripts/ty_benchmark/ @carljm @MichaReiser @AlexWaygood @sharkdp @dcreager
|
||||
/crates/ty_python_semantic @carljm @AlexWaygood @sharkdp @dcreager
|
||||
/crates/ty_python_semantic/ @carljm @AlexWaygood @sharkdp @dcreager
|
||||
/crates/ty_module_resolver/ @carljm @MichaReiser @AlexWaygood @Gankra
|
||||
|
||||
8
.github/workflows/build-binaries.yml
vendored
8
.github/workflows/build-binaries.yml
vendored
@@ -51,6 +51,7 @@ jobs:
|
||||
- name: "Build sdist"
|
||||
uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4
|
||||
with:
|
||||
maturin-version: v1.9.6
|
||||
command: sdist
|
||||
args: --out dist
|
||||
- name: "Test sdist"
|
||||
@@ -81,6 +82,7 @@ jobs:
|
||||
- name: "Build wheels - x86_64"
|
||||
uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4
|
||||
with:
|
||||
maturin-version: v1.9.6
|
||||
target: x86_64
|
||||
args: --release --locked --out dist
|
||||
- name: "Upload wheels"
|
||||
@@ -123,6 +125,7 @@ jobs:
|
||||
- name: "Build wheels - aarch64"
|
||||
uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4
|
||||
with:
|
||||
maturin-version: v1.9.6
|
||||
target: aarch64
|
||||
args: --release --locked --out dist
|
||||
- name: "Test wheel - aarch64"
|
||||
@@ -179,6 +182,7 @@ jobs:
|
||||
- name: "Build wheels"
|
||||
uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4
|
||||
with:
|
||||
maturin-version: v1.9.6
|
||||
target: ${{ matrix.platform.target }}
|
||||
args: --release --locked --out dist
|
||||
env:
|
||||
@@ -232,6 +236,7 @@ jobs:
|
||||
- name: "Build wheels"
|
||||
uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4
|
||||
with:
|
||||
maturin-version: v1.9.6
|
||||
target: ${{ matrix.target }}
|
||||
manylinux: auto
|
||||
args: --release --locked --out dist
|
||||
@@ -308,6 +313,7 @@ jobs:
|
||||
- name: "Build wheels"
|
||||
uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4
|
||||
with:
|
||||
maturin-version: v1.9.6
|
||||
target: ${{ matrix.platform.target }}
|
||||
manylinux: auto
|
||||
docker-options: ${{ matrix.platform.maturin_docker_options }}
|
||||
@@ -374,6 +380,7 @@ jobs:
|
||||
- name: "Build wheels"
|
||||
uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4
|
||||
with:
|
||||
maturin-version: v1.9.6
|
||||
target: ${{ matrix.target }}
|
||||
manylinux: musllinux_1_2
|
||||
args: --release --locked --out dist
|
||||
@@ -439,6 +446,7 @@ jobs:
|
||||
- name: "Build wheels"
|
||||
uses: PyO3/maturin-action@86b9d133d34bc1b40018696f782949dac11bd380 # v1.49.4
|
||||
with:
|
||||
maturin-version: v1.9.6
|
||||
target: ${{ matrix.platform.target }}
|
||||
manylinux: musllinux_1_2
|
||||
args: --release --locked --out dist
|
||||
|
||||
58
CHANGELOG.md
58
CHANGELOG.md
@@ -1,5 +1,63 @@
|
||||
# Changelog
|
||||
|
||||
## 0.14.11
|
||||
|
||||
Released on 2026-01-08.
|
||||
|
||||
### Preview features
|
||||
|
||||
- Consolidate diagnostics for matched disable/enable suppression comments ([#22099](https://github.com/astral-sh/ruff/pull/22099))
|
||||
- Report diagnostics for invalid/unmatched range suppression comments ([#21908](https://github.com/astral-sh/ruff/pull/21908))
|
||||
- \[`airflow`\] Passing positional argument into `airflow.lineage.hook.HookLineageCollector.create_asset` is not allowed (`AIR303`) ([#22046](https://github.com/astral-sh/ruff/pull/22046))
|
||||
- \[`refurb`\] Mark `FURB192` fix as always unsafe ([#22210](https://github.com/astral-sh/ruff/pull/22210))
|
||||
- \[`ruff`\] Add `non-empty-init-module` (`RUF067`) ([#22143](https://github.com/astral-sh/ruff/pull/22143))
|
||||
|
||||
### Bug fixes
|
||||
|
||||
- Fix GitHub format for multi-line diagnostics ([#22108](https://github.com/astral-sh/ruff/pull/22108))
|
||||
- \[`flake8-unused-arguments`\] Mark `**kwargs` in `TypeVar` as used (`ARG001`) ([#22214](https://github.com/astral-sh/ruff/pull/22214))
|
||||
|
||||
### Rule changes
|
||||
|
||||
- Add `help:` subdiagnostics for several Ruff rules that can sometimes appear to disagree with `ty` ([#22331](https://github.com/astral-sh/ruff/pull/22331))
|
||||
- \[`pylint`\] Demote `PLW1510` fix to display-only ([#22318](https://github.com/astral-sh/ruff/pull/22318))
|
||||
- \[`pylint`\] Ignore identical members (`PLR1714`) ([#22220](https://github.com/astral-sh/ruff/pull/22220))
|
||||
- \[`pylint`\] Improve diagnostic range for `PLC0206` ([#22312](https://github.com/astral-sh/ruff/pull/22312))
|
||||
- \[`ruff`\] Improve fix title for `RUF102` invalid rule code ([#22100](https://github.com/astral-sh/ruff/pull/22100))
|
||||
- \[`flake8-simplify`\]: Avoid unnecessary builtins import for `SIM105` ([#22358](https://github.com/astral-sh/ruff/pull/22358))
|
||||
|
||||
### Configuration
|
||||
|
||||
- Allow Python 3.15 as valid `target-version` value in preview ([#22419](https://github.com/astral-sh/ruff/pull/22419))
|
||||
- Check `required-version` before parsing rules ([#22410](https://github.com/astral-sh/ruff/pull/22410))
|
||||
- Include configured `src` directories when resolving graphs ([#22451](https://github.com/astral-sh/ruff/pull/22451))
|
||||
|
||||
### Documentation
|
||||
|
||||
- Update `T201` suggestion to not use root logger to satisfy `LOG015` ([#22059](https://github.com/astral-sh/ruff/pull/22059))
|
||||
- Fix `iter` example in unsafe fixes doc ([#22118](https://github.com/astral-sh/ruff/pull/22118))
|
||||
- \[`flake8_print`\] better suggestion for `basicConfig` in `T201` docs ([#22101](https://github.com/astral-sh/ruff/pull/22101))
|
||||
- \[`pylint`\] Restore the fix safety docs for `PLW0133` ([#22211](https://github.com/astral-sh/ruff/pull/22211))
|
||||
- Fix Jupyter notebook discovery info for editors ([#22447](https://github.com/astral-sh/ruff/pull/22447))
|
||||
|
||||
### Contributors
|
||||
|
||||
- [@charliermarsh](https://github.com/charliermarsh)
|
||||
- [@ntBre](https://github.com/ntBre)
|
||||
- [@cenviity](https://github.com/cenviity)
|
||||
- [@njhearp](https://github.com/njhearp)
|
||||
- [@cbachhuber](https://github.com/cbachhuber)
|
||||
- [@jelle-openai](https://github.com/jelle-openai)
|
||||
- [@AlexWaygood](https://github.com/AlexWaygood)
|
||||
- [@ValdonVitija](https://github.com/ValdonVitija)
|
||||
- [@BurntSushi](https://github.com/BurntSushi)
|
||||
- [@Jkhall81](https://github.com/Jkhall81)
|
||||
- [@PeterJCLaw](https://github.com/PeterJCLaw)
|
||||
- [@harupy](https://github.com/harupy)
|
||||
- [@amyreese](https://github.com/amyreese)
|
||||
- [@sjyangkevin](https://github.com/sjyangkevin)
|
||||
- [@woodruffw](https://github.com/woodruffw)
|
||||
|
||||
## 0.14.10
|
||||
|
||||
Released on 2025-12-18.
|
||||
|
||||
@@ -10,6 +10,12 @@ Run all tests (using `nextest` for faster execution):
|
||||
cargo nextest run
|
||||
```
|
||||
|
||||
For faster test execution, use the `fast-test` profile which enables optimizations while retaining debug info:
|
||||
|
||||
```sh
|
||||
cargo nextest run --cargo-profile fast-test
|
||||
```
|
||||
|
||||
Run tests for a specific crate:
|
||||
|
||||
```sh
|
||||
|
||||
9
Cargo.lock
generated
9
Cargo.lock
generated
@@ -2912,7 +2912,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ruff"
|
||||
version = "0.14.10"
|
||||
version = "0.14.11"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"argfile",
|
||||
@@ -2928,6 +2928,7 @@ dependencies = [
|
||||
"filetime",
|
||||
"globwalk",
|
||||
"ignore",
|
||||
"indexmap",
|
||||
"indoc",
|
||||
"insta",
|
||||
"insta-cmd",
|
||||
@@ -3171,7 +3172,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ruff_linter"
|
||||
version = "0.14.10"
|
||||
version = "0.14.11"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"anyhow",
|
||||
@@ -3529,7 +3530,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ruff_wasm"
|
||||
version = "0.14.10"
|
||||
version = "0.14.11"
|
||||
dependencies = [
|
||||
"console_error_panic_hook",
|
||||
"console_log",
|
||||
@@ -4511,11 +4512,13 @@ dependencies = [
|
||||
"regex-automata",
|
||||
"ruff_cache",
|
||||
"ruff_db",
|
||||
"ruff_diagnostics",
|
||||
"ruff_macros",
|
||||
"ruff_memory_usage",
|
||||
"ruff_options_metadata",
|
||||
"ruff_python_ast",
|
||||
"ruff_python_formatter",
|
||||
"ruff_python_trivia",
|
||||
"ruff_text_size",
|
||||
"rustc-hash",
|
||||
"salsa",
|
||||
|
||||
@@ -335,6 +335,11 @@ strip = false
|
||||
debug = "full"
|
||||
lto = false
|
||||
|
||||
# Profile for faster iteration: applies minimal optimizations for faster tests.
|
||||
[profile.fast-test]
|
||||
inherits = "dev"
|
||||
opt-level = 1
|
||||
|
||||
# The profile that 'cargo dist' will build with.
|
||||
[profile.dist]
|
||||
inherits = "release"
|
||||
|
||||
@@ -150,8 +150,8 @@ curl -LsSf https://astral.sh/ruff/install.sh | sh
|
||||
powershell -c "irm https://astral.sh/ruff/install.ps1 | iex"
|
||||
|
||||
# For a specific version.
|
||||
curl -LsSf https://astral.sh/ruff/0.14.10/install.sh | sh
|
||||
powershell -c "irm https://astral.sh/ruff/0.14.10/install.ps1 | iex"
|
||||
curl -LsSf https://astral.sh/ruff/0.14.11/install.sh | sh
|
||||
powershell -c "irm https://astral.sh/ruff/0.14.11/install.ps1 | iex"
|
||||
```
|
||||
|
||||
You can also install Ruff via [Homebrew](https://formulae.brew.sh/formula/ruff), [Conda](https://anaconda.org/conda-forge/ruff),
|
||||
@@ -184,7 +184,7 @@ Ruff can also be used as a [pre-commit](https://pre-commit.com/) hook via [`ruff
|
||||
```yaml
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
# Ruff version.
|
||||
rev: v0.14.10
|
||||
rev: v0.14.11
|
||||
hooks:
|
||||
# Run the linter.
|
||||
- id: ruff-check
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ruff"
|
||||
version = "0.14.10"
|
||||
version = "0.14.11"
|
||||
publish = true
|
||||
authors = { workspace = true }
|
||||
edition = { workspace = true }
|
||||
@@ -31,6 +31,7 @@ ruff_options_metadata = { workspace = true, features = ["serde"] }
|
||||
ruff_python_ast = { workspace = true }
|
||||
ruff_python_formatter = { workspace = true }
|
||||
ruff_python_parser = { workspace = true }
|
||||
ruff_python_trivia = { workspace = true }
|
||||
ruff_server = { workspace = true }
|
||||
ruff_source_file = { workspace = true }
|
||||
ruff_text_size = { workspace = true }
|
||||
@@ -48,6 +49,7 @@ colored = { workspace = true }
|
||||
filetime = { workspace = true }
|
||||
globwalk = { workspace = true }
|
||||
ignore = { workspace = true }
|
||||
indexmap = { workspace = true }
|
||||
is-macro = { workspace = true }
|
||||
itertools = { workspace = true }
|
||||
jiff = { workspace = true }
|
||||
|
||||
@@ -2,6 +2,7 @@ use crate::args::{AnalyzeGraphArgs, ConfigArguments};
|
||||
use crate::resolve::resolve;
|
||||
use crate::{ExitStatus, resolve_default_files};
|
||||
use anyhow::Result;
|
||||
use indexmap::IndexSet;
|
||||
use log::{debug, warn};
|
||||
use path_absolutize::CWD;
|
||||
use ruff_db::system::{SystemPath, SystemPathBuf};
|
||||
@@ -11,7 +12,7 @@ use ruff_linter::source_kind::SourceKind;
|
||||
use ruff_linter::{warn_user, warn_user_once};
|
||||
use ruff_python_ast::{PySourceType, SourceType};
|
||||
use ruff_workspace::resolver::{ResolvedFile, match_exclusion, python_files_in_path};
|
||||
use rustc_hash::FxHashMap;
|
||||
use rustc_hash::{FxBuildHasher, FxHashMap};
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
@@ -59,17 +60,34 @@ pub(crate) fn analyze_graph(
|
||||
})
|
||||
.collect::<FxHashMap<_, _>>();
|
||||
|
||||
// Create a database from the source roots.
|
||||
let src_roots = package_roots
|
||||
.values()
|
||||
.filter_map(|package| package.as_deref())
|
||||
.filter_map(|package| package.parent())
|
||||
.map(Path::to_path_buf)
|
||||
.filter_map(|path| SystemPathBuf::from_path_buf(path).ok())
|
||||
.collect();
|
||||
// Create a database from the source roots, combining configured `src` paths with detected
|
||||
// package roots. Configured paths are added first so they take precedence, and duplicates
|
||||
// are removed.
|
||||
let mut src_roots: IndexSet<SystemPathBuf, FxBuildHasher> = IndexSet::default();
|
||||
|
||||
// Add configured `src` paths first (for precedence), filtering to only include existing
|
||||
// directories.
|
||||
src_roots.extend(
|
||||
pyproject_config
|
||||
.settings
|
||||
.linter
|
||||
.src
|
||||
.iter()
|
||||
.filter(|path| path.is_dir())
|
||||
.filter_map(|path| SystemPathBuf::from_path_buf(path.clone()).ok()),
|
||||
);
|
||||
|
||||
// Add detected package roots.
|
||||
src_roots.extend(
|
||||
package_roots
|
||||
.values()
|
||||
.filter_map(|package| package.as_deref())
|
||||
.filter_map(|path| path.parent())
|
||||
.filter_map(|path| SystemPathBuf::from_path_buf(path.to_path_buf()).ok()),
|
||||
);
|
||||
|
||||
let db = ModuleDb::from_src_roots(
|
||||
src_roots,
|
||||
src_roots.into_iter().collect(),
|
||||
pyproject_config
|
||||
.settings
|
||||
.analyze
|
||||
|
||||
@@ -11,6 +11,7 @@ use itertools::Itertools;
|
||||
use log::{error, warn};
|
||||
use rayon::iter::Either::{Left, Right};
|
||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||
use regex::{Captures, Regex};
|
||||
use ruff_db::diagnostic::{
|
||||
Annotation, Diagnostic, DiagnosticId, DisplayDiagnosticConfig, Severity, Span,
|
||||
};
|
||||
@@ -18,6 +19,7 @@ use ruff_linter::message::{EmitterContext, create_panic_diagnostic, render_diagn
|
||||
use ruff_linter::settings::types::OutputFormat;
|
||||
use ruff_notebook::NotebookIndex;
|
||||
use ruff_python_parser::ParseError;
|
||||
use ruff_python_trivia::textwrap::{dedent, indent};
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use thiserror::Error;
|
||||
use tracing::debug;
|
||||
@@ -489,6 +491,66 @@ pub(crate) fn format_source(
|
||||
formatted,
|
||||
)))
|
||||
}
|
||||
SourceKind::Markdown(unformatted_document) => {
|
||||
// adapted from blacken-docs
|
||||
// https://github.com/adamchainz/blacken-docs/blob/fb107c1dce25f9206e29297aaa1ed7afc2980a5a/src/blacken_docs/__init__.py#L17
|
||||
let code_block_regex = Regex::new(
|
||||
r"(?imsx)
|
||||
(?<before>
|
||||
^(?<indent>\ *)```[^\S\r\n]*
|
||||
(?:python|py|python3|py3)
|
||||
(?:\ .*?)?\n
|
||||
)
|
||||
(?<code>.*?)
|
||||
(?<after>
|
||||
^\ *```[^\S\r\n]*$
|
||||
)
|
||||
",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut changed = false;
|
||||
let formatted_document =
|
||||
code_block_regex.replace_all(unformatted_document, |capture: &Captures| {
|
||||
let (original, [before, code_indent, unformatted_code, after]) =
|
||||
capture.extract();
|
||||
|
||||
let unformatted_code = dedent(unformatted_code);
|
||||
let options = settings.to_format_options(source_type, &unformatted_code, path);
|
||||
|
||||
let formatted_code = if let Some(_range) = range {
|
||||
unimplemented!()
|
||||
} else {
|
||||
// Using `Printed::into_code` requires adding `ruff_formatter` as a direct dependency, and I suspect that Rust can optimize the closure away regardless.
|
||||
#[expect(clippy::redundant_closure_for_method_calls)]
|
||||
format_module_source(&unformatted_code, options)
|
||||
.map(|formatted| formatted.into_code())
|
||||
};
|
||||
|
||||
// TODO: figure out how to properly raise errors from inside closure
|
||||
if let Ok(formatted_code) = formatted_code {
|
||||
if formatted_code.len() == unformatted_code.len()
|
||||
&& formatted_code == *unformatted_code
|
||||
{
|
||||
original.to_string()
|
||||
} else {
|
||||
changed = true;
|
||||
let formatted_code = indent(formatted_code.as_str(), code_indent);
|
||||
format!("{before}{formatted_code}{after}")
|
||||
}
|
||||
} else {
|
||||
original.to_string()
|
||||
}
|
||||
});
|
||||
|
||||
if changed {
|
||||
Ok(FormattedSource::Formatted(SourceKind::Markdown(
|
||||
formatted_document.to_string(),
|
||||
)))
|
||||
} else {
|
||||
Ok(FormattedSource::Unchanged)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -714,6 +714,121 @@ fn notebook_basic() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Test that the `src` configuration option is respected.
|
||||
///
|
||||
/// This is useful for monorepos where there are multiple source directories that need to be
|
||||
/// included in the module resolution search path.
|
||||
#[test]
|
||||
fn src_option() -> Result<()> {
|
||||
let tempdir = TempDir::new()?;
|
||||
let root = ChildPath::new(tempdir.path());
|
||||
|
||||
// Create a lib directory with a package.
|
||||
root.child("lib")
|
||||
.child("mylib")
|
||||
.child("__init__.py")
|
||||
.write_str("def helper(): pass")?;
|
||||
|
||||
// Create an app directory with a file that imports from mylib.
|
||||
root.child("app").child("__init__.py").write_str("")?;
|
||||
root.child("app")
|
||||
.child("main.py")
|
||||
.write_str("from mylib import helper")?;
|
||||
|
||||
// Without src configured, the import from mylib won't resolve.
|
||||
insta::with_settings!({
|
||||
filters => INSTA_FILTERS.to_vec(),
|
||||
}, {
|
||||
assert_cmd_snapshot!(command().arg("app").current_dir(&root), @r#"
|
||||
success: true
|
||||
exit_code: 0
|
||||
----- stdout -----
|
||||
{
|
||||
"app/__init__.py": [],
|
||||
"app/main.py": []
|
||||
}
|
||||
|
||||
----- stderr -----
|
||||
"#);
|
||||
});
|
||||
|
||||
// With src = ["lib"], the import should resolve.
|
||||
root.child("ruff.toml").write_str(indoc::indoc! {r#"
|
||||
src = ["lib"]
|
||||
"#})?;
|
||||
|
||||
insta::with_settings!({
|
||||
filters => INSTA_FILTERS.to_vec(),
|
||||
}, {
|
||||
assert_cmd_snapshot!(command().arg("app").current_dir(&root), @r#"
|
||||
success: true
|
||||
exit_code: 0
|
||||
----- stdout -----
|
||||
{
|
||||
"app/__init__.py": [],
|
||||
"app/main.py": [
|
||||
"lib/mylib/__init__.py"
|
||||
]
|
||||
}
|
||||
|
||||
----- stderr -----
|
||||
"#);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Test that glob patterns in `src` are expanded.
|
||||
#[test]
|
||||
fn src_glob_expansion() -> Result<()> {
|
||||
let tempdir = TempDir::new()?;
|
||||
let root = ChildPath::new(tempdir.path());
|
||||
|
||||
// Create multiple lib directories with packages.
|
||||
root.child("libs")
|
||||
.child("lib_a")
|
||||
.child("pkg_a")
|
||||
.child("__init__.py")
|
||||
.write_str("def func_a(): pass")?;
|
||||
root.child("libs")
|
||||
.child("lib_b")
|
||||
.child("pkg_b")
|
||||
.child("__init__.py")
|
||||
.write_str("def func_b(): pass")?;
|
||||
|
||||
// Create an app that imports from both packages.
|
||||
root.child("app").child("__init__.py").write_str("")?;
|
||||
root.child("app")
|
||||
.child("main.py")
|
||||
.write_str("from pkg_a import func_a\nfrom pkg_b import func_b")?;
|
||||
|
||||
// Use a glob pattern to include all lib directories.
|
||||
root.child("ruff.toml").write_str(indoc::indoc! {r#"
|
||||
src = ["libs/*"]
|
||||
"#})?;
|
||||
|
||||
insta::with_settings!({
|
||||
filters => INSTA_FILTERS.to_vec(),
|
||||
}, {
|
||||
assert_cmd_snapshot!(command().arg("app").current_dir(&root), @r#"
|
||||
success: true
|
||||
exit_code: 0
|
||||
----- stdout -----
|
||||
{
|
||||
"app/__init__.py": [],
|
||||
"app/main.py": [
|
||||
"libs/lib_a/pkg_a/__init__.py",
|
||||
"libs/lib_b/pkg_b/__init__.py"
|
||||
]
|
||||
}
|
||||
|
||||
----- stderr -----
|
||||
"#);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notebook_with_magic() -> Result<()> {
|
||||
let tempdir = TempDir::new()?;
|
||||
|
||||
@@ -1126,6 +1126,35 @@ import os
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn required_version_fails_to_parse() -> Result<()> {
|
||||
let fixture = CliTest::with_file(
|
||||
"ruff.toml",
|
||||
r#"
|
||||
required-version = "pikachu"
|
||||
"#,
|
||||
)?;
|
||||
assert_cmd_snapshot!(fixture
|
||||
.check_command(), @r#"
|
||||
success: false
|
||||
exit_code: 2
|
||||
----- stdout -----
|
||||
|
||||
----- stderr -----
|
||||
ruff failed
|
||||
Cause: Failed to load configuration `[TMP]/ruff.toml`
|
||||
Cause: Failed to parse [TMP]/ruff.toml
|
||||
Cause: TOML parse error at line 2, column 20
|
||||
|
|
||||
2 | required-version = "pikachu"
|
||||
| ^^^^^^^^^
|
||||
Failed to parse version: Unexpected end of version specifier, expected operator:
|
||||
pikachu
|
||||
^^^^^^^
|
||||
"#);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn required_version_exact_mismatch() -> Result<()> {
|
||||
let version = env!("CARGO_PKG_VERSION");
|
||||
@@ -1137,10 +1166,10 @@ required-version = "0.1.0"
|
||||
"#,
|
||||
)?;
|
||||
|
||||
insta::with_settings!({
|
||||
filters => vec![(version, "[VERSION]")]
|
||||
}, {
|
||||
assert_cmd_snapshot!(fixture
|
||||
let mut settings = insta::Settings::clone_current();
|
||||
settings.add_filter(version, "[VERSION]");
|
||||
settings.bind(|| {
|
||||
assert_cmd_snapshot!(fixture
|
||||
.check_command()
|
||||
.arg("--config")
|
||||
.arg("ruff.toml")
|
||||
@@ -1154,6 +1183,7 @@ import os
|
||||
|
||||
----- stderr -----
|
||||
ruff failed
|
||||
Cause: Failed to load configuration `[TMP]/ruff.toml`
|
||||
Cause: Required version `==0.1.0` does not match the running version `[VERSION]`
|
||||
");
|
||||
});
|
||||
@@ -1212,10 +1242,10 @@ required-version = ">{version}"
|
||||
),
|
||||
)?;
|
||||
|
||||
insta::with_settings!({
|
||||
filters => vec![(version, "[VERSION]")]
|
||||
}, {
|
||||
assert_cmd_snapshot!(fixture
|
||||
let mut settings = insta::Settings::clone_current();
|
||||
settings.add_filter(version, "[VERSION]");
|
||||
settings.bind(|| {
|
||||
assert_cmd_snapshot!(fixture
|
||||
.check_command()
|
||||
.arg("--config")
|
||||
.arg("ruff.toml")
|
||||
@@ -1229,6 +1259,48 @@ import os
|
||||
|
||||
----- stderr -----
|
||||
ruff failed
|
||||
Cause: Failed to load configuration `[TMP]/ruff.toml`
|
||||
Cause: Required version `>[VERSION]` does not match the running version `[VERSION]`
|
||||
");
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn required_version_precedes_rule_validation() -> Result<()> {
|
||||
let version = env!("CARGO_PKG_VERSION");
|
||||
|
||||
let fixture = CliTest::with_file(
|
||||
"ruff.toml",
|
||||
&format!(
|
||||
r#"
|
||||
required-version = ">{version}"
|
||||
|
||||
[lint]
|
||||
select = ["RUF999"]
|
||||
"#
|
||||
),
|
||||
)?;
|
||||
|
||||
let mut settings = insta::Settings::clone_current();
|
||||
settings.add_filter(version, "[VERSION]");
|
||||
settings.bind(|| {
|
||||
assert_cmd_snapshot!(fixture
|
||||
.check_command()
|
||||
.arg("--config")
|
||||
.arg("ruff.toml")
|
||||
.arg("-")
|
||||
.pass_stdin(r#"
|
||||
import os
|
||||
"#), @"
|
||||
success: false
|
||||
exit_code: 2
|
||||
----- stdout -----
|
||||
|
||||
----- stderr -----
|
||||
ruff failed
|
||||
Cause: Failed to load configuration `[TMP]/ruff.toml`
|
||||
Cause: Required version `>[VERSION]` does not match the running version `[VERSION]`
|
||||
");
|
||||
});
|
||||
|
||||
@@ -221,7 +221,7 @@ fn setup_micro_case(code: &str) -> Case {
|
||||
let file_path = "src/test.py";
|
||||
fs.write_file_all(
|
||||
SystemPathBuf::from(file_path),
|
||||
ruff_python_trivia::textwrap::dedent(code),
|
||||
&*ruff_python_trivia::textwrap::dedent(code),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::fmt::Formatter;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
|
||||
@@ -49,3 +50,15 @@ impl CancellationToken {
|
||||
self.cancelled.load(std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
/// The operation was canceled by the provided [`CancellationToken`].
|
||||
#[derive(Debug)]
|
||||
pub struct Canceled;
|
||||
|
||||
impl std::error::Error for Canceled {}
|
||||
|
||||
impl std::fmt::Display for Canceled {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("operation was canceled")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,6 +98,44 @@ impl Diagnostic {
|
||||
diag
|
||||
}
|
||||
|
||||
/// Adds sub diagnostics that tell the user that this is a bug in ty
|
||||
/// and asks them to open an issue on GitHub.
|
||||
pub fn add_bug_sub_diagnostics(&mut self, url_encoded_title: &str) {
|
||||
self.sub(SubDiagnostic::new(
|
||||
SubDiagnosticSeverity::Info,
|
||||
"This indicates a bug in ty.",
|
||||
));
|
||||
|
||||
self.sub(SubDiagnostic::new(
|
||||
SubDiagnosticSeverity::Info,
|
||||
format_args!(
|
||||
"If you could open an issue at https://github.com/astral-sh/ty/issues/new?title={url_encoded_title}, we'd be very appreciative!"
|
||||
),
|
||||
));
|
||||
self.sub(SubDiagnostic::new(
|
||||
SubDiagnosticSeverity::Info,
|
||||
format!(
|
||||
"Platform: {os} {arch}",
|
||||
os = std::env::consts::OS,
|
||||
arch = std::env::consts::ARCH
|
||||
),
|
||||
));
|
||||
if let Some(version) = crate::program_version() {
|
||||
self.sub(SubDiagnostic::new(
|
||||
SubDiagnosticSeverity::Info,
|
||||
format!("Version: {version}"),
|
||||
));
|
||||
}
|
||||
|
||||
self.sub(SubDiagnostic::new(
|
||||
SubDiagnosticSeverity::Info,
|
||||
format!(
|
||||
"Args: {args:?}",
|
||||
args = std::env::args().collect::<Vec<_>>()
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
/// Add an annotation to this diagnostic.
|
||||
///
|
||||
/// Annotations for a diagnostic are optional, but if any are added,
|
||||
@@ -1019,6 +1057,13 @@ impl DiagnosticId {
|
||||
matches!(self, DiagnosticId::Lint(_))
|
||||
}
|
||||
|
||||
pub const fn as_lint(&self) -> Option<LintName> {
|
||||
match self {
|
||||
DiagnosticId::Lint(name) => Some(*name),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if this `DiagnosticId` represents a lint with the given name.
|
||||
pub fn is_lint_named(&self, name: &str) -> bool {
|
||||
matches!(self, DiagnosticId::Lint(self_name) if self_name == name)
|
||||
|
||||
@@ -14,6 +14,7 @@ use crate::diagnostic::{Span, UnifiedFile};
|
||||
use crate::file_revision::FileRevision;
|
||||
use crate::files::file_root::FileRoots;
|
||||
use crate::files::private::FileStatus;
|
||||
use crate::source::SourceText;
|
||||
use crate::system::{SystemPath, SystemPathBuf, SystemVirtualPath, SystemVirtualPathBuf};
|
||||
use crate::vendored::{VendoredPath, VendoredPathBuf};
|
||||
use crate::{Db, FxDashMap, vendored};
|
||||
@@ -323,6 +324,17 @@ pub struct File {
|
||||
/// the file has been deleted is to change the status to `Deleted`.
|
||||
#[default]
|
||||
status: FileStatus,
|
||||
|
||||
/// Overrides the result of [`source_text`](crate::source::source_text).
|
||||
///
|
||||
/// This is useful when running queries after modifying a file's content but
|
||||
/// before the content is written to disk. For example, to verify that the applied fixes
|
||||
/// didn't introduce any new errors.
|
||||
///
|
||||
/// The override gets automatically removed the next time the file changes.
|
||||
#[default]
|
||||
#[returns(ref)]
|
||||
pub source_text_override: Option<SourceText>,
|
||||
}
|
||||
|
||||
// The Salsa heap is tracked separately.
|
||||
@@ -444,20 +456,28 @@ impl File {
|
||||
_ => (FileStatus::NotFound, FileRevision::zero(), None),
|
||||
};
|
||||
|
||||
let mut clear_override = false;
|
||||
|
||||
if file.status(db) != status {
|
||||
tracing::debug!("Updating the status of `{}`", file.path(db));
|
||||
file.set_status(db).to(status);
|
||||
clear_override = true;
|
||||
}
|
||||
|
||||
if file.revision(db) != revision {
|
||||
tracing::debug!("Updating the revision of `{}`", file.path(db));
|
||||
file.set_revision(db).to(revision);
|
||||
clear_override = true;
|
||||
}
|
||||
|
||||
if file.permissions(db) != permission {
|
||||
tracing::debug!("Updating the permissions of `{}`", file.path(db));
|
||||
file.set_permissions(db).to(permission);
|
||||
}
|
||||
|
||||
if clear_override && file.source_text_override(db).is_some() {
|
||||
file.set_source_text_override(db).to(None);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the file exists.
|
||||
@@ -526,7 +546,7 @@ impl VirtualFile {
|
||||
}
|
||||
|
||||
/// Increments the revision of the underlying [`File`].
|
||||
fn sync(&self, db: &mut dyn Db) {
|
||||
pub fn sync(&self, db: &mut dyn Db) {
|
||||
let file = self.0;
|
||||
tracing::debug!("Updating the revision of `{}`", file.path(db));
|
||||
let current_revision = file.revision(db);
|
||||
|
||||
@@ -85,6 +85,13 @@ pub fn max_parallelism() -> NonZeroUsize {
|
||||
})
|
||||
}
|
||||
|
||||
// Use a reasonably large stack size to avoid running into stack overflows too easily. The
|
||||
// size was chosen in such a way as to still be able to handle large expressions involving
|
||||
// binary operators (x + x + … + x) both during the AST walk in semantic index building as
|
||||
// well as during type checking. Using this stack size, we can handle handle expressions
|
||||
// that are several times larger than the corresponding limits in existing type checkers.
|
||||
pub const STACK_SIZE: usize = 16 * 1024 * 1024;
|
||||
|
||||
/// Trait for types that can provide Rust documentation.
|
||||
///
|
||||
/// Use `derive(RustDoc)` to automatically implement this trait for types that have a static string documentation.
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use std::borrow::Cow;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
||||
use ruff_diagnostics::SourceMap;
|
||||
use ruff_notebook::Notebook;
|
||||
use ruff_python_ast::PySourceType;
|
||||
use ruff_source_file::LineIndex;
|
||||
@@ -16,6 +18,10 @@ pub fn source_text(db: &dyn Db, file: File) -> SourceText {
|
||||
let _span = tracing::trace_span!("source_text", file = %path).entered();
|
||||
let mut read_error = None;
|
||||
|
||||
if let Some(source) = file.source_text_override(db) {
|
||||
return source.clone();
|
||||
}
|
||||
|
||||
let kind = if is_notebook(db.system(), path) {
|
||||
file.read_to_notebook(db)
|
||||
.unwrap_or_else(|error| {
|
||||
@@ -90,6 +96,45 @@ impl SourceText {
|
||||
pub fn read_error(&self) -> Option<&SourceTextError> {
|
||||
self.inner.read_error.as_ref()
|
||||
}
|
||||
|
||||
/// Returns a new instance for this file with the updated source text (Python code).
|
||||
///
|
||||
/// Uses the `source_map` to preserve the cell-boundaries.
|
||||
#[must_use]
|
||||
pub fn with_text(&self, new_text: String, source_map: &SourceMap) -> Self {
|
||||
let new_kind = match &self.inner.kind {
|
||||
SourceTextKind::Text(_) => SourceTextKind::Text(new_text),
|
||||
|
||||
SourceTextKind::Notebook { notebook } => {
|
||||
let mut new_notebook = notebook.as_ref().clone();
|
||||
new_notebook.update(source_map, new_text);
|
||||
SourceTextKind::Notebook {
|
||||
notebook: new_notebook.into(),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Self {
|
||||
inner: Arc::new(SourceTextInner {
|
||||
kind: new_kind,
|
||||
read_error: self.inner.read_error.clone(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Cow<'_, [u8]> {
|
||||
match &self.inner.kind {
|
||||
SourceTextKind::Text(source) => Cow::Borrowed(source.as_bytes()),
|
||||
SourceTextKind::Notebook { notebook } => {
|
||||
let mut output: Vec<u8> = Vec::new();
|
||||
notebook
|
||||
.write(&mut output)
|
||||
.expect("writing to a Vec should never fail");
|
||||
|
||||
Cow::Owned(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for SourceText {
|
||||
@@ -117,13 +162,13 @@ impl std::fmt::Debug for SourceText {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, get_size2::GetSize)]
|
||||
#[derive(Eq, PartialEq, get_size2::GetSize, Clone)]
|
||||
struct SourceTextInner {
|
||||
kind: SourceTextKind,
|
||||
read_error: Option<SourceTextError>,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, get_size2::GetSize)]
|
||||
#[derive(Eq, PartialEq, get_size2::GetSize, Clone)]
|
||||
enum SourceTextKind {
|
||||
Text(String),
|
||||
Notebook {
|
||||
|
||||
@@ -271,7 +271,12 @@ pub trait WritableSystem: System {
|
||||
fn create_new_file(&self, path: &SystemPath) -> Result<()>;
|
||||
|
||||
/// Writes the given content to the file at the given path.
|
||||
fn write_file(&self, path: &SystemPath, content: &str) -> Result<()>;
|
||||
fn write_file(&self, path: &SystemPath, content: &str) -> Result<()> {
|
||||
self.write_file_bytes(path, content.as_bytes())
|
||||
}
|
||||
|
||||
/// Writes the given content to the file at the given path.
|
||||
fn write_file_bytes(&self, path: &SystemPath, content: &[u8]) -> Result<()>;
|
||||
|
||||
/// Creates a directory at `path` as well as any intermediate directories.
|
||||
fn create_directory_all(&self, path: &SystemPath) -> Result<()>;
|
||||
@@ -311,6 +316,8 @@ pub trait WritableSystem: System {
|
||||
|
||||
Ok(Some(cache_path))
|
||||
}
|
||||
|
||||
fn dyn_clone(&self) -> Box<dyn WritableSystem>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
|
||||
@@ -122,7 +122,9 @@ impl MemoryFileSystem {
|
||||
let entry = by_path.get(&normalized).ok_or_else(not_found)?;
|
||||
|
||||
match entry {
|
||||
Entry::File(file) => Ok(file.content.clone()),
|
||||
Entry::File(file) => {
|
||||
String::from_utf8(file.content.to_vec()).map_err(|_| invalid_utf8())
|
||||
}
|
||||
Entry::Directory(_) => Err(is_a_directory()),
|
||||
}
|
||||
}
|
||||
@@ -139,7 +141,7 @@ impl MemoryFileSystem {
|
||||
.get(&path.as_ref().to_path_buf())
|
||||
.ok_or_else(not_found)?;
|
||||
|
||||
Ok(file.content.clone())
|
||||
String::from_utf8(file.content.to_vec()).map_err(|_| invalid_utf8())
|
||||
}
|
||||
|
||||
pub fn exists(&self, path: &SystemPath) -> bool {
|
||||
@@ -161,7 +163,7 @@ impl MemoryFileSystem {
|
||||
match by_path.entry(normalized) {
|
||||
btree_map::Entry::Vacant(entry) => {
|
||||
entry.insert(Entry::File(File {
|
||||
content: String::new(),
|
||||
content: Box::default(),
|
||||
last_modified: file_time_now(),
|
||||
}));
|
||||
|
||||
@@ -177,13 +179,17 @@ impl MemoryFileSystem {
|
||||
/// Stores a new file in the file system.
|
||||
///
|
||||
/// The operation overrides the content for an existing file with the same normalized `path`.
|
||||
pub fn write_file(&self, path: impl AsRef<SystemPath>, content: impl ToString) -> Result<()> {
|
||||
pub fn write_file(
|
||||
&self,
|
||||
path: impl AsRef<SystemPath>,
|
||||
content: impl AsRef<[u8]>,
|
||||
) -> Result<()> {
|
||||
let mut by_path = self.inner.by_path.write().unwrap();
|
||||
|
||||
let normalized = self.normalize_path(path.as_ref());
|
||||
|
||||
let file = get_or_create_file(&mut by_path, &normalized)?;
|
||||
file.content = content.to_string();
|
||||
file.content = content.as_ref().to_vec().into_boxed_slice();
|
||||
file.last_modified = file_time_now();
|
||||
|
||||
Ok(())
|
||||
@@ -214,7 +220,7 @@ impl MemoryFileSystem {
|
||||
pub fn write_file_all(
|
||||
&self,
|
||||
path: impl AsRef<SystemPath>,
|
||||
content: impl ToString,
|
||||
content: impl AsRef<[u8]>,
|
||||
) -> Result<()> {
|
||||
let path = path.as_ref();
|
||||
|
||||
@@ -228,19 +234,24 @@ impl MemoryFileSystem {
|
||||
/// Stores a new virtual file in the file system.
|
||||
///
|
||||
/// The operation overrides the content for an existing virtual file with the same `path`.
|
||||
pub fn write_virtual_file(&self, path: impl AsRef<SystemVirtualPath>, content: impl ToString) {
|
||||
pub fn write_virtual_file(
|
||||
&self,
|
||||
path: impl AsRef<SystemVirtualPath>,
|
||||
content: impl AsRef<[u8]>,
|
||||
) {
|
||||
let path = path.as_ref();
|
||||
let mut virtual_files = self.inner.virtual_files.write().unwrap();
|
||||
let content = content.as_ref().to_vec().into_boxed_slice();
|
||||
|
||||
match virtual_files.entry(path.to_path_buf()) {
|
||||
std::collections::hash_map::Entry::Vacant(entry) => {
|
||||
entry.insert(File {
|
||||
content: content.to_string(),
|
||||
content,
|
||||
last_modified: file_time_now(),
|
||||
});
|
||||
}
|
||||
std::collections::hash_map::Entry::Occupied(mut entry) => {
|
||||
entry.get_mut().content = content.to_string();
|
||||
entry.get_mut().content = content;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -468,7 +479,7 @@ impl Entry {
|
||||
|
||||
#[derive(Debug)]
|
||||
struct File {
|
||||
content: String,
|
||||
content: Box<[u8]>,
|
||||
last_modified: FileTime,
|
||||
}
|
||||
|
||||
@@ -497,6 +508,13 @@ fn directory_not_empty() -> std::io::Error {
|
||||
std::io::Error::other("directory not empty")
|
||||
}
|
||||
|
||||
fn invalid_utf8() -> std::io::Error {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"stream did not contain valid UTF-8",
|
||||
)
|
||||
}
|
||||
|
||||
fn create_dir_all(
|
||||
paths: &mut RwLockWriteGuard<BTreeMap<Utf8PathBuf, Entry>>,
|
||||
normalized: &Utf8Path,
|
||||
@@ -533,7 +551,7 @@ fn get_or_create_file<'a>(
|
||||
|
||||
let entry = paths.entry(normalized.to_path_buf()).or_insert_with(|| {
|
||||
Entry::File(File {
|
||||
content: String::new(),
|
||||
content: Box::default(),
|
||||
last_modified: file_time_now(),
|
||||
})
|
||||
});
|
||||
@@ -844,7 +862,7 @@ mod tests {
|
||||
let fs = with_files(["c.py"]);
|
||||
|
||||
let error = fs
|
||||
.write_file(SystemPath::new("a/b.py"), "content".to_string())
|
||||
.write_file(SystemPath::new("a/b.py"), "content")
|
||||
.unwrap_err();
|
||||
|
||||
assert_eq!(error.kind(), ErrorKind::NotFound);
|
||||
@@ -855,7 +873,7 @@ mod tests {
|
||||
let fs = with_files(["a/b.py"]);
|
||||
|
||||
let error = fs
|
||||
.write_file_all(SystemPath::new("a/b.py/c"), "content".to_string())
|
||||
.write_file_all(SystemPath::new("a/b.py/c"), "content")
|
||||
.unwrap_err();
|
||||
|
||||
assert_eq!(error.kind(), ErrorKind::Other);
|
||||
@@ -878,7 +896,7 @@ mod tests {
|
||||
let fs = MemoryFileSystem::new();
|
||||
let path = SystemPath::new("a.py");
|
||||
|
||||
fs.write_file_all(path, "Test content".to_string())?;
|
||||
fs.write_file_all(path, "Test content")?;
|
||||
|
||||
assert_eq!(fs.read_to_string(path)?, "Test content");
|
||||
|
||||
@@ -915,9 +933,7 @@ mod tests {
|
||||
|
||||
fs.create_directory_all("a")?;
|
||||
|
||||
let error = fs
|
||||
.write_file(SystemPath::new("a"), "content".to_string())
|
||||
.unwrap_err();
|
||||
let error = fs.write_file(SystemPath::new("a"), "content").unwrap_err();
|
||||
|
||||
assert_eq!(error.kind(), ErrorKind::Other);
|
||||
|
||||
|
||||
@@ -361,13 +361,17 @@ impl WritableSystem for OsSystem {
|
||||
std::fs::File::create_new(path).map(drop)
|
||||
}
|
||||
|
||||
fn write_file(&self, path: &SystemPath, content: &str) -> Result<()> {
|
||||
fn write_file_bytes(&self, path: &SystemPath, content: &[u8]) -> Result<()> {
|
||||
std::fs::write(path.as_std_path(), content)
|
||||
}
|
||||
|
||||
fn create_directory_all(&self, path: &SystemPath) -> Result<()> {
|
||||
std::fs::create_dir_all(path.as_std_path())
|
||||
}
|
||||
|
||||
fn dyn_clone(&self) -> Box<dyn WritableSystem> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for OsSystem {
|
||||
|
||||
@@ -205,13 +205,17 @@ impl WritableSystem for TestSystem {
|
||||
self.system().create_new_file(path)
|
||||
}
|
||||
|
||||
fn write_file(&self, path: &SystemPath, content: &str) -> Result<()> {
|
||||
self.system().write_file(path, content)
|
||||
fn write_file_bytes(&self, path: &SystemPath, content: &[u8]) -> Result<()> {
|
||||
self.system().write_file_bytes(path, content)
|
||||
}
|
||||
|
||||
fn create_directory_all(&self, path: &SystemPath) -> Result<()> {
|
||||
self.system().create_directory_all(path)
|
||||
}
|
||||
|
||||
fn dyn_clone(&self) -> Box<dyn WritableSystem> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for databases that use a [`WritableSystem`].
|
||||
@@ -283,7 +287,11 @@ pub trait DbWithTestSystem: Db + Sized {
|
||||
///
|
||||
/// ## Panics
|
||||
/// If the db isn't using the [`InMemorySystem`].
|
||||
fn write_virtual_file(&mut self, path: impl AsRef<SystemVirtualPath>, content: impl ToString) {
|
||||
fn write_virtual_file(
|
||||
&mut self,
|
||||
path: impl AsRef<SystemVirtualPath>,
|
||||
content: impl AsRef<[u8]>,
|
||||
) {
|
||||
let path = path.as_ref();
|
||||
self.test_system()
|
||||
.memory_file_system()
|
||||
@@ -322,23 +330,23 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
#[derive(Clone, Default, Debug)]
|
||||
pub struct InMemorySystem {
|
||||
user_config_directory: Mutex<Option<SystemPathBuf>>,
|
||||
user_config_directory: Arc<Mutex<Option<SystemPathBuf>>>,
|
||||
memory_fs: MemoryFileSystem,
|
||||
}
|
||||
|
||||
impl InMemorySystem {
|
||||
pub fn new(cwd: SystemPathBuf) -> Self {
|
||||
Self {
|
||||
user_config_directory: Mutex::new(None),
|
||||
user_config_directory: Mutex::new(None).into(),
|
||||
memory_fs: MemoryFileSystem::with_current_directory(cwd),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_memory_fs(memory_fs: MemoryFileSystem) -> Self {
|
||||
Self {
|
||||
user_config_directory: Mutex::new(None),
|
||||
user_config_directory: Mutex::new(None).into(),
|
||||
memory_fs,
|
||||
}
|
||||
}
|
||||
@@ -440,10 +448,7 @@ impl System for InMemorySystem {
|
||||
}
|
||||
|
||||
fn dyn_clone(&self) -> Box<dyn System> {
|
||||
Box::new(Self {
|
||||
user_config_directory: Mutex::new(self.user_config_directory.lock().unwrap().clone()),
|
||||
memory_fs: self.memory_fs.clone(),
|
||||
})
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -452,11 +457,15 @@ impl WritableSystem for InMemorySystem {
|
||||
self.memory_fs.create_new_file(path)
|
||||
}
|
||||
|
||||
fn write_file(&self, path: &SystemPath, content: &str) -> Result<()> {
|
||||
fn write_file_bytes(&self, path: &SystemPath, content: &[u8]) -> Result<()> {
|
||||
self.memory_fs.write_file(path, content)
|
||||
}
|
||||
|
||||
fn create_directory_all(&self, path: &SystemPath) -> Result<()> {
|
||||
self.memory_fs.create_directory_all(path)
|
||||
}
|
||||
|
||||
fn dyn_clone(&self) -> Box<dyn WritableSystem> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ruff_linter"
|
||||
version = "0.14.10"
|
||||
version = "0.14.11"
|
||||
publish = false
|
||||
authors = { workspace = true }
|
||||
edition = { workspace = true }
|
||||
|
||||
@@ -26,6 +26,7 @@ use crate::doc_lines::{doc_lines_from_ast, doc_lines_from_tokens};
|
||||
use crate::fix::{FixResult, fix_file};
|
||||
use crate::noqa::add_noqa;
|
||||
use crate::package::PackageRoot;
|
||||
use crate::preview::is_py315_support_enabled;
|
||||
use crate::registry::Rule;
|
||||
#[cfg(any(feature = "test-rules", test))]
|
||||
use crate::rules::ruff::rules::test_rules::{self, TEST_RULES, TestRule};
|
||||
@@ -33,7 +34,7 @@ use crate::settings::types::UnsafeFixes;
|
||||
use crate::settings::{LinterSettings, TargetVersion, flags};
|
||||
use crate::source_kind::SourceKind;
|
||||
use crate::suppression::Suppressions;
|
||||
use crate::{Locator, directives, fs};
|
||||
use crate::{Locator, directives, fs, warn_user_once};
|
||||
|
||||
pub(crate) mod float;
|
||||
|
||||
@@ -450,6 +451,14 @@ pub fn lint_only(
|
||||
) -> LinterResult {
|
||||
let target_version = settings.resolve_target_version(path);
|
||||
|
||||
if matches!(target_version.linter_version(), PythonVersion::PY315)
|
||||
&& !is_py315_support_enabled(settings)
|
||||
{
|
||||
warn_user_once!(
|
||||
"Support for Python 3.15 is under development and may be unstable. Enable `preview` to remove this warning."
|
||||
);
|
||||
}
|
||||
|
||||
let parsed = source.into_parsed(source_kind, source_type, target_version.parser_version());
|
||||
|
||||
// Map row and column locations to byte slices (lazily).
|
||||
@@ -555,6 +564,14 @@ pub fn lint_fix<'a>(
|
||||
|
||||
let target_version = settings.resolve_target_version(path);
|
||||
|
||||
if matches!(target_version.linter_version(), PythonVersion::PY315)
|
||||
&& !is_py315_support_enabled(settings)
|
||||
{
|
||||
warn_user_once!(
|
||||
"Support for Python 3.15 is under development and may be unstable. Enable `preview` to remove this warning."
|
||||
);
|
||||
}
|
||||
|
||||
// Continuously fix until the source code stabilizes.
|
||||
loop {
|
||||
// Parse once.
|
||||
|
||||
@@ -296,3 +296,8 @@ pub(crate) const fn is_s310_resolve_string_literal_bindings_enabled(
|
||||
pub(crate) const fn is_range_suppressions_enabled(settings: &LinterSettings) -> bool {
|
||||
settings.preview.is_enabled()
|
||||
}
|
||||
|
||||
// https://github.com/astral-sh/ruff/pull/22419
|
||||
pub(crate) const fn is_py315_support_enabled(settings: &LinterSettings) -> bool {
|
||||
settings.preview.is_enabled()
|
||||
}
|
||||
|
||||
@@ -36,13 +36,16 @@ use crate::{Fix, FixAvailability, Violation};
|
||||
/// ```python
|
||||
/// import logging
|
||||
///
|
||||
/// logging.basicConfig(level=logging.INFO)
|
||||
/// logger = logging.getLogger(__name__)
|
||||
///
|
||||
///
|
||||
/// def sum_less_than_four(a, b):
|
||||
/// logger.debug("Calling sum_less_than_four")
|
||||
/// return a + b < 4
|
||||
///
|
||||
///
|
||||
/// if __name__ == "__main__":
|
||||
/// logging.basicConfig(level=logging.INFO)
|
||||
/// ```
|
||||
///
|
||||
/// ## Fix safety
|
||||
|
||||
@@ -5,7 +5,7 @@ use ruff_text_size::Ranged;
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::fix::edits::add_argument;
|
||||
use crate::{AlwaysFixableViolation, Applicability, Fix};
|
||||
use crate::{Fix, FixAvailability, Violation};
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for uses of `subprocess.run` without an explicit `check` argument.
|
||||
@@ -39,9 +39,12 @@ use crate::{AlwaysFixableViolation, Applicability, Fix};
|
||||
/// ```
|
||||
///
|
||||
/// ## Fix safety
|
||||
/// This rule's fix is marked as unsafe for function calls that contain
|
||||
/// `**kwargs`, as adding a `check` keyword argument to such a call may lead
|
||||
/// to a duplicate keyword argument error.
|
||||
///
|
||||
/// This rule's fix is marked as display-only because it's not clear whether the
|
||||
/// potential exception was meant to be ignored by setting `check=False` or if
|
||||
/// the author simply forgot to include `check=True`. The fix adds
|
||||
/// `check=False`, making the existing behavior explicit but possibly masking
|
||||
/// the original intention.
|
||||
///
|
||||
/// ## References
|
||||
/// - [Python documentation: `subprocess.run`](https://docs.python.org/3/library/subprocess.html#subprocess.run)
|
||||
@@ -49,14 +52,18 @@ use crate::{AlwaysFixableViolation, Applicability, Fix};
|
||||
#[violation_metadata(stable_since = "v0.0.285")]
|
||||
pub(crate) struct SubprocessRunWithoutCheck;
|
||||
|
||||
impl AlwaysFixableViolation for SubprocessRunWithoutCheck {
|
||||
impl Violation for SubprocessRunWithoutCheck {
|
||||
// The fix is always set on the diagnostic, but display-only fixes aren't
|
||||
// considered "fixable" in the tests.
|
||||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;
|
||||
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
"`subprocess.run` without explicit `check` argument".to_string()
|
||||
}
|
||||
|
||||
fn fix_title(&self) -> String {
|
||||
"Add explicit `check=False`".to_string()
|
||||
fn fix_title(&self) -> Option<String> {
|
||||
Some("Add explicit `check=False`".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,20 +81,11 @@ pub(crate) fn subprocess_run_without_check(checker: &Checker, call: &ast::ExprCa
|
||||
if call.arguments.find_keyword("check").is_none() {
|
||||
let mut diagnostic =
|
||||
checker.report_diagnostic(SubprocessRunWithoutCheck, call.func.range());
|
||||
diagnostic.set_fix(Fix::applicable_edit(
|
||||
add_argument("check=False", &call.arguments, checker.tokens()),
|
||||
// If the function call contains `**kwargs`, mark the fix as unsafe.
|
||||
if call
|
||||
.arguments
|
||||
.keywords
|
||||
.iter()
|
||||
.any(|keyword| keyword.arg.is_none())
|
||||
{
|
||||
Applicability::Unsafe
|
||||
} else {
|
||||
Applicability::Safe
|
||||
},
|
||||
));
|
||||
diagnostic.set_fix(Fix::display_only_edit(add_argument(
|
||||
"check=False",
|
||||
&call.arguments,
|
||||
checker.tokens(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ help: Add explicit `check=False`
|
||||
5 | subprocess.run("ls", shell=True)
|
||||
6 | subprocess.run(
|
||||
7 | ["ls"],
|
||||
note: This is a display-only fix and is likely to be incorrect
|
||||
|
||||
PLW1510 [*] `subprocess.run` without explicit `check` argument
|
||||
--> subprocess_run_without_check.py:5:1
|
||||
@@ -39,6 +40,7 @@ help: Add explicit `check=False`
|
||||
6 | subprocess.run(
|
||||
7 | ["ls"],
|
||||
8 | shell=False,
|
||||
note: This is a display-only fix and is likely to be incorrect
|
||||
|
||||
PLW1510 [*] `subprocess.run` without explicit `check` argument
|
||||
--> subprocess_run_without_check.py:6:1
|
||||
@@ -59,6 +61,7 @@ help: Add explicit `check=False`
|
||||
9 | )
|
||||
10 | subprocess.run(["ls"], **kwargs)
|
||||
11 |
|
||||
note: This is a display-only fix and is likely to be incorrect
|
||||
|
||||
PLW1510 [*] `subprocess.run` without explicit `check` argument
|
||||
--> subprocess_run_without_check.py:10:1
|
||||
@@ -79,4 +82,4 @@ help: Add explicit `check=False`
|
||||
11 |
|
||||
12 | # Non-errors.
|
||||
13 | subprocess.run("ls", check=True)
|
||||
note: This is an unsafe fix and may change runtime behavior
|
||||
note: This is a display-only fix and is likely to be incorrect
|
||||
|
||||
@@ -52,6 +52,7 @@ impl InvalidRuleCodeKind {
|
||||
pub(crate) struct InvalidRuleCode {
|
||||
pub(crate) rule_code: String,
|
||||
pub(crate) kind: InvalidRuleCodeKind,
|
||||
pub(crate) whole_comment: bool,
|
||||
}
|
||||
|
||||
impl AlwaysFixableViolation for InvalidRuleCode {
|
||||
@@ -65,7 +66,11 @@ impl AlwaysFixableViolation for InvalidRuleCode {
|
||||
}
|
||||
|
||||
fn fix_title(&self) -> String {
|
||||
"Remove the rule code".to_string()
|
||||
if self.whole_comment {
|
||||
format!("Remove the {} comment", self.kind.as_str())
|
||||
} else {
|
||||
format!("Remove the rule code `{}`", self.rule_code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +127,7 @@ fn all_codes_invalid_diagnostic(
|
||||
.collect::<Vec<_>>()
|
||||
.join(", "),
|
||||
kind: InvalidRuleCodeKind::Noqa,
|
||||
whole_comment: true,
|
||||
},
|
||||
directive.range(),
|
||||
)
|
||||
@@ -139,6 +145,7 @@ fn some_codes_are_invalid_diagnostic(
|
||||
InvalidRuleCode {
|
||||
rule_code: invalid_code.to_string(),
|
||||
kind: InvalidRuleCodeKind::Noqa,
|
||||
whole_comment: false,
|
||||
},
|
||||
invalid_code.range(),
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123
|
||||
3 | # External code
|
||||
4 | import re # noqa: V123
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the `# noqa` comment
|
||||
1 | # Invalid code
|
||||
- import os # noqa: INVALID123
|
||||
2 + import os
|
||||
@@ -28,7 +28,7 @@ RUF102 [*] Invalid rule code in `# noqa`: V123
|
||||
5 | # Valid noqa
|
||||
6 | import sys # noqa: E402
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the `# noqa` comment
|
||||
1 | # Invalid code
|
||||
2 | import os # noqa: INVALID123
|
||||
3 | # External code
|
||||
@@ -48,7 +48,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID456
|
||||
8 | from itertools import product # Preceeding comment # noqa: INVALID789
|
||||
9 | # Succeeding comment
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID456`
|
||||
4 | import re # noqa: V123
|
||||
5 | # Valid noqa
|
||||
6 | import sys # noqa: E402
|
||||
@@ -68,7 +68,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID789
|
||||
9 | # Succeeding comment
|
||||
10 | import math # noqa: INVALID000 # Succeeding comment
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the `# noqa` comment
|
||||
5 | # Valid noqa
|
||||
6 | import sys # noqa: E402
|
||||
7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456
|
||||
@@ -88,7 +88,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID000
|
||||
11 | # Mixed valid and invalid
|
||||
12 | from typing import List # noqa: F401, INVALID123
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the `# noqa` comment
|
||||
7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456
|
||||
8 | from itertools import product # Preceeding comment # noqa: INVALID789
|
||||
9 | # Succeeding comment
|
||||
@@ -108,7 +108,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123
|
||||
13 | # Test for multiple invalid
|
||||
14 | from collections import defaultdict # noqa: INVALID100, INVALID200, F401
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID123`
|
||||
9 | # Succeeding comment
|
||||
10 | import math # noqa: INVALID000 # Succeeding comment
|
||||
11 | # Mixed valid and invalid
|
||||
@@ -128,7 +128,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID100
|
||||
15 | # Test for preserving valid codes when fixing
|
||||
16 | from itertools import chain # noqa: E402, INVALID300, F401
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID100`
|
||||
11 | # Mixed valid and invalid
|
||||
12 | from typing import List # noqa: F401, INVALID123
|
||||
13 | # Test for multiple invalid
|
||||
@@ -148,7 +148,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID200
|
||||
15 | # Test for preserving valid codes when fixing
|
||||
16 | from itertools import chain # noqa: E402, INVALID300, F401
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID200`
|
||||
11 | # Mixed valid and invalid
|
||||
12 | from typing import List # noqa: F401, INVALID123
|
||||
13 | # Test for multiple invalid
|
||||
@@ -168,7 +168,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID300
|
||||
17 | # Test for mixed code types
|
||||
18 | import json # noqa: E402, INVALID400, V100
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID300`
|
||||
13 | # Test for multiple invalid
|
||||
14 | from collections import defaultdict # noqa: INVALID100, INVALID200, F401
|
||||
15 | # Test for preserving valid codes when fixing
|
||||
@@ -188,7 +188,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID400
|
||||
19 | # Test for rule redirects
|
||||
20 | import pandas as pd # noqa: TCH002
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID400`
|
||||
15 | # Test for preserving valid codes when fixing
|
||||
16 | from itertools import chain # noqa: E402, INVALID300, F401
|
||||
17 | # Test for mixed code types
|
||||
@@ -207,7 +207,7 @@ RUF102 [*] Invalid rule code in `# noqa`: V100
|
||||
19 | # Test for rule redirects
|
||||
20 | import pandas as pd # noqa: TCH002
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `V100`
|
||||
15 | # Test for preserving valid codes when fixing
|
||||
16 | from itertools import chain # noqa: E402, INVALID300, F401
|
||||
17 | # Test for mixed code types
|
||||
|
||||
@@ -10,7 +10,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123
|
||||
3 | # External code
|
||||
4 | import re # noqa: V123
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the `# noqa` comment
|
||||
1 | # Invalid code
|
||||
- import os # noqa: INVALID123
|
||||
2 + import os
|
||||
@@ -28,7 +28,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID456
|
||||
8 | from itertools import product # Preceeding comment # noqa: INVALID789
|
||||
9 | # Succeeding comment
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID456`
|
||||
4 | import re # noqa: V123
|
||||
5 | # Valid noqa
|
||||
6 | import sys # noqa: E402
|
||||
@@ -48,7 +48,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID789
|
||||
9 | # Succeeding comment
|
||||
10 | import math # noqa: INVALID000 # Succeeding comment
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the `# noqa` comment
|
||||
5 | # Valid noqa
|
||||
6 | import sys # noqa: E402
|
||||
7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456
|
||||
@@ -68,7 +68,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID000
|
||||
11 | # Mixed valid and invalid
|
||||
12 | from typing import List # noqa: F401, INVALID123
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the `# noqa` comment
|
||||
7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456
|
||||
8 | from itertools import product # Preceeding comment # noqa: INVALID789
|
||||
9 | # Succeeding comment
|
||||
@@ -88,7 +88,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123
|
||||
13 | # Test for multiple invalid
|
||||
14 | from collections import defaultdict # noqa: INVALID100, INVALID200, F401
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID123`
|
||||
9 | # Succeeding comment
|
||||
10 | import math # noqa: INVALID000 # Succeeding comment
|
||||
11 | # Mixed valid and invalid
|
||||
@@ -108,7 +108,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID100
|
||||
15 | # Test for preserving valid codes when fixing
|
||||
16 | from itertools import chain # noqa: E402, INVALID300, F401
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID100`
|
||||
11 | # Mixed valid and invalid
|
||||
12 | from typing import List # noqa: F401, INVALID123
|
||||
13 | # Test for multiple invalid
|
||||
@@ -128,7 +128,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID200
|
||||
15 | # Test for preserving valid codes when fixing
|
||||
16 | from itertools import chain # noqa: E402, INVALID300, F401
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID200`
|
||||
11 | # Mixed valid and invalid
|
||||
12 | from typing import List # noqa: F401, INVALID123
|
||||
13 | # Test for multiple invalid
|
||||
@@ -148,7 +148,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID300
|
||||
17 | # Test for mixed code types
|
||||
18 | import json # noqa: E402, INVALID400, V100
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID300`
|
||||
13 | # Test for multiple invalid
|
||||
14 | from collections import defaultdict # noqa: INVALID100, INVALID200, F401
|
||||
15 | # Test for preserving valid codes when fixing
|
||||
@@ -168,7 +168,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID400
|
||||
19 | # Test for rule redirects
|
||||
20 | import pandas as pd # noqa: TCH002
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `INVALID400`
|
||||
15 | # Test for preserving valid codes when fixing
|
||||
16 | from itertools import chain # noqa: E402, INVALID300, F401
|
||||
17 | # Test for mixed code types
|
||||
|
||||
@@ -7,7 +7,7 @@ source: crates/ruff_linter/src/rules/ruff/mod.rs
|
||||
|
||||
--- Summary ---
|
||||
Removed: 15
|
||||
Added: 23
|
||||
Added: 20
|
||||
|
||||
--- Removed ---
|
||||
E741 Ambiguous variable name: `I`
|
||||
@@ -301,6 +301,7 @@ RUF100 [*] Unused suppression (non-enabled: `E501`)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
47 | I = 1
|
||||
48 | # ruff: enable[E501]
|
||||
| --------------------
|
||||
|
|
||||
help: Remove unused suppression
|
||||
43 | def f():
|
||||
@@ -308,26 +309,10 @@ help: Remove unused suppression
|
||||
45 | # logged to user
|
||||
- # ruff: disable[E501]
|
||||
46 | I = 1
|
||||
47 | # ruff: enable[E501]
|
||||
48 |
|
||||
|
||||
|
||||
RUF100 [*] Unused suppression (non-enabled: `E501`)
|
||||
--> suppressions.py:48:5
|
||||
|
|
||||
46 | # ruff: disable[E501]
|
||||
47 | I = 1
|
||||
48 | # ruff: enable[E501]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: Remove unused suppression
|
||||
45 | # logged to user
|
||||
46 | # ruff: disable[E501]
|
||||
47 | I = 1
|
||||
- # ruff: enable[E501]
|
||||
47 |
|
||||
48 |
|
||||
49 |
|
||||
50 | def f():
|
||||
49 | def f():
|
||||
|
||||
|
||||
RUF100 [*] Unused `noqa` directive (unused: `E741`, `F841`)
|
||||
@@ -563,8 +548,11 @@ RUF102 [*] Invalid rule code in suppression: YF829
|
||||
| ^^^^^
|
||||
94 | # ruff: disable[F841, RQW320]
|
||||
95 | value = 0
|
||||
96 | # ruff: enable[F841, RQW320]
|
||||
97 | # ruff: enable[YF829]
|
||||
| -----
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the suppression comment
|
||||
90 |
|
||||
91 | def f():
|
||||
92 | # Unknown rule codes
|
||||
@@ -572,6 +560,10 @@ help: Remove the rule code
|
||||
93 | # ruff: disable[F841, RQW320]
|
||||
94 | value = 0
|
||||
95 | # ruff: enable[F841, RQW320]
|
||||
- # ruff: enable[YF829]
|
||||
96 |
|
||||
97 |
|
||||
98 | def f():
|
||||
|
||||
|
||||
RUF102 [*] Invalid rule code in suppression: RQW320
|
||||
@@ -583,30 +575,15 @@ RUF102 [*] Invalid rule code in suppression: RQW320
|
||||
| ^^^^^^
|
||||
95 | value = 0
|
||||
96 | # ruff: enable[F841, RQW320]
|
||||
| ------
|
||||
97 | # ruff: enable[YF829]
|
||||
|
|
||||
help: Remove the rule code
|
||||
help: Remove the rule code `RQW320`
|
||||
91 | def f():
|
||||
92 | # Unknown rule codes
|
||||
93 | # ruff: disable[YF829]
|
||||
- # ruff: disable[F841, RQW320]
|
||||
94 + # ruff: disable[F841]
|
||||
95 | value = 0
|
||||
96 | # ruff: enable[F841, RQW320]
|
||||
97 | # ruff: enable[YF829]
|
||||
|
||||
|
||||
RUF102 [*] Invalid rule code in suppression: RQW320
|
||||
--> suppressions.py:96:26
|
||||
|
|
||||
94 | # ruff: disable[F841, RQW320]
|
||||
95 | value = 0
|
||||
96 | # ruff: enable[F841, RQW320]
|
||||
| ^^^^^^
|
||||
97 | # ruff: enable[YF829]
|
||||
|
|
||||
help: Remove the rule code
|
||||
93 | # ruff: disable[YF829]
|
||||
94 | # ruff: disable[F841, RQW320]
|
||||
95 | value = 0
|
||||
- # ruff: enable[F841, RQW320]
|
||||
96 + # ruff: enable[F841]
|
||||
@@ -615,24 +592,6 @@ help: Remove the rule code
|
||||
99 |
|
||||
|
||||
|
||||
RUF102 [*] Invalid rule code in suppression: YF829
|
||||
--> suppressions.py:97:20
|
||||
|
|
||||
95 | value = 0
|
||||
96 | # ruff: enable[F841, RQW320]
|
||||
97 | # ruff: enable[YF829]
|
||||
| ^^^^^
|
||||
|
|
||||
help: Remove the rule code
|
||||
94 | # ruff: disable[F841, RQW320]
|
||||
95 | value = 0
|
||||
96 | # ruff: enable[F841, RQW320]
|
||||
- # ruff: enable[YF829]
|
||||
97 |
|
||||
98 |
|
||||
99 | def f():
|
||||
|
||||
|
||||
RUF103 [*] Invalid suppression comment: missing suppression codes like `[E501, ...]`
|
||||
--> suppressions.py:109:5
|
||||
|
|
||||
|
||||
@@ -36,6 +36,7 @@ pub enum PythonVersion {
|
||||
Py312,
|
||||
Py313,
|
||||
Py314,
|
||||
Py315,
|
||||
}
|
||||
|
||||
impl Default for PythonVersion {
|
||||
@@ -58,6 +59,7 @@ impl TryFrom<ast::PythonVersion> for PythonVersion {
|
||||
ast::PythonVersion::PY312 => Ok(Self::Py312),
|
||||
ast::PythonVersion::PY313 => Ok(Self::Py313),
|
||||
ast::PythonVersion::PY314 => Ok(Self::Py314),
|
||||
ast::PythonVersion::PY315 => Ok(Self::Py315),
|
||||
_ => Err(format!("unrecognized python version {value}")),
|
||||
}
|
||||
}
|
||||
@@ -88,6 +90,7 @@ impl PythonVersion {
|
||||
Self::Py312 => (3, 12),
|
||||
Self::Py313 => (3, 13),
|
||||
Self::Py314 => (3, 14),
|
||||
Self::Py315 => (3, 15),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -604,13 +607,21 @@ impl TryFrom<String> for RequiredVersion {
|
||||
type Error = pep440_rs::VersionSpecifiersParseError;
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
value.parse()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for RequiredVersion {
|
||||
type Err = pep440_rs::VersionSpecifiersParseError;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
// Treat `0.3.1` as `==0.3.1`, for backwards compatibility.
|
||||
if let Ok(version) = pep440_rs::Version::from_str(&value) {
|
||||
if let Ok(version) = pep440_rs::Version::from_str(value) {
|
||||
Ok(Self(VersionSpecifiers::from(
|
||||
VersionSpecifier::equals_version(version),
|
||||
)))
|
||||
} else {
|
||||
Ok(Self(VersionSpecifiers::from_str(&value)?))
|
||||
Ok(Self(VersionSpecifiers::from_str(value)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ pub enum SourceKind {
|
||||
Python(String),
|
||||
/// The source contains a Jupyter notebook.
|
||||
IpyNotebook(Box<Notebook>),
|
||||
/// The source contains Markdown text.
|
||||
Markdown(String),
|
||||
}
|
||||
|
||||
impl SourceKind {
|
||||
@@ -33,6 +35,7 @@ impl SourceKind {
|
||||
match self {
|
||||
SourceKind::IpyNotebook(notebook) => Some(notebook),
|
||||
SourceKind::Python(_) => None,
|
||||
SourceKind::Markdown(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,20 +43,36 @@ impl SourceKind {
|
||||
match self {
|
||||
SourceKind::Python(code) => Some(code),
|
||||
SourceKind::IpyNotebook(_) => None,
|
||||
SourceKind::Markdown(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_markdown(&self) -> Option<&str> {
|
||||
match self {
|
||||
SourceKind::Markdown(code) => Some(code),
|
||||
SourceKind::Python(_) => None,
|
||||
SourceKind::IpyNotebook(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_python(self) -> String {
|
||||
match self {
|
||||
SourceKind::Python(code) => code,
|
||||
SourceKind::IpyNotebook(_) => panic!("expected python code"),
|
||||
_ => panic!("expected python code"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_ipy_notebook(self) -> Notebook {
|
||||
match self {
|
||||
SourceKind::IpyNotebook(notebook) => *notebook,
|
||||
SourceKind::Python(_) => panic!("expected ipy notebook"),
|
||||
_ => panic!("expected ipy notebook"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_markdown(self) -> String {
|
||||
match self {
|
||||
SourceKind::Markdown(code) => code,
|
||||
_ => panic!("expected markdown text"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,6 +85,7 @@ impl SourceKind {
|
||||
SourceKind::IpyNotebook(cloned)
|
||||
}
|
||||
SourceKind::Python(_) => SourceKind::Python(new_source),
|
||||
SourceKind::Markdown(_) => SourceKind::Markdown(new_source),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,20 +94,28 @@ impl SourceKind {
|
||||
match self {
|
||||
SourceKind::Python(source) => source,
|
||||
SourceKind::IpyNotebook(notebook) => notebook.source_code(),
|
||||
SourceKind::Markdown(source) => source,
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the [`SourceKind`] from the given path. Returns `None` if the source is not a Python
|
||||
/// source file.
|
||||
pub fn from_path(path: &Path, source_type: PySourceType) -> Result<Option<Self>, SourceError> {
|
||||
if source_type.is_ipynb() {
|
||||
let notebook = Notebook::from_path(path)?;
|
||||
Ok(notebook
|
||||
.is_python_notebook()
|
||||
.then_some(Self::IpyNotebook(Box::new(notebook))))
|
||||
} else {
|
||||
let contents = std::fs::read_to_string(path)?;
|
||||
Ok(Some(Self::Python(contents)))
|
||||
match source_type {
|
||||
PySourceType::Ipynb => {
|
||||
let notebook = Notebook::from_path(path)?;
|
||||
Ok(notebook
|
||||
.is_python_notebook()
|
||||
.then_some(Self::IpyNotebook(Box::new(notebook))))
|
||||
}
|
||||
PySourceType::Markdown => {
|
||||
let contents = std::fs::read_to_string(path)?;
|
||||
Ok(Some(Self::Markdown(contents)))
|
||||
}
|
||||
PySourceType::Python | PySourceType::Stub => {
|
||||
let contents = std::fs::read_to_string(path)?;
|
||||
Ok(Some(Self::Python(contents)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +148,10 @@ impl SourceKind {
|
||||
notebook.write(writer)?;
|
||||
Ok(())
|
||||
}
|
||||
SourceKind::Markdown(source) => {
|
||||
writer.write_all(source.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,6 +172,10 @@ impl SourceKind {
|
||||
kind: DiffKind::IpyNotebook(src, dst),
|
||||
path,
|
||||
}),
|
||||
(SourceKind::Markdown(src), SourceKind::Markdown(dst)) => Some(SourceKindDiff {
|
||||
kind: DiffKind::Markdown(src, dst),
|
||||
path,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -212,6 +248,17 @@ impl std::fmt::Display for SourceKindDiff<'_> {
|
||||
|
||||
writeln!(f)?;
|
||||
}
|
||||
DiffKind::Markdown(original, modified) => {
|
||||
let mut diff = CodeDiff::new(original, modified);
|
||||
|
||||
let relative_path = self.path.map(fs::relativize_path);
|
||||
|
||||
if let Some(relative_path) = &relative_path {
|
||||
diff.header(relative_path, relative_path);
|
||||
}
|
||||
|
||||
writeln!(f, "{diff}")?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -222,6 +269,7 @@ impl std::fmt::Display for SourceKindDiff<'_> {
|
||||
enum DiffKind<'a> {
|
||||
Python(&'a str, &'a str),
|
||||
IpyNotebook(&'a Notebook, &'a Notebook),
|
||||
Markdown(&'a str, &'a str),
|
||||
}
|
||||
|
||||
struct CodeDiff<'a> {
|
||||
|
||||
@@ -13,7 +13,6 @@ use ruff_python_trivia::Cursor;
|
||||
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize, TextSlice};
|
||||
use smallvec::{SmallVec, smallvec};
|
||||
|
||||
use crate::Locator;
|
||||
use crate::checkers::ast::LintContext;
|
||||
use crate::codes::Rule;
|
||||
use crate::fix::edits::delete_comment;
|
||||
@@ -24,6 +23,7 @@ use crate::rules::ruff::rules::{
|
||||
UnmatchedSuppressionComment, UnusedCodes, UnusedNOQA, UnusedNOQAKind, code_is_valid,
|
||||
};
|
||||
use crate::settings::LinterSettings;
|
||||
use crate::{Locator, Violation};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
enum SuppressionAction {
|
||||
@@ -85,11 +85,39 @@ pub(crate) struct Suppression {
|
||||
/// Range for which the suppression applies
|
||||
range: TextRange,
|
||||
|
||||
/// Any comments associated with the suppression
|
||||
comments: SmallVec<[SuppressionComment; 2]>,
|
||||
|
||||
/// Whether this suppression actually suppressed a diagnostic
|
||||
used: Cell<bool>,
|
||||
|
||||
comments: DisableEnableComments,
|
||||
}
|
||||
|
||||
impl Suppression {
|
||||
fn codes(&self) -> &[TextRange] {
|
||||
&self.comments.disable_comment().codes
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum DisableEnableComments {
|
||||
/// An implicitly closed disable comment without a matching enable comment.
|
||||
Disable(SuppressionComment),
|
||||
/// A matching pair of disable and enable comments.
|
||||
DisableEnable(SuppressionComment, SuppressionComment),
|
||||
}
|
||||
|
||||
impl DisableEnableComments {
|
||||
pub(crate) fn disable_comment(&self) -> &SuppressionComment {
|
||||
match self {
|
||||
DisableEnableComments::Disable(comment) => comment,
|
||||
DisableEnableComments::DisableEnable(disable, _) => disable,
|
||||
}
|
||||
}
|
||||
pub(crate) fn enable_comment(&self) -> Option<&SuppressionComment> {
|
||||
match self {
|
||||
DisableEnableComments::Disable(_) => None,
|
||||
DisableEnableComments::DisableEnable(_, enable) => Some(enable),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
@@ -171,23 +199,17 @@ impl Suppressions {
|
||||
if !code_is_valid(&suppression.code, &context.settings().external) {
|
||||
// InvalidRuleCode
|
||||
if context.is_rule_enabled(Rule::InvalidRuleCode) {
|
||||
for comment in &suppression.comments {
|
||||
let (range, edit) = Suppressions::delete_code_or_comment(
|
||||
locator,
|
||||
suppression,
|
||||
comment,
|
||||
true,
|
||||
);
|
||||
context
|
||||
.report_diagnostic(
|
||||
InvalidRuleCode {
|
||||
rule_code: suppression.code.to_string(),
|
||||
kind: InvalidRuleCodeKind::Suppression,
|
||||
},
|
||||
range,
|
||||
)
|
||||
.set_fix(Fix::safe_edit(edit));
|
||||
}
|
||||
Suppressions::report_suppression(
|
||||
context,
|
||||
locator,
|
||||
suppression,
|
||||
true,
|
||||
InvalidRuleCode {
|
||||
rule_code: suppression.code.to_string(),
|
||||
kind: InvalidRuleCodeKind::Suppression,
|
||||
whole_comment: suppression.codes().len() == 1,
|
||||
},
|
||||
);
|
||||
}
|
||||
} else if !suppression.used.get() {
|
||||
// UnusedNOQA
|
||||
@@ -197,42 +219,37 @@ impl Suppressions {
|
||||
) else {
|
||||
continue; // "external" lint code, don't treat it as unused
|
||||
};
|
||||
for comment in &suppression.comments {
|
||||
let (range, edit) = Suppressions::delete_code_or_comment(
|
||||
locator,
|
||||
suppression,
|
||||
comment,
|
||||
false,
|
||||
);
|
||||
|
||||
let codes = if context.is_rule_enabled(rule) {
|
||||
UnusedCodes {
|
||||
unmatched: vec![suppression.code.to_string()],
|
||||
..Default::default()
|
||||
}
|
||||
} else {
|
||||
UnusedCodes {
|
||||
disabled: vec![suppression.code.to_string()],
|
||||
..Default::default()
|
||||
}
|
||||
};
|
||||
let codes = if context.is_rule_enabled(rule) {
|
||||
UnusedCodes {
|
||||
unmatched: vec![suppression.code.to_string()],
|
||||
..Default::default()
|
||||
}
|
||||
} else {
|
||||
UnusedCodes {
|
||||
disabled: vec![suppression.code.to_string()],
|
||||
..Default::default()
|
||||
}
|
||||
};
|
||||
|
||||
context
|
||||
.report_diagnostic(
|
||||
UnusedNOQA {
|
||||
codes: Some(codes),
|
||||
kind: UnusedNOQAKind::Suppression,
|
||||
},
|
||||
range,
|
||||
)
|
||||
.set_fix(Fix::safe_edit(edit));
|
||||
}
|
||||
Suppressions::report_suppression(
|
||||
context,
|
||||
locator,
|
||||
suppression,
|
||||
false,
|
||||
UnusedNOQA {
|
||||
codes: Some(codes),
|
||||
kind: UnusedNOQAKind::Suppression,
|
||||
},
|
||||
);
|
||||
}
|
||||
} else if suppression.comments.len() == 1 {
|
||||
} else if let DisableEnableComments::Disable(comment) = &suppression.comments {
|
||||
// UnmatchedSuppressionComment
|
||||
let range = suppression.comments[0].range;
|
||||
if unmatched_ranges.insert(range) {
|
||||
context.report_diagnostic_if_enabled(UnmatchedSuppressionComment {}, range);
|
||||
if unmatched_ranges.insert(comment.range) {
|
||||
context.report_diagnostic_if_enabled(
|
||||
UnmatchedSuppressionComment {},
|
||||
comment.range,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -267,6 +284,35 @@ impl Suppressions {
|
||||
}
|
||||
}
|
||||
|
||||
fn report_suppression<T: Violation>(
|
||||
context: &LintContext,
|
||||
locator: &Locator,
|
||||
suppression: &Suppression,
|
||||
highlight_only_code: bool,
|
||||
kind: T,
|
||||
) {
|
||||
let disable_comment = suppression.comments.disable_comment();
|
||||
let (range, edit) = Suppressions::delete_code_or_comment(
|
||||
locator,
|
||||
suppression,
|
||||
disable_comment,
|
||||
highlight_only_code,
|
||||
);
|
||||
let mut diagnostic = context.report_diagnostic(kind, range);
|
||||
if let Some(enable_comment) = suppression.comments.enable_comment() {
|
||||
let (enable_range, enable_range_edit) = Suppressions::delete_code_or_comment(
|
||||
locator,
|
||||
suppression,
|
||||
enable_comment,
|
||||
highlight_only_code,
|
||||
);
|
||||
diagnostic.secondary_annotation("", enable_range);
|
||||
diagnostic.set_fix(Fix::safe_edits(edit, [enable_range_edit]));
|
||||
} else {
|
||||
diagnostic.set_fix(Fix::safe_edit(edit));
|
||||
}
|
||||
}
|
||||
|
||||
fn delete_code_or_comment(
|
||||
locator: &Locator<'_>,
|
||||
suppression: &Suppression,
|
||||
@@ -424,7 +470,10 @@ impl<'a> SuppressionsBuilder<'a> {
|
||||
self.valid.push(Suppression {
|
||||
code: code.into(),
|
||||
range: combined_range,
|
||||
comments: smallvec![comment.comment.clone(), other.comment.clone()],
|
||||
comments: DisableEnableComments::DisableEnable(
|
||||
comment.comment.clone(),
|
||||
other.comment.clone(),
|
||||
),
|
||||
used: false.into(),
|
||||
});
|
||||
}
|
||||
@@ -441,7 +490,7 @@ impl<'a> SuppressionsBuilder<'a> {
|
||||
self.valid.push(Suppression {
|
||||
code: code.into(),
|
||||
range: implicit_range,
|
||||
comments: smallvec![comment.comment.clone()],
|
||||
comments: DisableEnableComments::Disable(comment.comment.clone()),
|
||||
used: false.into(),
|
||||
});
|
||||
}
|
||||
@@ -643,7 +692,7 @@ mod tests {
|
||||
use insta::assert_debug_snapshot;
|
||||
use itertools::Itertools;
|
||||
use ruff_python_parser::{Mode, ParseOptions, parse};
|
||||
use ruff_text_size::{TextRange, TextSize};
|
||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||
use similar::DiffableStr;
|
||||
|
||||
use crate::{
|
||||
@@ -705,24 +754,22 @@ print('hello')
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo]\nprint('hello')\n# ruff: enable[foo]",
|
||||
code: "foo",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[foo]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[foo]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
],
|
||||
invalid: [],
|
||||
@@ -751,30 +798,28 @@ def foo():
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[bar]\n print('hello')\n\n",
|
||||
code: "bar",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: None,
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo]\nprint('hello')\n\ndef foo():\n # ruff: disable[bar]\n print('hello')\n\n",
|
||||
code: "foo",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: None,
|
||||
},
|
||||
],
|
||||
invalid: [],
|
||||
@@ -803,46 +848,42 @@ class Foo:
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[bar]\n print('hello')\n # ruff: enable[bar]",
|
||||
code: "bar",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[bar]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[bar]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo]\n def bar(self):\n # ruff: disable[bar]\n print('hello')\n # ruff: enable[bar]\n # ruff: enable[foo]",
|
||||
code: "foo",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[foo]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[foo]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
],
|
||||
invalid: [],
|
||||
@@ -872,46 +913,42 @@ def foo():
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo]\n print('hello')\n # ruff: disable[bar]\n print('hello')\n # ruff: enable[foo]",
|
||||
code: "foo",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[foo]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[foo]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[bar]\n print('hello')\n # ruff: enable[foo]\n print('hello')\n # ruff: enable[bar]",
|
||||
code: "bar",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[bar]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[bar]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
],
|
||||
invalid: [],
|
||||
@@ -936,50 +973,46 @@ print('hello')
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo, bar]\nprint('hello')\n# ruff: enable[foo, bar]",
|
||||
code: "foo",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo, bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[foo, bar]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo, bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[foo, bar]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo, bar]\nprint('hello')\n# ruff: enable[foo, bar]",
|
||||
code: "bar",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo, bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[foo, bar]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo, bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[foo, bar]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
],
|
||||
invalid: [],
|
||||
@@ -1005,16 +1038,15 @@ print('world')
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo]\nprint('hello')\n# ruff: enable[bar]\nprint('world')\n",
|
||||
code: "foo",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: None,
|
||||
},
|
||||
],
|
||||
invalid: [
|
||||
@@ -1051,32 +1083,30 @@ print('hello')
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo, bar]\nprint('hello')\n# ruff: enable[bar, foo]\n",
|
||||
code: "foo",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo, bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo, bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: None,
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo, bar]\nprint('hello')\n# ruff: enable[bar, foo]\n",
|
||||
code: "bar",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo, bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo, bar]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
"bar",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: None,
|
||||
},
|
||||
],
|
||||
invalid: [
|
||||
@@ -1116,38 +1146,35 @@ print('hello')
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo] first\nprint('hello')\n# ruff: disable[foo] second\nprint('hello')\n# ruff: enable[foo]",
|
||||
code: "foo",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo] first",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "first",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[foo]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo] first",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "first",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[foo]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[foo] second\nprint('hello')\n# ruff: enable[foo]\n",
|
||||
code: "foo",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[foo] second",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "second",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[foo] second",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"foo",
|
||||
],
|
||||
reason: "second",
|
||||
},
|
||||
enable_comment: None,
|
||||
},
|
||||
],
|
||||
invalid: [],
|
||||
@@ -1189,100 +1216,92 @@ def bar():
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[delta] unmatched\n pass\n # ruff: enable[beta,gamma]\n# ruff: enable[alpha]\n\n# ruff: disable # parse error!\n",
|
||||
code: "delta",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[delta] unmatched",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"delta",
|
||||
],
|
||||
reason: "unmatched",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[delta] unmatched",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"delta",
|
||||
],
|
||||
reason: "unmatched",
|
||||
},
|
||||
enable_comment: None,
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[beta,gamma]\n if True:\n # ruff: disable[delta] unmatched\n pass\n # ruff: enable[beta,gamma]",
|
||||
code: "beta",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[beta,gamma]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"beta",
|
||||
"gamma",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[beta,gamma]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"beta",
|
||||
"gamma",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[beta,gamma]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"beta",
|
||||
"gamma",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[beta,gamma]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"beta",
|
||||
"gamma",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[beta,gamma]\n if True:\n # ruff: disable[delta] unmatched\n pass\n # ruff: enable[beta,gamma]",
|
||||
code: "gamma",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[beta,gamma]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"beta",
|
||||
"gamma",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[beta,gamma]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"beta",
|
||||
"gamma",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[beta,gamma]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"beta",
|
||||
"gamma",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[beta,gamma]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"beta",
|
||||
"gamma",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[zeta] unmatched\n pass\n# ruff: enable[zeta] underindented\n pass\n",
|
||||
code: "zeta",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[zeta] unmatched",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"zeta",
|
||||
],
|
||||
reason: "unmatched",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[zeta] unmatched",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"zeta",
|
||||
],
|
||||
reason: "unmatched",
|
||||
},
|
||||
enable_comment: None,
|
||||
},
|
||||
Suppression {
|
||||
covered_source: "# ruff: disable[alpha]\ndef foo():\n # ruff: disable[beta,gamma]\n if True:\n # ruff: disable[delta] unmatched\n pass\n # ruff: enable[beta,gamma]\n# ruff: enable[alpha]",
|
||||
code: "alpha",
|
||||
comments: [
|
||||
SuppressionComment {
|
||||
text: "# ruff: disable[alpha]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"alpha",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
SuppressionComment {
|
||||
text: "# ruff: enable[alpha]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"alpha",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
],
|
||||
disable_comment: SuppressionComment {
|
||||
text: "# ruff: disable[alpha]",
|
||||
action: Disable,
|
||||
codes: [
|
||||
"alpha",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
enable_comment: SuppressionComment {
|
||||
text: "# ruff: enable[alpha]",
|
||||
action: Enable,
|
||||
codes: [
|
||||
"alpha",
|
||||
],
|
||||
reason: "",
|
||||
},
|
||||
},
|
||||
],
|
||||
invalid: [
|
||||
@@ -1532,10 +1551,8 @@ def bar():
|
||||
#[test]
|
||||
fn comment_attributes() {
|
||||
let source = "# ruff: disable[foo, bar] hello world";
|
||||
let mut parser = SuppressionParser::new(
|
||||
source,
|
||||
TextRange::new(0.into(), TextSize::try_from(source.len()).unwrap()),
|
||||
);
|
||||
let mut parser =
|
||||
SuppressionParser::new(source, TextRange::new(0.into(), source.text_len()));
|
||||
let comment = parser.parse_comment().unwrap();
|
||||
assert_eq!(comment.action, SuppressionAction::Disable);
|
||||
assert_eq!(
|
||||
@@ -1554,12 +1571,12 @@ def bar():
|
||||
source: &'_ str,
|
||||
) -> Result<DebugSuppressionComment<'_>, ParseError> {
|
||||
let offset = TextSize::new(source.find('#').unwrap_or(0).try_into().unwrap());
|
||||
let mut parser = SuppressionParser::new(
|
||||
source,
|
||||
TextRange::new(offset, TextSize::try_from(source.len()).unwrap()),
|
||||
);
|
||||
let mut parser = SuppressionParser::new(source, TextRange::new(offset, source.text_len()));
|
||||
match parser.parse_comment() {
|
||||
Ok(comment) => Ok(DebugSuppressionComment { source, comment }),
|
||||
Ok(comment) => Ok(DebugSuppressionComment {
|
||||
source,
|
||||
comment: Some(comment),
|
||||
}),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
@@ -1639,16 +1656,18 @@ def bar():
|
||||
.field("covered_source", &&self.source[self.suppression.range])
|
||||
.field("code", &self.suppression.code)
|
||||
.field(
|
||||
"comments",
|
||||
&self
|
||||
.suppression
|
||||
.comments
|
||||
.iter()
|
||||
.map(|comment| DebugSuppressionComment {
|
||||
source: self.source,
|
||||
comment: comment.clone(),
|
||||
})
|
||||
.collect_vec(),
|
||||
"disable_comment",
|
||||
&DebugSuppressionComment {
|
||||
source: self.source,
|
||||
comment: Some(self.suppression.comments.disable_comment().clone()),
|
||||
},
|
||||
)
|
||||
.field(
|
||||
"enable_comment",
|
||||
&DebugSuppressionComment {
|
||||
source: self.source,
|
||||
comment: self.suppression.comments.enable_comment().cloned(),
|
||||
},
|
||||
)
|
||||
.finish()
|
||||
}
|
||||
@@ -1667,7 +1686,7 @@ def bar():
|
||||
"comment",
|
||||
&DebugSuppressionComment {
|
||||
source: self.source,
|
||||
comment: self.invalid.comment.clone(),
|
||||
comment: Some(self.invalid.comment.clone()),
|
||||
},
|
||||
)
|
||||
.finish()
|
||||
@@ -1690,23 +1709,27 @@ def bar():
|
||||
|
||||
struct DebugSuppressionComment<'a> {
|
||||
source: &'a str,
|
||||
comment: SuppressionComment,
|
||||
comment: Option<SuppressionComment>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for DebugSuppressionComment<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("SuppressionComment")
|
||||
.field("text", &&self.source[self.comment.range])
|
||||
.field("action", &self.comment.action)
|
||||
.field(
|
||||
"codes",
|
||||
&DebugCodes {
|
||||
source: self.source,
|
||||
codes: &self.comment.codes,
|
||||
},
|
||||
)
|
||||
.field("reason", &&self.source[self.comment.reason])
|
||||
.finish()
|
||||
match &self.comment {
|
||||
Some(comment) => f
|
||||
.debug_struct("SuppressionComment")
|
||||
.field("text", &&self.source[comment.range])
|
||||
.field("action", &comment.action)
|
||||
.field(
|
||||
"codes",
|
||||
&DebugCodes {
|
||||
source: self.source,
|
||||
codes: &comment.codes,
|
||||
},
|
||||
)
|
||||
.field("reason", &&self.source[comment.reason])
|
||||
.finish(),
|
||||
None => f.debug_tuple("None").finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,8 @@ pub enum PySourceType {
|
||||
Stub,
|
||||
/// The source is a Jupyter notebook (`.ipynb`).
|
||||
Ipynb,
|
||||
/// The source is a Markdown file (`.md`).
|
||||
Markdown,
|
||||
}
|
||||
|
||||
impl PySourceType {
|
||||
@@ -106,6 +108,7 @@ impl PySourceType {
|
||||
"pyi" => Self::Stub,
|
||||
"pyw" => Self::Python,
|
||||
"ipynb" => Self::Ipynb,
|
||||
"md" => Self::Markdown,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
@@ -134,6 +137,10 @@ impl PySourceType {
|
||||
pub const fn is_ipynb(self) -> bool {
|
||||
matches!(self, Self::Ipynb)
|
||||
}
|
||||
|
||||
pub const fn is_markdown(self) -> bool {
|
||||
matches!(self, Self::Markdown)
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: AsRef<Path>> From<P> for PySourceType {
|
||||
|
||||
@@ -35,6 +35,10 @@ impl PythonVersion {
|
||||
major: 3,
|
||||
minor: 14,
|
||||
};
|
||||
pub const PY315: PythonVersion = PythonVersion {
|
||||
major: 3,
|
||||
minor: 15,
|
||||
};
|
||||
|
||||
pub fn iter() -> impl Iterator<Item = PythonVersion> {
|
||||
[
|
||||
@@ -46,6 +50,7 @@ impl PythonVersion {
|
||||
PythonVersion::PY312,
|
||||
PythonVersion::PY313,
|
||||
PythonVersion::PY314,
|
||||
PythonVersion::PY315,
|
||||
]
|
||||
.into_iter()
|
||||
}
|
||||
@@ -61,7 +66,7 @@ impl PythonVersion {
|
||||
|
||||
/// The latest Python version supported in preview
|
||||
pub fn latest_preview() -> Self {
|
||||
let latest_preview = Self::PY314;
|
||||
let latest_preview = Self::PY315;
|
||||
debug_assert!(latest_preview >= Self::latest());
|
||||
latest_preview
|
||||
}
|
||||
|
||||
@@ -91,20 +91,22 @@ def example(session):
|
||||
.all()
|
||||
# fmt: on
|
||||
def off_and_on_without_data():
|
||||
"""All comments here are technically on the same prefix.
|
||||
|
||||
The comments between will be formatted. This is a known limitation.
|
||||
"""
|
||||
"""Test that comment-only fmt:off/on blocks preserve formatting."""
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
|
||||
|
||||
#hey, that won't work
|
||||
|
||||
#should not be formatted
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
pass
|
||||
def on_and_off_broken():
|
||||
"""Another known limitation."""
|
||||
def on_and_off_with_comment_only_blocks():
|
||||
"""Test that fmt:off/on works with multiple directives and comment-only blocks."""
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
this=should.not_be.formatted()
|
||||
@@ -113,7 +115,16 @@ def on_and_off_broken():
|
||||
now . considers . multiple . fmt . directives . within . one . prefix
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
# ...but comments still get reformatted even though they should not be
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
|
||||
#should not be formatted
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
def long_lines():
|
||||
if True:
|
||||
@@ -178,6 +189,50 @@ cfg.rule(
|
||||
# fmt: on
|
||||
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5
|
||||
)
|
||||
|
||||
# Test comment-only blocks at file level with various spacing patterns
|
||||
# fmt: off
|
||||
#nospace
|
||||
# twospaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#nospaceatall
|
||||
#extraspaces
|
||||
#evenmorespaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#SBATCH --job-name=test
|
||||
#SBATCH --output=test.out
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#first
|
||||
|
||||
#second
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#!@#$%^&*()
|
||||
#<=>+-*/
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#x=1+2
|
||||
#y = 3
|
||||
#z = 4
|
||||
# fmt: on
|
||||
|
||||
# fmt: off
|
||||
yield 'hello'
|
||||
# No formatting to the end of the file
|
||||
|
||||
@@ -112,29 +112,42 @@ def example(session):
|
||||
|
||||
|
||||
def off_and_on_without_data():
|
||||
"""All comments here are technically on the same prefix.
|
||||
|
||||
The comments between will be formatted. This is a known limitation.
|
||||
"""
|
||||
"""Test that comment-only fmt:off/on blocks preserve formatting."""
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
|
||||
# hey, that won't work
|
||||
#should not be formatted
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
pass
|
||||
|
||||
|
||||
def on_and_off_broken():
|
||||
"""Another known limitation."""
|
||||
def on_and_off_with_comment_only_blocks():
|
||||
"""Test that fmt:off/on works with multiple directives and comment-only blocks."""
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
this=should.not_be.formatted()
|
||||
and_=indeed . it is not formatted
|
||||
because . the . handling . inside . generate_ignored_nodes()
|
||||
now . considers . multiple . fmt . directives . within . one . prefix
|
||||
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
# ...but comments still get reformatted even though they should not be
|
||||
|
||||
#should not be formatted
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
|
||||
|
||||
@@ -211,6 +224,50 @@ cfg.rule(
|
||||
# fmt: on
|
||||
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5,
|
||||
)
|
||||
|
||||
# Test comment-only blocks at file level with various spacing patterns
|
||||
# fmt: off
|
||||
#nospace
|
||||
# twospaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#nospaceatall
|
||||
#extraspaces
|
||||
#evenmorespaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#SBATCH --job-name=test
|
||||
#SBATCH --output=test.out
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#first
|
||||
|
||||
#second
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#!@#$%^&*()
|
||||
#<=>+-*/
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#x=1+2
|
||||
#y = 3
|
||||
#z = 4
|
||||
# fmt: on
|
||||
|
||||
# fmt: off
|
||||
yield 'hello'
|
||||
# No formatting to the end of the file
|
||||
|
||||
@@ -1,8 +1,21 @@
|
||||
def foo(): return "mock" # fmt: skip
|
||||
if True: print("yay") # fmt: skip
|
||||
for i in range(10): print(i) # fmt: skip
|
||||
if True: print("this"); print("that") # fmt: skip
|
||||
while True: print("loop"); break # fmt: skip
|
||||
for x in [1, 2]: print(x); print("done") # fmt: skip
|
||||
def f(x: int): return x # fmt: skip
|
||||
|
||||
j = 1 # fmt: skip
|
||||
while j < 10: j += 1 # fmt: skip
|
||||
|
||||
b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip
|
||||
|
||||
v = (
|
||||
foo_dict # fmt: skip
|
||||
.setdefault("a", {})
|
||||
.setdefault("b", {})
|
||||
.setdefault("c", {})
|
||||
.setdefault("d", {})
|
||||
.setdefault("e", {})
|
||||
)
|
||||
|
||||
@@ -1,8 +1,21 @@
|
||||
def foo(): return "mock" # fmt: skip
|
||||
if True: print("yay") # fmt: skip
|
||||
for i in range(10): print(i) # fmt: skip
|
||||
if True: print("this"); print("that") # fmt: skip
|
||||
while True: print("loop"); break # fmt: skip
|
||||
for x in [1, 2]: print(x); print("done") # fmt: skip
|
||||
def f(x: int): return x # fmt: skip
|
||||
|
||||
j = 1 # fmt: skip
|
||||
while j < 10: j += 1 # fmt: skip
|
||||
|
||||
b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip
|
||||
|
||||
v = (
|
||||
foo_dict # fmt: skip
|
||||
.setdefault("a", {})
|
||||
.setdefault("b", {})
|
||||
.setdefault("c", {})
|
||||
.setdefault("d", {})
|
||||
.setdefault("e", {})
|
||||
)
|
||||
|
||||
@@ -4,3 +4,84 @@ def foo():
|
||||
|
||||
# comment 1 # fmt: skip
|
||||
# comment 2
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3,
|
||||
4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3,
|
||||
4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3,
|
||||
4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3,
|
||||
4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
if False:
|
||||
# fmt: off # some other comment
|
||||
pass
|
||||
|
||||
@@ -4,3 +4,84 @@ def foo():
|
||||
|
||||
# comment 1 # fmt: skip
|
||||
# comment 2
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3,
|
||||
4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3,
|
||||
4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3,
|
||||
4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3,
|
||||
4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
if False:
|
||||
# fmt: off # some other comment
|
||||
pass
|
||||
|
||||
1
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip12.options.json
vendored
Normal file
1
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip12.options.json
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"preview": "enabled"}
|
||||
8
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip12.py
vendored
Normal file
8
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip12.py
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
with open("file.txt") as f: content = f.read() # fmt: skip
|
||||
|
||||
# Ideally, only the last line would be ignored
|
||||
# But ignoring only part of the asexpr_test causes a parse error
|
||||
# Same with ignoring the asexpr_test without also ignoring the entire with_stmt
|
||||
with open (
|
||||
"file.txt" ,
|
||||
) as f: content = f.read() # fmt: skip
|
||||
8
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip12.py.expect
vendored
Normal file
8
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip12.py.expect
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
with open("file.txt") as f: content = f.read() # fmt: skip
|
||||
|
||||
# Ideally, only the last line would be ignored
|
||||
# But ignoring only part of the asexpr_test causes a parse error
|
||||
# Same with ignoring the asexpr_test without also ignoring the entire with_stmt
|
||||
with open (
|
||||
"file.txt" ,
|
||||
) as f: content = f.read() # fmt: skip
|
||||
1
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip13.options.json
vendored
Normal file
1
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip13.options.json
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"preview": "enabled"}
|
||||
28
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip13.py
vendored
Normal file
28
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip13.py
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
t = (
|
||||
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
|
||||
t = (
|
||||
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
{"foo": "bar",},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{"foo": "bar",},
|
||||
)
|
||||
32
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip13.py.expect
vendored
Normal file
32
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip13.py.expect
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
t = (
|
||||
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
|
||||
t = (
|
||||
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
{
|
||||
"foo": "bar",
|
||||
},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{
|
||||
"foo": "bar",
|
||||
},
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
a = "this is some code"
|
||||
b = 5 #fmt:skip
|
||||
b = 5 # fmt:skip
|
||||
c = 9 #fmt: skip
|
||||
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" #fmt:skip
|
||||
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
a = "this is some code"
|
||||
b = 5 # fmt:skip
|
||||
c = 9 # fmt: skip
|
||||
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip
|
||||
b = 5 # fmt:skip
|
||||
c = 9 #fmt: skip
|
||||
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip
|
||||
|
||||
19
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip_multiple_in_clause.py
vendored
Normal file
19
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip_multiple_in_clause.py
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
# Multiple fmt: skip in multi-part if-clause
|
||||
class ClassWithALongName:
|
||||
Constant1 = 1
|
||||
Constant2 = 2
|
||||
Constant3 = 3
|
||||
|
||||
|
||||
def test():
|
||||
if (
|
||||
"cond1" == "cond1"
|
||||
and "cond2" == "cond2"
|
||||
and 1 in (
|
||||
ClassWithALongName.Constant1,
|
||||
ClassWithALongName.Constant2,
|
||||
ClassWithALongName.Constant3, # fmt: skip
|
||||
) # fmt: skip
|
||||
):
|
||||
return True
|
||||
return False
|
||||
@@ -0,0 +1,19 @@
|
||||
# Multiple fmt: skip in multi-part if-clause
|
||||
class ClassWithALongName:
|
||||
Constant1 = 1
|
||||
Constant2 = 2
|
||||
Constant3 = 3
|
||||
|
||||
|
||||
def test():
|
||||
if (
|
||||
"cond1" == "cond1"
|
||||
and "cond2" == "cond2"
|
||||
and 1 in (
|
||||
ClassWithALongName.Constant1,
|
||||
ClassWithALongName.Constant2,
|
||||
ClassWithALongName.Constant3, # fmt: skip
|
||||
) # fmt: skip
|
||||
):
|
||||
return True
|
||||
return False
|
||||
35
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip_multiple_strings.py
vendored
Normal file
35
crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip_multiple_strings.py
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
# Multiple fmt: skip on string literals
|
||||
a = (
|
||||
"this should " # fmt: skip
|
||||
"be fine"
|
||||
)
|
||||
|
||||
b = (
|
||||
"this is " # fmt: skip
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
c = (
|
||||
"and neither " # fmt: skip
|
||||
"is this " # fmt: skip
|
||||
"working"
|
||||
)
|
||||
|
||||
d = (
|
||||
"nor "
|
||||
"is this " # fmt: skip
|
||||
"working" # fmt: skip
|
||||
)
|
||||
|
||||
e = (
|
||||
"and this " # fmt: skip
|
||||
"is definitely "
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
# Dictionary entries with fmt: skip (covers issue with long lines)
|
||||
hotkeys = {
|
||||
"editor:swap-line-down": [{"key": "ArrowDown", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
"editor:swap-line-up": [{"key": "ArrowUp", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
"editor:toggle-source": [{"key": "S", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
# Multiple fmt: skip on string literals
|
||||
a = (
|
||||
"this should " # fmt: skip
|
||||
"be fine"
|
||||
)
|
||||
|
||||
b = (
|
||||
"this is " # fmt: skip
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
c = (
|
||||
"and neither " # fmt: skip
|
||||
"is this " # fmt: skip
|
||||
"working"
|
||||
)
|
||||
|
||||
d = (
|
||||
"nor "
|
||||
"is this " # fmt: skip
|
||||
"working" # fmt: skip
|
||||
)
|
||||
|
||||
e = (
|
||||
"and this " # fmt: skip
|
||||
"is definitely "
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
# Dictionary entries with fmt: skip (covers issue with long lines)
|
||||
hotkeys = {
|
||||
"editor:swap-line-down": [{"key": "ArrowDown", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
"editor:swap-line-up": [{"key": "ArrowUp", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
"editor:toggle-source": [{"key": "S", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
}
|
||||
24
crates/ruff_python_formatter/resources/test/fixtures/black/cases/jupytext_markdown_fmt.py
vendored
Normal file
24
crates/ruff_python_formatter/resources/test/fixtures/black/cases/jupytext_markdown_fmt.py
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# Test that Jupytext markdown comments are preserved before fmt:off/on blocks
|
||||
# %% [markdown]
|
||||
|
||||
# fmt: off
|
||||
# fmt: on
|
||||
|
||||
# Also test with other comments
|
||||
# Some comment
|
||||
# %% [markdown]
|
||||
# Another comment
|
||||
|
||||
# fmt: off
|
||||
x = 1
|
||||
# fmt: on
|
||||
|
||||
# Test multiple markdown comments
|
||||
# %% [markdown]
|
||||
# First markdown
|
||||
# %% [code]
|
||||
# Code cell
|
||||
|
||||
# fmt: off
|
||||
y = 2
|
||||
# fmt: on
|
||||
24
crates/ruff_python_formatter/resources/test/fixtures/black/cases/jupytext_markdown_fmt.py.expect
vendored
Normal file
24
crates/ruff_python_formatter/resources/test/fixtures/black/cases/jupytext_markdown_fmt.py.expect
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# Test that Jupytext markdown comments are preserved before fmt:off/on blocks
|
||||
# %% [markdown]
|
||||
|
||||
# fmt: off
|
||||
# fmt: on
|
||||
|
||||
# Also test with other comments
|
||||
# Some comment
|
||||
# %% [markdown]
|
||||
# Another comment
|
||||
|
||||
# fmt: off
|
||||
x = 1
|
||||
# fmt: on
|
||||
|
||||
# Test multiple markdown comments
|
||||
# %% [markdown]
|
||||
# First markdown
|
||||
# %% [code]
|
||||
# Code cell
|
||||
|
||||
# fmt: off
|
||||
y = 2
|
||||
# fmt: on
|
||||
1
crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_750.options.json
vendored
Normal file
1
crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_750.options.json
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"target_version": "3.14"}
|
||||
40
crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_750.py
vendored
Normal file
40
crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_750.py
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
x = t"foo"
|
||||
x = t'foo {{ {2 + 2}bar {{ baz'
|
||||
|
||||
x = t"foo {f'abc'} bar"
|
||||
|
||||
x = t"""foo {{ a
|
||||
foo {2 + 2}bar {{ baz
|
||||
|
||||
x = f"foo {{ {
|
||||
2 + 2 # comment
|
||||
}bar"
|
||||
|
||||
{{ baz
|
||||
|
||||
}} buzz
|
||||
|
||||
{print("abc" + "def"
|
||||
)}
|
||||
abc"""
|
||||
|
||||
t'{(abc:=10)}'
|
||||
|
||||
t'''This is a really long string, but just make sure that you reflow tstrings {
|
||||
2+2:d
|
||||
}'''
|
||||
t'This is a really long string, but just make sure that you reflow tstrings correctly {2+2:d}'
|
||||
|
||||
t"{ 2 + 2 = }"
|
||||
|
||||
t'{
|
||||
X
|
||||
!r
|
||||
}'
|
||||
|
||||
tr'\{{\}}'
|
||||
|
||||
t'''
|
||||
WITH {f'''
|
||||
{1}_cte AS ()'''}
|
||||
'''
|
||||
40
crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_750.py.expect
vendored
Normal file
40
crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_750.py.expect
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
x = t"foo"
|
||||
x = t"foo {{ {2 + 2}bar {{ baz"
|
||||
|
||||
x = t"foo {f'abc'} bar"
|
||||
|
||||
x = t"""foo {{ a
|
||||
foo {2 + 2}bar {{ baz
|
||||
|
||||
x = f"foo {{ {
|
||||
2 + 2 # comment
|
||||
}bar"
|
||||
|
||||
{{ baz
|
||||
|
||||
}} buzz
|
||||
|
||||
{print("abc" + "def"
|
||||
)}
|
||||
abc"""
|
||||
|
||||
t"{(abc:=10)}"
|
||||
|
||||
t"""This is a really long string, but just make sure that you reflow tstrings {
|
||||
2+2:d
|
||||
}"""
|
||||
t"This is a really long string, but just make sure that you reflow tstrings correctly {2+2:d}"
|
||||
|
||||
t"{ 2 + 2 = }"
|
||||
|
||||
t"{
|
||||
X
|
||||
!r
|
||||
}"
|
||||
|
||||
rt"\{{\}}"
|
||||
|
||||
t"""
|
||||
WITH {f'''
|
||||
{1}_cte AS ()'''}
|
||||
"""
|
||||
@@ -0,0 +1 @@
|
||||
{"preview": "enabled"}
|
||||
19
crates/ruff_python_formatter/resources/test/fixtures/black/cases/preview_fmtpass_imports.py
vendored
Normal file
19
crates/ruff_python_formatter/resources/test/fixtures/black/cases/preview_fmtpass_imports.py
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
# Regression test for https://github.com/psf/black/issues/3438
|
||||
|
||||
import ast
|
||||
import collections # fmt: skip
|
||||
import dataclasses
|
||||
# fmt: off
|
||||
import os
|
||||
# fmt: on
|
||||
import pathlib
|
||||
|
||||
import re # fmt: skip
|
||||
import secrets
|
||||
|
||||
# fmt: off
|
||||
import sys
|
||||
# fmt: on
|
||||
|
||||
import tempfile
|
||||
import zoneinfo
|
||||
@@ -0,0 +1,19 @@
|
||||
# Regression test for https://github.com/psf/black/issues/3438
|
||||
|
||||
import ast
|
||||
import collections # fmt: skip
|
||||
import dataclasses
|
||||
# fmt: off
|
||||
import os
|
||||
# fmt: on
|
||||
import pathlib
|
||||
|
||||
import re # fmt: skip
|
||||
import secrets
|
||||
|
||||
# fmt: off
|
||||
import sys
|
||||
# fmt: on
|
||||
|
||||
import tempfile
|
||||
import zoneinfo
|
||||
@@ -156,24 +156,6 @@ Please use `--build-option` instead,
|
||||
`--global-option` is reserved to flags like `--verbose` or `--quiet`.
|
||||
"""
|
||||
|
||||
this_will_become_one_line = (
|
||||
"a"
|
||||
"b"
|
||||
"c"
|
||||
)
|
||||
|
||||
this_will_stay_on_three_lines = (
|
||||
"a" # comment
|
||||
"b"
|
||||
"c"
|
||||
)
|
||||
|
||||
this_will_also_become_one_line = ( # comment
|
||||
"a"
|
||||
"b"
|
||||
"c"
|
||||
)
|
||||
|
||||
assert some_var == expected_result, """
|
||||
test
|
||||
"""
|
||||
|
||||
@@ -198,16 +198,6 @@ Please use `--build-option` instead,
|
||||
`--global-option` is reserved to flags like `--verbose` or `--quiet`.
|
||||
"""
|
||||
|
||||
this_will_become_one_line = "abc"
|
||||
|
||||
this_will_stay_on_three_lines = (
|
||||
"a" # comment
|
||||
"b"
|
||||
"c"
|
||||
)
|
||||
|
||||
this_will_also_become_one_line = "abc" # comment
|
||||
|
||||
assert some_var == expected_result, """
|
||||
test
|
||||
"""
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"preview": "enabled"}
|
||||
@@ -0,0 +1,10 @@
|
||||
def foo(
|
||||
a, #type:int
|
||||
b, #type: str
|
||||
c, # type: List[int]
|
||||
d, # type: Dict[int, str]
|
||||
e, # type: ignore
|
||||
f, # type : ignore
|
||||
g, # type : ignore
|
||||
):
|
||||
pass
|
||||
@@ -0,0 +1,10 @@
|
||||
def foo(
|
||||
a, # type: int
|
||||
b, # type: str
|
||||
c, # type: List[int]
|
||||
d, # type: Dict[int, str]
|
||||
e, # type: ignore
|
||||
f, # type : ignore
|
||||
g, # type : ignore
|
||||
):
|
||||
pass
|
||||
@@ -0,0 +1 @@
|
||||
{"preview": "enabled"}
|
||||
16
crates/ruff_python_formatter/resources/test/fixtures/black/cases/remove_parens_from_lhs.py
vendored
Normal file
16
crates/ruff_python_formatter/resources/test/fixtures/black/cases/remove_parens_from_lhs.py
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
# Remove unnecessary parentheses from LHS of assignments
|
||||
|
||||
|
||||
def a():
|
||||
return [1, 2, 3]
|
||||
|
||||
|
||||
# Single variable with unnecessary parentheses
|
||||
(b) = a()[0]
|
||||
|
||||
# Tuple unpacking with unnecessary parentheses
|
||||
(c, *_) = a()
|
||||
|
||||
# These should not be changed - parentheses are necessary
|
||||
(d,) = a() # single-element tuple
|
||||
e = (1 + 2) * 3 # RHS has precedence needs
|
||||
@@ -0,0 +1,16 @@
|
||||
# Remove unnecessary parentheses from LHS of assignments
|
||||
|
||||
|
||||
def a():
|
||||
return [1, 2, 3]
|
||||
|
||||
|
||||
# Single variable with unnecessary parentheses
|
||||
b = a()[0]
|
||||
|
||||
# Tuple unpacking with unnecessary parentheses
|
||||
c, *_ = a()
|
||||
|
||||
# These should not be changed - parentheses are necessary
|
||||
(d,) = a() # single-element tuple
|
||||
e = (1 + 2) * 3 # RHS has precedence needs
|
||||
@@ -334,7 +334,7 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLines {
|
||||
PySourceType::Stub => {
|
||||
write!(f, [empty_line()])
|
||||
}
|
||||
PySourceType::Python | PySourceType::Ipynb => {
|
||||
PySourceType::Python | PySourceType::Ipynb | PySourceType::Markdown => {
|
||||
write!(f, [empty_line(), empty_line()])
|
||||
}
|
||||
},
|
||||
|
||||
@@ -283,7 +283,9 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||
PySourceType::Stub => {
|
||||
empty_line().fmt(f)?;
|
||||
}
|
||||
PySourceType::Python | PySourceType::Ipynb => {
|
||||
PySourceType::Python
|
||||
| PySourceType::Ipynb
|
||||
| PySourceType::Markdown => {
|
||||
write!(f, [empty_line(), empty_line()])?;
|
||||
}
|
||||
},
|
||||
@@ -324,7 +326,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||
PySourceType::Stub => {
|
||||
empty_line().fmt(f)?;
|
||||
}
|
||||
PySourceType::Python | PySourceType::Ipynb => {
|
||||
PySourceType::Python | PySourceType::Ipynb | PySourceType::Markdown => {
|
||||
write!(f, [empty_line(), empty_line()])?;
|
||||
}
|
||||
},
|
||||
@@ -376,7 +378,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||
PySourceType::Stub => {
|
||||
empty_line().fmt(f)?;
|
||||
}
|
||||
PySourceType::Python | PySourceType::Ipynb => {
|
||||
PySourceType::Python | PySourceType::Ipynb | PySourceType::Markdown => {
|
||||
write!(f, [empty_line(), empty_line()])?;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtonoff.py
|
||||
---
|
||||
## Input
|
||||
|
||||
@@ -98,20 +97,22 @@ def example(session):
|
||||
.all()
|
||||
# fmt: on
|
||||
def off_and_on_without_data():
|
||||
"""All comments here are technically on the same prefix.
|
||||
|
||||
The comments between will be formatted. This is a known limitation.
|
||||
"""
|
||||
"""Test that comment-only fmt:off/on blocks preserve formatting."""
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
|
||||
|
||||
#hey, that won't work
|
||||
|
||||
#should not be formatted
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
pass
|
||||
def on_and_off_broken():
|
||||
"""Another known limitation."""
|
||||
def on_and_off_with_comment_only_blocks():
|
||||
"""Test that fmt:off/on works with multiple directives and comment-only blocks."""
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
this=should.not_be.formatted()
|
||||
@@ -120,7 +121,16 @@ def on_and_off_broken():
|
||||
now . considers . multiple . fmt . directives . within . one . prefix
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
# ...but comments still get reformatted even though they should not be
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
|
||||
#should not be formatted
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
def long_lines():
|
||||
if True:
|
||||
@@ -185,6 +195,50 @@ cfg.rule(
|
||||
# fmt: on
|
||||
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5
|
||||
)
|
||||
|
||||
# Test comment-only blocks at file level with various spacing patterns
|
||||
# fmt: off
|
||||
#nospace
|
||||
# twospaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#nospaceatall
|
||||
#extraspaces
|
||||
#evenmorespaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#SBATCH --job-name=test
|
||||
#SBATCH --output=test.out
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#first
|
||||
|
||||
#second
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#!@#$%^&*()
|
||||
#<=>+-*/
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#x=1+2
|
||||
#y = 3
|
||||
#z = 4
|
||||
# fmt: on
|
||||
|
||||
# fmt: off
|
||||
yield 'hello'
|
||||
# No formatting to the end of the file
|
||||
@@ -225,28 +279,16 @@ d={'a':1,
|
||||
# fmt: on
|
||||
goes + here,
|
||||
andhere,
|
||||
@@ -118,8 +119,10 @@
|
||||
"""
|
||||
# fmt: off
|
||||
|
||||
- # hey, that won't work
|
||||
|
||||
+ #hey, that won't work
|
||||
+
|
||||
+
|
||||
# fmt: on
|
||||
pass
|
||||
|
||||
@@ -134,7 +137,7 @@
|
||||
@@ -136,7 +137,7 @@
|
||||
and_=indeed . it is not formatted
|
||||
because . the . handling . inside . generate_ignored_nodes()
|
||||
now . considers . multiple . fmt . directives . within . one . prefix
|
||||
# fmt: on
|
||||
-
|
||||
+ # fmt: on
|
||||
# fmt: off
|
||||
- # ...but comments still get reformatted even though they should not be
|
||||
+ # ...but comments still get reformatted even though they should not be
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
|
||||
|
||||
@@ -174,14 +177,18 @@
|
||||
@@ -187,14 +188,18 @@
|
||||
$
|
||||
""",
|
||||
# fmt: off
|
||||
@@ -387,22 +429,24 @@ def example(session):
|
||||
|
||||
|
||||
def off_and_on_without_data():
|
||||
"""All comments here are technically on the same prefix.
|
||||
|
||||
The comments between will be formatted. This is a known limitation.
|
||||
"""
|
||||
"""Test that comment-only fmt:off/on blocks preserve formatting."""
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
|
||||
#should not be formatted
|
||||
|
||||
#hey, that won't work
|
||||
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
pass
|
||||
|
||||
|
||||
def on_and_off_broken():
|
||||
"""Another known limitation."""
|
||||
def on_and_off_with_comment_only_blocks():
|
||||
"""Test that fmt:off/on works with multiple directives and comment-only blocks."""
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
this=should.not_be.formatted()
|
||||
@@ -411,7 +455,16 @@ def on_and_off_broken():
|
||||
now . considers . multiple . fmt . directives . within . one . prefix
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
# ...but comments still get reformatted even though they should not be
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
|
||||
#should not be formatted
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
|
||||
|
||||
@@ -492,6 +545,50 @@ cfg.rule(
|
||||
# fmt: on
|
||||
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5,
|
||||
)
|
||||
|
||||
# Test comment-only blocks at file level with various spacing patterns
|
||||
# fmt: off
|
||||
#nospace
|
||||
# twospaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#nospaceatall
|
||||
#extraspaces
|
||||
#evenmorespaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#SBATCH --job-name=test
|
||||
#SBATCH --output=test.out
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#first
|
||||
|
||||
#second
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#!@#$%^&*()
|
||||
#<=>+-*/
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#x=1+2
|
||||
#y = 3
|
||||
#z = 4
|
||||
# fmt: on
|
||||
|
||||
# fmt: off
|
||||
yield 'hello'
|
||||
# No formatting to the end of the file
|
||||
@@ -617,29 +714,42 @@ def example(session):
|
||||
|
||||
|
||||
def off_and_on_without_data():
|
||||
"""All comments here are technically on the same prefix.
|
||||
|
||||
The comments between will be formatted. This is a known limitation.
|
||||
"""
|
||||
"""Test that comment-only fmt:off/on blocks preserve formatting."""
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
|
||||
# hey, that won't work
|
||||
#should not be formatted
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
pass
|
||||
|
||||
|
||||
def on_and_off_broken():
|
||||
"""Another known limitation."""
|
||||
def on_and_off_with_comment_only_blocks():
|
||||
"""Test that fmt:off/on works with multiple directives and comment-only blocks."""
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
this=should.not_be.formatted()
|
||||
and_=indeed . it is not formatted
|
||||
because . the . handling . inside . generate_ignored_nodes()
|
||||
now . considers . multiple . fmt . directives . within . one . prefix
|
||||
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
# ...but comments still get reformatted even though they should not be
|
||||
|
||||
#should not be formatted
|
||||
|
||||
# fmt: on
|
||||
# fmt: off
|
||||
#should not be formatted
|
||||
#should not be formatted #also should not be formatted
|
||||
# fmt: on
|
||||
|
||||
|
||||
@@ -716,6 +826,50 @@ cfg.rule(
|
||||
# fmt: on
|
||||
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5,
|
||||
)
|
||||
|
||||
# Test comment-only blocks at file level with various spacing patterns
|
||||
# fmt: off
|
||||
#nospace
|
||||
# twospaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#nospaceatall
|
||||
#extraspaces
|
||||
#evenmorespaces
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#SBATCH --job-name=test
|
||||
#SBATCH --output=test.out
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#first
|
||||
|
||||
#second
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#!@#$%^&*()
|
||||
#<=>+-*/
|
||||
# fmt: on
|
||||
|
||||
|
||||
# fmt: off
|
||||
#x=1+2
|
||||
#y = 3
|
||||
#z = 4
|
||||
# fmt: on
|
||||
|
||||
# fmt: off
|
||||
yield 'hello'
|
||||
# No formatting to the end of the file
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmtskip10.py
|
||||
---
|
||||
## Input
|
||||
|
||||
@@ -8,11 +7,24 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/fmt
|
||||
def foo(): return "mock" # fmt: skip
|
||||
if True: print("yay") # fmt: skip
|
||||
for i in range(10): print(i) # fmt: skip
|
||||
if True: print("this"); print("that") # fmt: skip
|
||||
while True: print("loop"); break # fmt: skip
|
||||
for x in [1, 2]: print(x); print("done") # fmt: skip
|
||||
def f(x: int): return x # fmt: skip
|
||||
|
||||
j = 1 # fmt: skip
|
||||
while j < 10: j += 1 # fmt: skip
|
||||
|
||||
b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip
|
||||
|
||||
v = (
|
||||
foo_dict # fmt: skip
|
||||
.setdefault("a", {})
|
||||
.setdefault("b", {})
|
||||
.setdefault("c", {})
|
||||
.setdefault("d", {})
|
||||
.setdefault("e", {})
|
||||
)
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
@@ -20,19 +32,30 @@ b = [c for c in "A very long string that would normally generate some kind of co
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,8 +1,10 @@
|
||||
@@ -1,15 +1,20 @@
|
||||
def foo(): return "mock" # fmt: skip
|
||||
+
|
||||
+
|
||||
if True: print("yay") # fmt: skip
|
||||
for i in range(10): print(i) # fmt: skip
|
||||
if True: print("this"); print("that") # fmt: skip
|
||||
while True: print("loop"); break # fmt: skip
|
||||
for x in [1, 2]: print(x); print("done") # fmt: skip
|
||||
-def f(x: int): return x # fmt: skip
|
||||
|
||||
-j = 1 # fmt: skip
|
||||
+
|
||||
+def f(x: int): return x # fmt: skip
|
||||
+
|
||||
+
|
||||
+j = 1 # fmt: skip
|
||||
while j < 10: j += 1 # fmt: skip
|
||||
|
||||
-b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip
|
||||
+b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip
|
||||
|
||||
v = (
|
||||
foo_dict # fmt: skip
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
@@ -43,11 +66,27 @@ def foo(): return "mock" # fmt: skip
|
||||
|
||||
if True: print("yay") # fmt: skip
|
||||
for i in range(10): print(i) # fmt: skip
|
||||
if True: print("this"); print("that") # fmt: skip
|
||||
while True: print("loop"); break # fmt: skip
|
||||
for x in [1, 2]: print(x); print("done") # fmt: skip
|
||||
|
||||
|
||||
def f(x: int): return x # fmt: skip
|
||||
|
||||
|
||||
j = 1 # fmt: skip
|
||||
while j < 10: j += 1 # fmt: skip
|
||||
|
||||
b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip
|
||||
|
||||
v = (
|
||||
foo_dict # fmt: skip
|
||||
.setdefault("a", {})
|
||||
.setdefault("b", {})
|
||||
.setdefault("c", {})
|
||||
.setdefault("d", {})
|
||||
.setdefault("e", {})
|
||||
)
|
||||
```
|
||||
|
||||
## Black Output
|
||||
@@ -56,9 +95,22 @@ b = [c for c in "A very long string that would normally generate some kind of co
|
||||
def foo(): return "mock" # fmt: skip
|
||||
if True: print("yay") # fmt: skip
|
||||
for i in range(10): print(i) # fmt: skip
|
||||
if True: print("this"); print("that") # fmt: skip
|
||||
while True: print("loop"); break # fmt: skip
|
||||
for x in [1, 2]: print(x); print("done") # fmt: skip
|
||||
def f(x: int): return x # fmt: skip
|
||||
|
||||
j = 1 # fmt: skip
|
||||
while j < 10: j += 1 # fmt: skip
|
||||
|
||||
b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip
|
||||
|
||||
v = (
|
||||
foo_dict # fmt: skip
|
||||
.setdefault("a", {})
|
||||
.setdefault("b", {})
|
||||
.setdefault("c", {})
|
||||
.setdefault("d", {})
|
||||
.setdefault("e", {})
|
||||
)
|
||||
```
|
||||
|
||||
@@ -0,0 +1,321 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
def foo():
|
||||
pass
|
||||
|
||||
|
||||
# comment 1 # fmt: skip
|
||||
# comment 2
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3,
|
||||
4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3,
|
||||
4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3,
|
||||
4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3,
|
||||
4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
if False:
|
||||
# fmt: off # some other comment
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -46,8 +46,7 @@
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
- (3,
|
||||
- 4),
|
||||
+ (3, 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
@@ -55,8 +54,7 @@
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
- (3,
|
||||
- 4),
|
||||
+ (3, 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
@@ -65,8 +63,7 @@
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
- (3,
|
||||
- 4),
|
||||
+ (3, 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
@@ -75,8 +72,7 @@
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
- (3,
|
||||
- 4),
|
||||
+ (3, 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
def foo():
|
||||
pass
|
||||
|
||||
|
||||
# comment 1 # fmt: skip
|
||||
# comment 2
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3, 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3, 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3, 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3, 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
if False:
|
||||
# fmt: off # some other comment
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
def foo():
|
||||
pass
|
||||
|
||||
|
||||
# comment 1 # fmt: skip
|
||||
# comment 2
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
# (3,
|
||||
# 4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3,
|
||||
4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# # fmt: off
|
||||
(3,
|
||||
4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3,
|
||||
4),
|
||||
# # fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
[
|
||||
(1, 2),
|
||||
# fmt: off
|
||||
(3,
|
||||
4),
|
||||
# fmt: on
|
||||
(5, 6),
|
||||
]
|
||||
|
||||
|
||||
if False:
|
||||
# fmt: off # some other comment
|
||||
pass
|
||||
```
|
||||
@@ -0,0 +1,59 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
with open("file.txt") as f: content = f.read() # fmt: skip
|
||||
|
||||
# Ideally, only the last line would be ignored
|
||||
# But ignoring only part of the asexpr_test causes a parse error
|
||||
# Same with ignoring the asexpr_test without also ignoring the entire with_stmt
|
||||
with open (
|
||||
"file.txt" ,
|
||||
) as f: content = f.read() # fmt: skip
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,8 +1,8 @@
|
||||
-with open("file.txt") as f: content = f.read() # fmt: skip
|
||||
+with open("file.txt") as f: content = f.read() # fmt: skip
|
||||
|
||||
# Ideally, only the last line would be ignored
|
||||
# But ignoring only part of the asexpr_test causes a parse error
|
||||
# Same with ignoring the asexpr_test without also ignoring the entire with_stmt
|
||||
with open (
|
||||
"file.txt" ,
|
||||
-) as f: content = f.read() # fmt: skip
|
||||
+) as f: content = f.read() # fmt: skip
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
with open("file.txt") as f: content = f.read() # fmt: skip
|
||||
|
||||
# Ideally, only the last line would be ignored
|
||||
# But ignoring only part of the asexpr_test causes a parse error
|
||||
# Same with ignoring the asexpr_test without also ignoring the entire with_stmt
|
||||
with open (
|
||||
"file.txt" ,
|
||||
) as f: content = f.read() # fmt: skip
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
with open("file.txt") as f: content = f.read() # fmt: skip
|
||||
|
||||
# Ideally, only the last line would be ignored
|
||||
# But ignoring only part of the asexpr_test causes a parse error
|
||||
# Same with ignoring the asexpr_test without also ignoring the entire with_stmt
|
||||
with open (
|
||||
"file.txt" ,
|
||||
) as f: content = f.read() # fmt: skip
|
||||
```
|
||||
@@ -0,0 +1,149 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
t = (
|
||||
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
|
||||
t = (
|
||||
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
{"foo": "bar",},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{"foo": "bar",},
|
||||
)
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,5 +1,9 @@
|
||||
t = (
|
||||
- {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
+ {
|
||||
+ "foo": "very long string",
|
||||
+ "bar": "another very long string",
|
||||
+ "baz": "we should run out of space by now",
|
||||
+ }, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
@@ -14,8 +18,12 @@
|
||||
|
||||
|
||||
t = (
|
||||
- {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
{
|
||||
+ "foo": "very long string",
|
||||
+ "bar": "another very long string",
|
||||
+ "baz": "we should run out of space by now",
|
||||
+ }, # fmt: skip
|
||||
+ {
|
||||
"foo": "bar",
|
||||
},
|
||||
)
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{
|
||||
"foo": "bar",
|
||||
},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{
|
||||
"foo": "bar",
|
||||
},
|
||||
)
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
t = (
|
||||
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{"foo": "bar"},
|
||||
)
|
||||
|
||||
|
||||
t = (
|
||||
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
|
||||
{
|
||||
"foo": "bar",
|
||||
},
|
||||
)
|
||||
|
||||
t = (
|
||||
{
|
||||
"foo": "very long string",
|
||||
"bar": "another very long string",
|
||||
"baz": "we should run out of space by now",
|
||||
}, # fmt: skip
|
||||
{
|
||||
"foo": "bar",
|
||||
},
|
||||
)
|
||||
```
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
a = "this is some code"
|
||||
b = 5 # fmt:skip
|
||||
c = 9 #fmt: skip
|
||||
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,4 +1,4 @@
|
||||
a = "this is some code"
|
||||
-b = 5 # fmt:skip
|
||||
-c = 9 #fmt: skip
|
||||
+b = 5 # fmt:skip
|
||||
+c = 9 # fmt: skip
|
||||
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
a = "this is some code"
|
||||
b = 5 # fmt:skip
|
||||
c = 9 # fmt: skip
|
||||
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
a = "this is some code"
|
||||
b = 5 # fmt:skip
|
||||
c = 9 #fmt: skip
|
||||
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip
|
||||
```
|
||||
@@ -0,0 +1,98 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
# Multiple fmt: skip in multi-part if-clause
|
||||
class ClassWithALongName:
|
||||
Constant1 = 1
|
||||
Constant2 = 2
|
||||
Constant3 = 3
|
||||
|
||||
|
||||
def test():
|
||||
if (
|
||||
"cond1" == "cond1"
|
||||
and "cond2" == "cond2"
|
||||
and 1 in (
|
||||
ClassWithALongName.Constant1,
|
||||
ClassWithALongName.Constant2,
|
||||
ClassWithALongName.Constant3, # fmt: skip
|
||||
) # fmt: skip
|
||||
):
|
||||
return True
|
||||
return False
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -9,11 +9,12 @@
|
||||
if (
|
||||
"cond1" == "cond1"
|
||||
and "cond2" == "cond2"
|
||||
- and 1 in (
|
||||
+ and 1
|
||||
+ in (
|
||||
ClassWithALongName.Constant1,
|
||||
ClassWithALongName.Constant2,
|
||||
- ClassWithALongName.Constant3, # fmt: skip
|
||||
- ) # fmt: skip
|
||||
+ ClassWithALongName.Constant3, # fmt: skip
|
||||
+ ) # fmt: skip
|
||||
):
|
||||
return True
|
||||
return False
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
# Multiple fmt: skip in multi-part if-clause
|
||||
class ClassWithALongName:
|
||||
Constant1 = 1
|
||||
Constant2 = 2
|
||||
Constant3 = 3
|
||||
|
||||
|
||||
def test():
|
||||
if (
|
||||
"cond1" == "cond1"
|
||||
and "cond2" == "cond2"
|
||||
and 1
|
||||
in (
|
||||
ClassWithALongName.Constant1,
|
||||
ClassWithALongName.Constant2,
|
||||
ClassWithALongName.Constant3, # fmt: skip
|
||||
) # fmt: skip
|
||||
):
|
||||
return True
|
||||
return False
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
# Multiple fmt: skip in multi-part if-clause
|
||||
class ClassWithALongName:
|
||||
Constant1 = 1
|
||||
Constant2 = 2
|
||||
Constant3 = 3
|
||||
|
||||
|
||||
def test():
|
||||
if (
|
||||
"cond1" == "cond1"
|
||||
and "cond2" == "cond2"
|
||||
and 1 in (
|
||||
ClassWithALongName.Constant1,
|
||||
ClassWithALongName.Constant2,
|
||||
ClassWithALongName.Constant3, # fmt: skip
|
||||
) # fmt: skip
|
||||
):
|
||||
return True
|
||||
return False
|
||||
```
|
||||
@@ -0,0 +1,148 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
# Multiple fmt: skip on string literals
|
||||
a = (
|
||||
"this should " # fmt: skip
|
||||
"be fine"
|
||||
)
|
||||
|
||||
b = (
|
||||
"this is " # fmt: skip
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
c = (
|
||||
"and neither " # fmt: skip
|
||||
"is this " # fmt: skip
|
||||
"working"
|
||||
)
|
||||
|
||||
d = (
|
||||
"nor "
|
||||
"is this " # fmt: skip
|
||||
"working" # fmt: skip
|
||||
)
|
||||
|
||||
e = (
|
||||
"and this " # fmt: skip
|
||||
"is definitely "
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
# Dictionary entries with fmt: skip (covers issue with long lines)
|
||||
hotkeys = {
|
||||
"editor:swap-line-down": [{"key": "ArrowDown", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
"editor:swap-line-up": [{"key": "ArrowUp", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
"editor:toggle-source": [{"key": "S", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
}
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -29,7 +29,11 @@
|
||||
|
||||
# Dictionary entries with fmt: skip (covers issue with long lines)
|
||||
hotkeys = {
|
||||
- "editor:swap-line-down": [{"key": "ArrowDown", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
- "editor:swap-line-up": [{"key": "ArrowUp", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
- "editor:toggle-source": [{"key": "S", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
+ "editor:swap-line-down": [
|
||||
+ {"key": "ArrowDown", "modifiers": ["Alt", "Mod"]}
|
||||
+ ], # fmt: skip
|
||||
+ "editor:swap-line-up": [
|
||||
+ {"key": "ArrowUp", "modifiers": ["Alt", "Mod"]}
|
||||
+ ], # fmt: skip
|
||||
+ "editor:toggle-source": [{"key": "S", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
}
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
# Multiple fmt: skip on string literals
|
||||
a = (
|
||||
"this should " # fmt: skip
|
||||
"be fine"
|
||||
)
|
||||
|
||||
b = (
|
||||
"this is " # fmt: skip
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
c = (
|
||||
"and neither " # fmt: skip
|
||||
"is this " # fmt: skip
|
||||
"working"
|
||||
)
|
||||
|
||||
d = (
|
||||
"nor "
|
||||
"is this " # fmt: skip
|
||||
"working" # fmt: skip
|
||||
)
|
||||
|
||||
e = (
|
||||
"and this " # fmt: skip
|
||||
"is definitely "
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
# Dictionary entries with fmt: skip (covers issue with long lines)
|
||||
hotkeys = {
|
||||
"editor:swap-line-down": [
|
||||
{"key": "ArrowDown", "modifiers": ["Alt", "Mod"]}
|
||||
], # fmt: skip
|
||||
"editor:swap-line-up": [
|
||||
{"key": "ArrowUp", "modifiers": ["Alt", "Mod"]}
|
||||
], # fmt: skip
|
||||
"editor:toggle-source": [{"key": "S", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
}
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
# Multiple fmt: skip on string literals
|
||||
a = (
|
||||
"this should " # fmt: skip
|
||||
"be fine"
|
||||
)
|
||||
|
||||
b = (
|
||||
"this is " # fmt: skip
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
c = (
|
||||
"and neither " # fmt: skip
|
||||
"is this " # fmt: skip
|
||||
"working"
|
||||
)
|
||||
|
||||
d = (
|
||||
"nor "
|
||||
"is this " # fmt: skip
|
||||
"working" # fmt: skip
|
||||
)
|
||||
|
||||
e = (
|
||||
"and this " # fmt: skip
|
||||
"is definitely "
|
||||
"not working" # fmt: skip
|
||||
)
|
||||
|
||||
# Dictionary entries with fmt: skip (covers issue with long lines)
|
||||
hotkeys = {
|
||||
"editor:swap-line-down": [{"key": "ArrowDown", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
"editor:swap-line-up": [{"key": "ArrowUp", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
"editor:toggle-source": [{"key": "S", "modifiers": ["Alt", "Mod"]}], # fmt: skip
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,188 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
x = t"foo"
|
||||
x = t'foo {{ {2 + 2}bar {{ baz'
|
||||
|
||||
x = t"foo {f'abc'} bar"
|
||||
|
||||
x = t"""foo {{ a
|
||||
foo {2 + 2}bar {{ baz
|
||||
|
||||
x = f"foo {{ {
|
||||
2 + 2 # comment
|
||||
}bar"
|
||||
|
||||
{{ baz
|
||||
|
||||
}} buzz
|
||||
|
||||
{print("abc" + "def"
|
||||
)}
|
||||
abc"""
|
||||
|
||||
t'{(abc:=10)}'
|
||||
|
||||
t'''This is a really long string, but just make sure that you reflow tstrings {
|
||||
2+2:d
|
||||
}'''
|
||||
t'This is a really long string, but just make sure that you reflow tstrings correctly {2+2:d}'
|
||||
|
||||
t"{ 2 + 2 = }"
|
||||
|
||||
t'{
|
||||
X
|
||||
!r
|
||||
}'
|
||||
|
||||
tr'\{{\}}'
|
||||
|
||||
t'''
|
||||
WITH {f'''
|
||||
{1}_cte AS ()'''}
|
||||
'''
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -7,34 +7,32 @@
|
||||
foo {2 + 2}bar {{ baz
|
||||
|
||||
x = f"foo {{ {
|
||||
- 2 + 2 # comment
|
||||
- }bar"
|
||||
+ 2 + 2 # comment
|
||||
+}bar"
|
||||
|
||||
{{ baz
|
||||
|
||||
}} buzz
|
||||
|
||||
- {print("abc" + "def"
|
||||
-)}
|
||||
+ {print("abc" + "def")}
|
||||
abc"""
|
||||
|
||||
-t"{(abc:=10)}"
|
||||
+t"{(abc := 10)}"
|
||||
|
||||
t"""This is a really long string, but just make sure that you reflow tstrings {
|
||||
- 2+2:d
|
||||
+ 2 + 2:d
|
||||
}"""
|
||||
-t"This is a really long string, but just make sure that you reflow tstrings correctly {2+2:d}"
|
||||
+t"This is a really long string, but just make sure that you reflow tstrings correctly {2 + 2:d}"
|
||||
|
||||
t"{ 2 + 2 = }"
|
||||
|
||||
-t"{
|
||||
-X
|
||||
-!r
|
||||
-}"
|
||||
+t"{X!r}"
|
||||
|
||||
rt"\{{\}}"
|
||||
|
||||
t"""
|
||||
- WITH {f'''
|
||||
- {1}_cte AS ()'''}
|
||||
+ WITH {
|
||||
+ f'''
|
||||
+ {1}_cte AS ()'''
|
||||
+}
|
||||
"""
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
x = t"foo"
|
||||
x = t"foo {{ {2 + 2}bar {{ baz"
|
||||
|
||||
x = t"foo {f'abc'} bar"
|
||||
|
||||
x = t"""foo {{ a
|
||||
foo {2 + 2}bar {{ baz
|
||||
|
||||
x = f"foo {{ {
|
||||
2 + 2 # comment
|
||||
}bar"
|
||||
|
||||
{{ baz
|
||||
|
||||
}} buzz
|
||||
|
||||
{print("abc" + "def")}
|
||||
abc"""
|
||||
|
||||
t"{(abc := 10)}"
|
||||
|
||||
t"""This is a really long string, but just make sure that you reflow tstrings {
|
||||
2 + 2:d
|
||||
}"""
|
||||
t"This is a really long string, but just make sure that you reflow tstrings correctly {2 + 2:d}"
|
||||
|
||||
t"{ 2 + 2 = }"
|
||||
|
||||
t"{X!r}"
|
||||
|
||||
rt"\{{\}}"
|
||||
|
||||
t"""
|
||||
WITH {
|
||||
f'''
|
||||
{1}_cte AS ()'''
|
||||
}
|
||||
"""
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
x = t"foo"
|
||||
x = t"foo {{ {2 + 2}bar {{ baz"
|
||||
|
||||
x = t"foo {f'abc'} bar"
|
||||
|
||||
x = t"""foo {{ a
|
||||
foo {2 + 2}bar {{ baz
|
||||
|
||||
x = f"foo {{ {
|
||||
2 + 2 # comment
|
||||
}bar"
|
||||
|
||||
{{ baz
|
||||
|
||||
}} buzz
|
||||
|
||||
{print("abc" + "def"
|
||||
)}
|
||||
abc"""
|
||||
|
||||
t"{(abc:=10)}"
|
||||
|
||||
t"""This is a really long string, but just make sure that you reflow tstrings {
|
||||
2+2:d
|
||||
}"""
|
||||
t"This is a really long string, but just make sure that you reflow tstrings correctly {2+2:d}"
|
||||
|
||||
t"{ 2 + 2 = }"
|
||||
|
||||
t"{
|
||||
X
|
||||
!r
|
||||
}"
|
||||
|
||||
rt"\{{\}}"
|
||||
|
||||
t"""
|
||||
WITH {f'''
|
||||
{1}_cte AS ()'''}
|
||||
"""
|
||||
```
|
||||
@@ -0,0 +1,90 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
# Regression test for https://github.com/psf/black/issues/3438
|
||||
|
||||
import ast
|
||||
import collections # fmt: skip
|
||||
import dataclasses
|
||||
# fmt: off
|
||||
import os
|
||||
# fmt: on
|
||||
import pathlib
|
||||
|
||||
import re # fmt: skip
|
||||
import secrets
|
||||
|
||||
# fmt: off
|
||||
import sys
|
||||
# fmt: on
|
||||
|
||||
import tempfile
|
||||
import zoneinfo
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -3,6 +3,7 @@
|
||||
import ast
|
||||
import collections # fmt: skip
|
||||
import dataclasses
|
||||
+
|
||||
# fmt: off
|
||||
import os
|
||||
# fmt: on
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
# Regression test for https://github.com/psf/black/issues/3438
|
||||
|
||||
import ast
|
||||
import collections # fmt: skip
|
||||
import dataclasses
|
||||
|
||||
# fmt: off
|
||||
import os
|
||||
# fmt: on
|
||||
import pathlib
|
||||
|
||||
import re # fmt: skip
|
||||
import secrets
|
||||
|
||||
# fmt: off
|
||||
import sys
|
||||
# fmt: on
|
||||
|
||||
import tempfile
|
||||
import zoneinfo
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
# Regression test for https://github.com/psf/black/issues/3438
|
||||
|
||||
import ast
|
||||
import collections # fmt: skip
|
||||
import dataclasses
|
||||
# fmt: off
|
||||
import os
|
||||
# fmt: on
|
||||
import pathlib
|
||||
|
||||
import re # fmt: skip
|
||||
import secrets
|
||||
|
||||
# fmt: off
|
||||
import sys
|
||||
# fmt: on
|
||||
|
||||
import tempfile
|
||||
import zoneinfo
|
||||
```
|
||||
@@ -1,6 +1,5 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/preview_multiline_strings.py
|
||||
---
|
||||
## Input
|
||||
|
||||
@@ -163,24 +162,6 @@ Please use `--build-option` instead,
|
||||
`--global-option` is reserved to flags like `--verbose` or `--quiet`.
|
||||
"""
|
||||
|
||||
this_will_become_one_line = (
|
||||
"a"
|
||||
"b"
|
||||
"c"
|
||||
)
|
||||
|
||||
this_will_stay_on_three_lines = (
|
||||
"a" # comment
|
||||
"b"
|
||||
"c"
|
||||
)
|
||||
|
||||
this_will_also_become_one_line = ( # comment
|
||||
"a"
|
||||
"b"
|
||||
"c"
|
||||
)
|
||||
|
||||
assert some_var == expected_result, """
|
||||
test
|
||||
"""
|
||||
@@ -421,18 +402,7 @@ a = b if """
|
||||
[
|
||||
"""cow
|
||||
moos""",
|
||||
@@ -206,7 +245,9 @@
|
||||
"c"
|
||||
)
|
||||
|
||||
-this_will_also_become_one_line = "abc" # comment
|
||||
+this_will_also_become_one_line = ( # comment
|
||||
+ "abc"
|
||||
+)
|
||||
|
||||
assert some_var == expected_result, """
|
||||
test
|
||||
@@ -224,10 +265,8 @@
|
||||
@@ -214,10 +253,8 @@
|
||||
"""Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx
|
||||
xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
|
||||
),
|
||||
@@ -445,7 +415,7 @@ a = b if """
|
||||
},
|
||||
}
|
||||
|
||||
@@ -246,14 +285,12 @@
|
||||
@@ -236,14 +273,12 @@
|
||||
a
|
||||
a"""
|
||||
),
|
||||
@@ -706,18 +676,6 @@ Please use `--build-option` instead,
|
||||
`--global-option` is reserved to flags like `--verbose` or `--quiet`.
|
||||
"""
|
||||
|
||||
this_will_become_one_line = "abc"
|
||||
|
||||
this_will_stay_on_three_lines = (
|
||||
"a" # comment
|
||||
"b"
|
||||
"c"
|
||||
)
|
||||
|
||||
this_will_also_become_one_line = ( # comment
|
||||
"abc"
|
||||
)
|
||||
|
||||
assert some_var == expected_result, """
|
||||
test
|
||||
"""
|
||||
@@ -1028,16 +986,6 @@ Please use `--build-option` instead,
|
||||
`--global-option` is reserved to flags like `--verbose` or `--quiet`.
|
||||
"""
|
||||
|
||||
this_will_become_one_line = "abc"
|
||||
|
||||
this_will_stay_on_three_lines = (
|
||||
"a" # comment
|
||||
"b"
|
||||
"c"
|
||||
)
|
||||
|
||||
this_will_also_become_one_line = "abc" # comment
|
||||
|
||||
assert some_var == expected_result, """
|
||||
test
|
||||
"""
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
def foo(
|
||||
a, #type:int
|
||||
b, #type: str
|
||||
c, # type: List[int]
|
||||
d, # type: Dict[int, str]
|
||||
e, # type: ignore
|
||||
f, # type : ignore
|
||||
g, # type : ignore
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,9 +1,9 @@
|
||||
def foo(
|
||||
- a, # type: int
|
||||
+ a, # type:int
|
||||
b, # type: str
|
||||
c, # type: List[int]
|
||||
- d, # type: Dict[int, str]
|
||||
- e, # type: ignore
|
||||
+ d, # type: Dict[int, str]
|
||||
+ e, # type: ignore
|
||||
f, # type : ignore
|
||||
g, # type : ignore
|
||||
):
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
def foo(
|
||||
a, # type:int
|
||||
b, # type: str
|
||||
c, # type: List[int]
|
||||
d, # type: Dict[int, str]
|
||||
e, # type: ignore
|
||||
f, # type : ignore
|
||||
g, # type : ignore
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
def foo(
|
||||
a, # type: int
|
||||
b, # type: str
|
||||
c, # type: List[int]
|
||||
d, # type: Dict[int, str]
|
||||
e, # type: ignore
|
||||
f, # type : ignore
|
||||
g, # type : ignore
|
||||
):
|
||||
pass
|
||||
```
|
||||
@@ -0,0 +1,85 @@
|
||||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
---
|
||||
## Input
|
||||
|
||||
```python
|
||||
# Remove unnecessary parentheses from LHS of assignments
|
||||
|
||||
|
||||
def a():
|
||||
return [1, 2, 3]
|
||||
|
||||
|
||||
# Single variable with unnecessary parentheses
|
||||
(b) = a()[0]
|
||||
|
||||
# Tuple unpacking with unnecessary parentheses
|
||||
(c, *_) = a()
|
||||
|
||||
# These should not be changed - parentheses are necessary
|
||||
(d,) = a() # single-element tuple
|
||||
e = (1 + 2) * 3 # RHS has precedence needs
|
||||
```
|
||||
|
||||
## Black Differences
|
||||
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -6,10 +6,10 @@
|
||||
|
||||
|
||||
# Single variable with unnecessary parentheses
|
||||
-b = a()[0]
|
||||
+(b) = a()[0]
|
||||
|
||||
# Tuple unpacking with unnecessary parentheses
|
||||
-c, *_ = a()
|
||||
+(c, *_) = a()
|
||||
|
||||
# These should not be changed - parentheses are necessary
|
||||
(d,) = a() # single-element tuple
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```python
|
||||
# Remove unnecessary parentheses from LHS of assignments
|
||||
|
||||
|
||||
def a():
|
||||
return [1, 2, 3]
|
||||
|
||||
|
||||
# Single variable with unnecessary parentheses
|
||||
(b) = a()[0]
|
||||
|
||||
# Tuple unpacking with unnecessary parentheses
|
||||
(c, *_) = a()
|
||||
|
||||
# These should not be changed - parentheses are necessary
|
||||
(d,) = a() # single-element tuple
|
||||
e = (1 + 2) * 3 # RHS has precedence needs
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
||||
```python
|
||||
# Remove unnecessary parentheses from LHS of assignments
|
||||
|
||||
|
||||
def a():
|
||||
return [1, 2, 3]
|
||||
|
||||
|
||||
# Single variable with unnecessary parentheses
|
||||
b = a()[0]
|
||||
|
||||
# Tuple unpacking with unnecessary parentheses
|
||||
c, *_ = a()
|
||||
|
||||
# These should not be changed - parentheses are necessary
|
||||
(d,) = a() # single-element tuple
|
||||
e = (1 + 2) * 3 # RHS has precedence needs
|
||||
```
|
||||
@@ -525,7 +525,7 @@ pub trait AsMode {
|
||||
impl AsMode for PySourceType {
|
||||
fn as_mode(&self) -> Mode {
|
||||
match self {
|
||||
PySourceType::Python | PySourceType::Stub => Mode::Module,
|
||||
PySourceType::Python | PySourceType::Stub | PySourceType::Markdown => Mode::Module,
|
||||
PySourceType::Ipynb => Mode::Ipython,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ruff_wasm"
|
||||
version = "0.14.10"
|
||||
version = "0.14.11"
|
||||
publish = false
|
||||
authors = { workspace = true }
|
||||
edition = { workspace = true }
|
||||
|
||||
@@ -7,7 +7,6 @@ use std::collections::BTreeMap;
|
||||
use std::env::VarError;
|
||||
use std::num::{NonZeroU8, NonZeroU16};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str::FromStr;
|
||||
|
||||
use anyhow::{Context, Result, anyhow};
|
||||
use glob::{GlobError, Paths, PatternError, glob};
|
||||
@@ -36,8 +35,7 @@ use ruff_linter::settings::{
|
||||
DEFAULT_SELECTORS, DUMMY_VARIABLE_RGX, LinterSettings, TASK_TAGS, TargetVersion,
|
||||
};
|
||||
use ruff_linter::{
|
||||
RUFF_PKG_VERSION, RuleSelector, fs, warn_user_once, warn_user_once_by_id,
|
||||
warn_user_once_by_message,
|
||||
RuleSelector, fs, warn_user_once, warn_user_once_by_id, warn_user_once_by_message,
|
||||
};
|
||||
use ruff_python_ast as ast;
|
||||
use ruff_python_formatter::{
|
||||
@@ -53,6 +51,7 @@ use crate::options::{
|
||||
Flake8UnusedArgumentsOptions, FormatOptions, IsortOptions, LintCommonOptions, LintOptions,
|
||||
McCabeOptions, Options, Pep8NamingOptions, PyUpgradeOptions, PycodestyleOptions,
|
||||
PydoclintOptions, PydocstyleOptions, PyflakesOptions, PylintOptions, RuffOptions,
|
||||
validate_required_version,
|
||||
};
|
||||
use crate::settings::{
|
||||
EXCLUDE, FileResolverSettings, FormatterSettings, INCLUDE, INCLUDE_PREVIEW, LineEnding,
|
||||
@@ -155,13 +154,7 @@ pub struct Configuration {
|
||||
impl Configuration {
|
||||
pub fn into_settings(self, project_root: &Path) -> Result<Settings> {
|
||||
if let Some(required_version) = &self.required_version {
|
||||
let ruff_pkg_version = pep440_rs::Version::from_str(RUFF_PKG_VERSION)
|
||||
.expect("RUFF_PKG_VERSION is not a valid PEP 440 version specifier");
|
||||
if !required_version.contains(&ruff_pkg_version) {
|
||||
return Err(anyhow!(
|
||||
"Required version `{required_version}` does not match the running version `{RUFF_PKG_VERSION}`"
|
||||
));
|
||||
}
|
||||
validate_required_version(required_version)?;
|
||||
}
|
||||
|
||||
let linter_target_version = TargetVersion(self.target_version);
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
use anyhow::Result;
|
||||
use regex::Regex;
|
||||
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
|
||||
use serde::de::{self};
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use strum::IntoEnumIterator;
|
||||
use unicode_normalization::UnicodeNormalization;
|
||||
|
||||
use crate::settings::LineEnding;
|
||||
use ruff_formatter::IndentStyle;
|
||||
use ruff_graph::Direction;
|
||||
use ruff_linter::RUFF_PKG_VERSION;
|
||||
|
||||
use ruff_linter::line_width::{IndentWidth, LineLength};
|
||||
use ruff_linter::rules::flake8_import_conventions::settings::BannedAliases;
|
||||
use ruff_linter::rules::flake8_pytest_style::settings::SettingsError;
|
||||
@@ -556,6 +560,17 @@ pub struct LintOptions {
|
||||
pub future_annotations: Option<bool>,
|
||||
}
|
||||
|
||||
pub fn validate_required_version(required_version: &RequiredVersion) -> anyhow::Result<()> {
|
||||
let ruff_pkg_version = pep440_rs::Version::from_str(RUFF_PKG_VERSION)
|
||||
.expect("RUFF_PKG_VERSION is not a valid PEP 440 version specifier");
|
||||
if !required_version.contains(&ruff_pkg_version) {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Required version `{required_version}` does not match the running version `{RUFF_PKG_VERSION}`"
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Newtype wrapper for [`LintCommonOptions`] that allows customizing the JSON schema and omitting the fields from the [`OptionsMetadata`].
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
|
||||
@@ -5,12 +5,13 @@ use std::path::{Path, PathBuf};
|
||||
use anyhow::{Context, Result};
|
||||
use log::debug;
|
||||
use pep440_rs::{Operator, Version, VersionSpecifiers};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use strum::IntoEnumIterator;
|
||||
|
||||
use ruff_linter::settings::types::PythonVersion;
|
||||
use ruff_linter::settings::types::{PythonVersion, RequiredVersion};
|
||||
|
||||
use crate::options::Options;
|
||||
use crate::options::{Options, validate_required_version};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
struct Tools {
|
||||
@@ -40,20 +41,38 @@ impl Pyproject {
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a `ruff.toml` file.
|
||||
fn parse_ruff_toml<P: AsRef<Path>>(path: P) -> Result<Options> {
|
||||
fn parse_toml<P: AsRef<Path>, T: DeserializeOwned>(path: P, table_path: &[&str]) -> Result<T> {
|
||||
let path = path.as_ref();
|
||||
let contents = std::fs::read_to_string(path)
|
||||
.with_context(|| format!("Failed to read {}", path.display()))?;
|
||||
toml::from_str(&contents).with_context(|| format!("Failed to parse {}", path.display()))
|
||||
|
||||
// Parse the TOML document once into a spanned representation so we can:
|
||||
// - Inspect `required-version` without triggering strict deserialization errors.
|
||||
// - Deserialize with precise spans (line/column and excerpt) on errors.
|
||||
let root = toml::de::DeTable::parse(&contents)
|
||||
.with_context(|| format!("Failed to parse {}", path.display()))?;
|
||||
|
||||
check_required_version(root.get_ref(), table_path)?;
|
||||
|
||||
let deserializer = toml::de::Deserializer::from(root);
|
||||
T::deserialize(deserializer)
|
||||
.map_err(|mut err| {
|
||||
// `Deserializer::from` doesn't have access to the original input, but we do.
|
||||
// Attach it so TOML errors include line/column and a source excerpt.
|
||||
err.set_input(Some(&contents));
|
||||
err
|
||||
})
|
||||
.with_context(|| format!("Failed to parse {}", path.display()))
|
||||
}
|
||||
|
||||
/// Parse a `ruff.toml` file.
|
||||
fn parse_ruff_toml<P: AsRef<Path>>(path: P) -> Result<Options> {
|
||||
parse_toml(path, &[])
|
||||
}
|
||||
|
||||
/// Parse a `pyproject.toml` file.
|
||||
fn parse_pyproject_toml<P: AsRef<Path>>(path: P) -> Result<Pyproject> {
|
||||
let path = path.as_ref();
|
||||
let contents = std::fs::read_to_string(path)
|
||||
.with_context(|| format!("Failed to read {}", path.display()))?;
|
||||
toml::from_str(&contents).with_context(|| format!("Failed to parse {}", path.display()))
|
||||
parse_toml(path, &["tool", "ruff"])
|
||||
}
|
||||
|
||||
/// Return `true` if a `pyproject.toml` contains a `[tool.ruff]` section.
|
||||
@@ -98,6 +117,33 @@ pub fn find_settings_toml<P: AsRef<Path>>(path: P) -> Result<Option<PathBuf>> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn check_required_version(value: &toml::de::DeTable, table_path: &[&str]) -> Result<()> {
|
||||
let mut current = value;
|
||||
for key in table_path {
|
||||
let Some(next) = current.get(*key) else {
|
||||
return Ok(());
|
||||
};
|
||||
let toml::de::DeValue::Table(next) = next.get_ref() else {
|
||||
return Ok(());
|
||||
};
|
||||
current = next;
|
||||
}
|
||||
|
||||
let required_version = current
|
||||
.get("required-version")
|
||||
.and_then(|value| value.get_ref().as_str());
|
||||
|
||||
let Some(required_version) = required_version else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
// If it doesn't parse, we just fall through to normal parsing; it will give a nicer error message.
|
||||
if let Ok(required_version) = required_version.parse::<RequiredVersion>() {
|
||||
validate_required_version(&required_version)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Derive target version from `required-version` in `pyproject.toml`, if
|
||||
/// such a file exists in an ancestor directory.
|
||||
pub fn find_fallback_target_version<P: AsRef<Path>>(path: P) -> Option<PythonVersion> {
|
||||
|
||||
3
crates/ty/docs/cli.md
generated
3
crates/ty/docs/cli.md
generated
@@ -37,7 +37,8 @@ ty check [OPTIONS] [PATH]...
|
||||
|
||||
<h3 class="cli-reference">Options</h3>
|
||||
|
||||
<dl class="cli-reference"><dt id="ty-check--color"><a href="#ty-check--color"><code>--color</code></a> <i>when</i></dt><dd><p>Control when colored output is used</p>
|
||||
<dl class="cli-reference"><dt id="ty-check--add-ignore"><a href="#ty-check--add-ignore"><code>--add-ignore</code></a></dt><dd><p>Adds <code>ty: ignore</code> comments to suppress all rule diagnostics</p>
|
||||
</dd><dt id="ty-check--color"><a href="#ty-check--color"><code>--color</code></a> <i>when</i></dt><dd><p>Control when colored output is used</p>
|
||||
<p>Possible values:</p>
|
||||
<ul>
|
||||
<li><code>auto</code>: Display colors if the output goes to an interactive terminal</li>
|
||||
|
||||
229
crates/ty/docs/rules.md
generated
229
crates/ty/docs/rules.md
generated
@@ -8,7 +8,7 @@
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.20">0.0.1-alpha.20</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20ambiguous-protocol-member" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L538" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L540" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ def test(): -> "int":
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20call-non-callable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L137" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L139" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ Calling a non-callable object will raise a `TypeError` at runtime.
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.7">0.0.7</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20call-top-callable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L155" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L157" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ def f(x: object):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20conflicting-argument-forms" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L206" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L208" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -167,7 +167,7 @@ f(int) # error
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20conflicting-declarations" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L232" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L234" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -198,7 +198,7 @@ a = 1
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20conflicting-metaclass" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L257" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L259" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -230,7 +230,7 @@ class C(A, B): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20cyclic-class-definition" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L283" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L285" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -262,7 +262,7 @@ class B(A): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/1.0.0">1.0.0</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20cyclic-type-alias-definition" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L309" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L311" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -290,7 +290,7 @@ type B = A
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.16">0.0.1-alpha.16</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20deprecated" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L353" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L355" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -317,7 +317,7 @@ old_func() # emits [deprecated] diagnostic
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'ignore'."><code>ignore</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20division-by-zero" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L331" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L333" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -346,7 +346,7 @@ false positives it can produce.
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20duplicate-base" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L374" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L376" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -373,7 +373,7 @@ class B(A, A): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.12">0.0.1-alpha.12</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20duplicate-kw-only" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L395" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L397" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -529,7 +529,7 @@ def test(): -> "Literal[5]":
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20inconsistent-mro" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L621" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L623" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -559,7 +559,7 @@ class C(A, B): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20index-out-of-bounds" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L645" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L647" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -585,7 +585,7 @@ t[3] # IndexError: tuple index out of range
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.12">0.0.1-alpha.12</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20instance-layout-conflict" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L427" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L429" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -674,7 +674,7 @@ an atypical memory layout.
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-argument-type" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L699" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L701" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -701,7 +701,7 @@ func("foo") # error: [invalid-argument-type]
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-assignment" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L739" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L741" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -729,7 +729,7 @@ a: int = ''
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-attribute-access" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2042" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2044" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -763,7 +763,7 @@ C.instance_var = 3 # error: Cannot assign to instance variable
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.19">0.0.1-alpha.19</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-await" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L761" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L763" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -799,7 +799,7 @@ asyncio.run(main())
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-base" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L791" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L793" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -823,7 +823,7 @@ class A(42): ... # error: [invalid-base]
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-context-manager" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L842" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L844" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -850,7 +850,7 @@ with 1:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-declaration" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L863" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L865" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -879,7 +879,7 @@ a: str
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-exception-caught" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L886" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L888" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -923,7 +923,7 @@ except ZeroDivisionError:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.28">0.0.1-alpha.28</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-explicit-override" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1712" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1714" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -965,7 +965,7 @@ class D(A):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.35">0.0.1-alpha.35</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-frozen-dataclass-subclass" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2268" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2295" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1009,7 +1009,7 @@ class NonFrozenChild(FrozenBase): # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-generic-class" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L922" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L924" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1077,7 +1077,7 @@ a = 20 / 0 # type: ignore
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.17">0.0.1-alpha.17</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-key" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L666" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L668" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1116,7 +1116,7 @@ carol = Person(name="Carol", age=25) # typo!
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-legacy-type-variable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L953" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L955" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1151,7 +1151,7 @@ def f(t: TypeVar("U")): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-metaclass" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1050" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1052" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1185,7 +1185,7 @@ class B(metaclass=f): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.20">0.0.1-alpha.20</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-method-override" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2170" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2197" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1292,7 +1292,7 @@ Correct use of `@override` is enforced by ty's `invalid-explicit-override` rule.
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.19">0.0.1-alpha.19</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-named-tuple" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L573" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L575" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1346,7 +1346,7 @@ AttributeError: Cannot overwrite NamedTuple attribute _asdict
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/1.0.0">1.0.0</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-newtype" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1026" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1028" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1376,7 +1376,7 @@ Baz = NewType("Baz", int | str) # error: invalid base for `typing.NewType`
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-overload" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1077" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1079" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1426,7 +1426,7 @@ def foo(x: int) -> int: ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-parameter-default" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1176" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1178" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1452,7 +1452,7 @@ def f(a: int = ''): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-paramspec" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L981" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L983" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1483,7 +1483,7 @@ P2 = ParamSpec("S2") # error: ParamSpec name must match the variable it's assig
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-protocol" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L509" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L511" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1517,7 +1517,7 @@ TypeError: Protocols can only inherit from other protocols, got <class 'int'>
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-raise" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1196" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1198" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1566,7 +1566,7 @@ def g():
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-return-type" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L720" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L722" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1591,7 +1591,7 @@ def func() -> int:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-super-argument" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1239" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1241" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1681,13 +1681,59 @@ class C: ...
|
||||
- [Typing spec: The meaning of annotations](https://typing.python.org/en/latest/spec/annotations.html#the-meaning-of-annotations)
|
||||
- [Typing spec: String annotations](https://typing.python.org/en/latest/spec/annotations.html#string-annotations)
|
||||
|
||||
## `invalid-total-ordering`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.10">0.0.10</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-total-ordering" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2333" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
**What it does**
|
||||
|
||||
Checks for classes decorated with `@functools.total_ordering` that don't
|
||||
define any ordering method (`__lt__`, `__le__`, `__gt__`, or `__ge__`).
|
||||
|
||||
**Why is this bad?**
|
||||
|
||||
The `@total_ordering` decorator requires the class to define at least one
|
||||
ordering method. If none is defined, Python raises a `ValueError` at runtime.
|
||||
|
||||
**Example**
|
||||
|
||||
|
||||
```python
|
||||
from functools import total_ordering
|
||||
|
||||
@total_ordering
|
||||
class MyClass: # Error: no ordering method defined
|
||||
def __eq__(self, other: object) -> bool:
|
||||
return True
|
||||
```
|
||||
|
||||
Use instead:
|
||||
|
||||
```python
|
||||
from functools import total_ordering
|
||||
|
||||
@total_ordering
|
||||
class MyClass:
|
||||
def __eq__(self, other: object) -> bool:
|
||||
return True
|
||||
|
||||
def __lt__(self, other: "MyClass") -> bool:
|
||||
return True
|
||||
```
|
||||
|
||||
## `invalid-type-alias-type`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.6">0.0.1-alpha.6</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-alias-type" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1005" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1007" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1714,7 +1760,7 @@ NewAlias = TypeAliasType(get_name(), int) # error: TypeAliasType name mus
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.29">0.0.1-alpha.29</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-arguments" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1471" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1473" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1761,7 +1807,7 @@ Bar[int] # error: too few arguments
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-checking-constant" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1278" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1280" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1791,7 +1837,7 @@ TYPE_CHECKING = ''
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-form" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1302" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1304" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1821,7 +1867,7 @@ b: Annotated[int] # `Annotated` expects at least two arguments
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.11">0.0.1-alpha.11</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-guard-call" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1354" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1356" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1855,7 +1901,7 @@ f(10) # Error
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.11">0.0.1-alpha.11</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-guard-definition" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1326" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1328" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1889,7 +1935,7 @@ class C:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-variable-constraints" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1382" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1384" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1918,13 +1964,44 @@ T = TypeVar('T', bound=str) # valid bound TypeVar
|
||||
|
||||
[type variables]: https://docs.python.org/3/library/typing.html#typing.TypeVar
|
||||
|
||||
## `invalid-typed-dict-statement`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.9">0.0.9</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-typed-dict-statement" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2172" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
**What it does**
|
||||
|
||||
Detects statements other than annotated declarations in `TypedDict` class bodies.
|
||||
|
||||
**Why is this bad?**
|
||||
|
||||
`TypedDict` class bodies aren't allowed to contain any other types of statements. For
|
||||
example, method definitions and field values aren't allowed. None of these will be
|
||||
available on "instances of the `TypedDict`" at runtime (as `dict` is the runtime class of
|
||||
all "`TypedDict` instances").
|
||||
|
||||
**Example**
|
||||
|
||||
```python
|
||||
from typing import TypedDict
|
||||
|
||||
class Foo(TypedDict):
|
||||
def bar(self): # error: [invalid-typed-dict-statement]
|
||||
pass
|
||||
```
|
||||
|
||||
## `missing-argument`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20missing-argument" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1411" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1413" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1949,7 +2026,7 @@ func() # TypeError: func() missing 1 required positional argument: 'x'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.20">0.0.1-alpha.20</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20missing-typed-dict-key" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2143" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2145" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1982,7 +2059,7 @@ alice["age"] # KeyError
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20no-matching-overload" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1430" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1432" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2011,7 +2088,7 @@ func("string") # error: [no-matching-overload]
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20not-iterable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1512" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1514" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2037,7 +2114,7 @@ for i in 34: # TypeError: 'int' object is not iterable
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20not-subscriptable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1453" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1455" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2061,7 +2138,7 @@ Subscripting an object that does not support it will raise a `TypeError` at runt
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.29">0.0.1-alpha.29</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20override-of-final-method" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1685" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1687" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2094,7 +2171,7 @@ class B(A):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20parameter-already-assigned" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1563" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1565" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2121,7 +2198,7 @@ f(1, x=2) # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20positional-only-parameter-as-kwarg" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1896" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1898" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2148,7 +2225,7 @@ f(x=1) # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20possibly-missing-attribute" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1584" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1586" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2176,7 +2253,7 @@ A.c # AttributeError: type object 'A' has no attribute 'c'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20possibly-missing-implicit-call" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L180" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L182" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2208,7 +2285,7 @@ A()[0] # TypeError: 'A' object is not subscriptable
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'ignore'."><code>ignore</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20possibly-missing-import" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1606" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1608" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2245,7 +2322,7 @@ from module import a # ImportError: cannot import name 'a' from 'module'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'ignore'."><code>ignore</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20possibly-unresolved-reference" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1636" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1638" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2309,7 +2386,7 @@ def test(): -> "int":
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20redundant-cast" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2070" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2072" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2336,7 +2413,7 @@ cast(int, f()) # Redundant
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20static-assert-error" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2018" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2020" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2366,7 +2443,7 @@ static_assert(int(2.0 * 3.0) == 6) # error: does not have a statically known tr
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20subclass-of-final-class" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1662" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1664" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2395,7 +2472,7 @@ class B(A): ... # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.30">0.0.1-alpha.30</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20super-call-in-named-tuple-method" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1830" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1832" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2429,7 +2506,7 @@ class F(NamedTuple):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20too-many-positional-arguments" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1770" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1772" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2456,7 +2533,7 @@ f("foo") # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20type-assertion-failure" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1748" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1750" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2484,7 +2561,7 @@ def _(x: int):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unavailable-implicit-super-arguments" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1791" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1793" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2530,7 +2607,7 @@ class A:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20undefined-reveal" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1857" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1859" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2554,7 +2631,7 @@ reveal_type(1) # NameError: name 'reveal_type' is not defined
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unknown-argument" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1875" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1877" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2581,7 +2658,7 @@ f(x=1, y=2) # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unresolved-attribute" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1917" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1919" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2609,7 +2686,7 @@ A().foo # AttributeError: 'A' object has no attribute 'foo'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.15">0.0.1-alpha.15</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unresolved-global" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2091" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2093" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2667,7 +2744,7 @@ def g():
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unresolved-import" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1939" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1941" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2692,7 +2769,7 @@ import foo # ModuleNotFoundError: No module named 'foo'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unresolved-reference" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1958" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1960" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2717,7 +2794,7 @@ print(x) # NameError: name 'x' is not defined
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.7">0.0.1-alpha.7</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unsupported-base" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L809" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L811" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2756,7 +2833,7 @@ class D(C): ... # error: [unsupported-base]
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unsupported-bool-conversion" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1532" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1534" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2793,7 +2870,7 @@ b1 < b2 < b1 # exception raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unsupported-operator" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1977" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1979" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2852,7 +2929,7 @@ a = 20 / 2
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20useless-overload-body" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1120" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1122" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2915,7 +2992,7 @@ def foo(x: int | str) -> int | str:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20zero-stepsize-in-slice" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1999" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2001" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
|
||||
@@ -54,6 +54,10 @@ pub(crate) struct CheckCommand {
|
||||
)]
|
||||
pub paths: Vec<SystemPathBuf>,
|
||||
|
||||
/// Adds `ty: ignore` comments to suppress all rule diagnostics.
|
||||
#[arg(long)]
|
||||
pub(crate) add_ignore: bool,
|
||||
|
||||
/// Run the command within the given project directory.
|
||||
///
|
||||
/// All `pyproject.toml` files will be discovered by walking up the directory tree from the given project directory,
|
||||
|
||||
@@ -4,37 +4,36 @@ mod printer;
|
||||
mod python_version;
|
||||
mod version;
|
||||
|
||||
pub use args::Cli;
|
||||
use ty_project::metadata::settings::TerminalSettings;
|
||||
use ty_static::EnvVars;
|
||||
|
||||
use std::fmt::Write;
|
||||
use std::process::{ExitCode, Termination};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::args::{CheckCommand, Command, TerminalColor};
|
||||
use crate::logging::{VerbosityLevel, setup_tracing};
|
||||
use crate::printer::Printer;
|
||||
use anyhow::{Context, anyhow};
|
||||
use clap::{CommandFactory, Parser};
|
||||
use colored::Colorize;
|
||||
use crossbeam::channel as crossbeam_channel;
|
||||
use rayon::ThreadPoolBuilder;
|
||||
use ruff_db::cancellation::{CancellationToken, CancellationTokenSource};
|
||||
use ruff_db::cancellation::{Canceled, CancellationToken, CancellationTokenSource};
|
||||
use ruff_db::diagnostic::{
|
||||
Diagnostic, DiagnosticId, DisplayDiagnosticConfig, DisplayDiagnostics, Severity,
|
||||
};
|
||||
use ruff_db::files::File;
|
||||
use ruff_db::max_parallelism;
|
||||
use ruff_db::system::{OsSystem, SystemPath, SystemPathBuf};
|
||||
use ruff_db::{STACK_SIZE, max_parallelism};
|
||||
use salsa::Database;
|
||||
use ty_project::metadata::options::ProjectOptionsOverrides;
|
||||
use ty_project::metadata::settings::TerminalSettings;
|
||||
use ty_project::watch::ProjectWatcher;
|
||||
use ty_project::{CollectReporter, Db, watch};
|
||||
use ty_project::{CollectReporter, Db, suppress_all_diagnostics, watch};
|
||||
use ty_project::{ProjectDatabase, ProjectMetadata};
|
||||
use ty_server::run_server;
|
||||
use ty_static::EnvVars;
|
||||
|
||||
use crate::args::{CheckCommand, Command, TerminalColor};
|
||||
use crate::logging::{VerbosityLevel, setup_tracing};
|
||||
use crate::printer::Printer;
|
||||
pub use args::Cli;
|
||||
|
||||
pub fn run() -> anyhow::Result<ExitStatus> {
|
||||
setup_rayon();
|
||||
@@ -112,6 +111,12 @@ fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {
|
||||
.map(|path| SystemPath::absolute(path, &cwd))
|
||||
.collect();
|
||||
|
||||
let mode = if args.add_ignore {
|
||||
MainLoopMode::AddIgnore
|
||||
} else {
|
||||
MainLoopMode::Check
|
||||
};
|
||||
|
||||
let system = OsSystem::new(&cwd);
|
||||
let watch = args.watch;
|
||||
let exit_zero = args.exit_zero;
|
||||
@@ -144,7 +149,7 @@ fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {
|
||||
}
|
||||
|
||||
let (main_loop, main_loop_cancellation_token) =
|
||||
MainLoop::new(project_options_overrides, printer);
|
||||
MainLoop::new(mode, project_options_overrides, printer);
|
||||
|
||||
// Listen to Ctrl+C and abort the watch mode.
|
||||
let main_loop_cancellation_token = Mutex::new(Some(main_loop_cancellation_token));
|
||||
@@ -215,6 +220,8 @@ impl Termination for ExitStatus {
|
||||
}
|
||||
|
||||
struct MainLoop {
|
||||
mode: MainLoopMode,
|
||||
|
||||
/// Sender that can be used to send messages to the main loop.
|
||||
sender: crossbeam_channel::Sender<MainLoopMessage>,
|
||||
|
||||
@@ -237,6 +244,7 @@ struct MainLoop {
|
||||
|
||||
impl MainLoop {
|
||||
fn new(
|
||||
mode: MainLoopMode,
|
||||
project_options_overrides: ProjectOptionsOverrides,
|
||||
printer: Printer,
|
||||
) -> (Self, MainLoopCancellationToken) {
|
||||
@@ -247,6 +255,7 @@ impl MainLoop {
|
||||
|
||||
(
|
||||
Self {
|
||||
mode,
|
||||
sender: sender.clone(),
|
||||
receiver,
|
||||
watcher: None,
|
||||
@@ -325,80 +334,78 @@ impl MainLoop {
|
||||
result,
|
||||
revision: check_revision,
|
||||
} => {
|
||||
let terminal_settings = db.project().settings(db).terminal();
|
||||
let display_config = DisplayDiagnosticConfig::default()
|
||||
.format(terminal_settings.output_format.into())
|
||||
.color(colored::control::SHOULD_COLORIZE.should_colorize())
|
||||
.with_cancellation_token(Some(self.cancellation_token.clone()))
|
||||
.show_fix_diff(true);
|
||||
|
||||
if check_revision == revision {
|
||||
if db.project().files(db).is_empty() {
|
||||
tracing::warn!("No python files found under the given path(s)");
|
||||
}
|
||||
|
||||
// TODO: We should have an official flag to silence workspace diagnostics.
|
||||
if std::env::var("TY_MEMORY_REPORT").as_deref() == Ok("mypy_primer") {
|
||||
return Ok(ExitStatus::Success);
|
||||
}
|
||||
|
||||
let is_human_readable = terminal_settings.output_format.is_human_readable();
|
||||
|
||||
if result.is_empty() {
|
||||
if is_human_readable {
|
||||
writeln!(
|
||||
self.printer.stream_for_success_summary(),
|
||||
"{}",
|
||||
"All checks passed!".green().bold()
|
||||
)?;
|
||||
}
|
||||
|
||||
if self.watcher.is_none() {
|
||||
return Ok(ExitStatus::Success);
|
||||
}
|
||||
} else {
|
||||
let diagnostics_count = result.len();
|
||||
|
||||
let mut stdout = self.printer.stream_for_details().lock();
|
||||
let exit_status =
|
||||
exit_status_from_diagnostics(&result, terminal_settings);
|
||||
|
||||
// Only render diagnostics if they're going to be displayed, since doing
|
||||
// so is expensive.
|
||||
if stdout.is_enabled() {
|
||||
write!(
|
||||
stdout,
|
||||
"{}",
|
||||
DisplayDiagnostics::new(db, &display_config, &result)
|
||||
)?;
|
||||
}
|
||||
|
||||
if !self.cancellation_token.is_cancelled() {
|
||||
if is_human_readable {
|
||||
writeln!(
|
||||
self.printer.stream_for_failure_summary(),
|
||||
"Found {} diagnostic{}",
|
||||
diagnostics_count,
|
||||
if diagnostics_count > 1 { "s" } else { "" }
|
||||
)?;
|
||||
}
|
||||
|
||||
if exit_status.is_internal_error() {
|
||||
tracing::warn!(
|
||||
"A fatal error occurred while checking some files. Not all project files were analyzed. See the diagnostics list above for details."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if self.watcher.is_none() {
|
||||
return Ok(exit_status);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if check_revision != revision {
|
||||
tracing::debug!(
|
||||
"Discarding check result for outdated revision: current: {revision}, result revision: {check_revision}"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if db.project().files(db).is_empty() {
|
||||
tracing::warn!("No python files found under the given path(s)");
|
||||
}
|
||||
|
||||
let result = match self.mode {
|
||||
MainLoopMode::Check => {
|
||||
// TODO: We should have an official flag to silence workspace diagnostics.
|
||||
if std::env::var("TY_MEMORY_REPORT").as_deref() == Ok("mypy_primer") {
|
||||
return Ok(ExitStatus::Success);
|
||||
}
|
||||
|
||||
self.write_diagnostics(db, &result)?;
|
||||
|
||||
if self.cancellation_token.is_cancelled() {
|
||||
Err(Canceled)
|
||||
} else {
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
MainLoopMode::AddIgnore => {
|
||||
if let Ok(result) =
|
||||
suppress_all_diagnostics(db, result, &self.cancellation_token)
|
||||
{
|
||||
self.write_diagnostics(db, &result.diagnostics)?;
|
||||
|
||||
let terminal_settings = db.project().settings(db).terminal();
|
||||
let is_human_readable =
|
||||
terminal_settings.output_format.is_human_readable();
|
||||
|
||||
if is_human_readable {
|
||||
writeln!(
|
||||
self.printer.stream_for_failure_summary(),
|
||||
"Added {} ignore comment{}",
|
||||
result.count,
|
||||
if result.count > 1 { "s" } else { "" }
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(result.diagnostics)
|
||||
} else {
|
||||
Err(Canceled)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let exit_status = match result.as_deref() {
|
||||
Ok([]) => ExitStatus::Success,
|
||||
Ok(diagnostics) => {
|
||||
let terminal_settings = db.project().settings(db).terminal();
|
||||
exit_status_from_diagnostics(diagnostics, terminal_settings)
|
||||
}
|
||||
Err(Canceled) => ExitStatus::Success,
|
||||
};
|
||||
|
||||
if exit_status.is_internal_error() {
|
||||
tracing::warn!(
|
||||
"A fatal error occurred while checking some files. Not all project files were analyzed. See the diagnostics list above for details."
|
||||
);
|
||||
}
|
||||
|
||||
if self.watcher.is_some() {
|
||||
continue;
|
||||
}
|
||||
|
||||
return Ok(exit_status);
|
||||
}
|
||||
|
||||
MainLoopMessage::ApplyChanges(changes) => {
|
||||
@@ -425,6 +432,65 @@ impl MainLoop {
|
||||
|
||||
Ok(ExitStatus::Success)
|
||||
}
|
||||
|
||||
fn write_diagnostics(
|
||||
&self,
|
||||
db: &ProjectDatabase,
|
||||
diagnostics: &[Diagnostic],
|
||||
) -> anyhow::Result<()> {
|
||||
let terminal_settings = db.project().settings(db).terminal();
|
||||
let is_human_readable = terminal_settings.output_format.is_human_readable();
|
||||
|
||||
match diagnostics {
|
||||
[] => {
|
||||
if is_human_readable {
|
||||
writeln!(
|
||||
self.printer.stream_for_success_summary(),
|
||||
"{}",
|
||||
"All checks passed!".green().bold()
|
||||
)?;
|
||||
}
|
||||
}
|
||||
diagnostics => {
|
||||
let diagnostics_count = diagnostics.len();
|
||||
|
||||
let mut stdout = self.printer.stream_for_details().lock();
|
||||
|
||||
// Only render diagnostics if they're going to be displayed, since doing
|
||||
// so is expensive.
|
||||
if stdout.is_enabled() {
|
||||
let display_config = DisplayDiagnosticConfig::default()
|
||||
.format(terminal_settings.output_format.into())
|
||||
.color(colored::control::SHOULD_COLORIZE.should_colorize())
|
||||
.with_cancellation_token(Some(self.cancellation_token.clone()))
|
||||
.show_fix_diff(true);
|
||||
|
||||
write!(
|
||||
stdout,
|
||||
"{}",
|
||||
DisplayDiagnostics::new(db, &display_config, diagnostics)
|
||||
)?;
|
||||
}
|
||||
|
||||
if !self.cancellation_token.is_cancelled() && is_human_readable {
|
||||
writeln!(
|
||||
self.printer.stream_for_failure_summary(),
|
||||
"Found {} diagnostic{}",
|
||||
diagnostics_count,
|
||||
if diagnostics_count > 1 { "s" } else { "" }
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
enum MainLoopMode {
|
||||
Check,
|
||||
AddIgnore,
|
||||
}
|
||||
|
||||
fn exit_status_from_diagnostics(
|
||||
@@ -559,12 +625,7 @@ fn set_colored_override(color: Option<TerminalColor>) {
|
||||
fn setup_rayon() {
|
||||
ThreadPoolBuilder::default()
|
||||
.num_threads(max_parallelism().get())
|
||||
// Use a reasonably large stack size to avoid running into stack overflows too easily. The
|
||||
// size was chosen in such a way as to still be able to handle large expressions involving
|
||||
// binary operators (x + x + … + x) both during the AST walk in semantic index building as
|
||||
// well as during type checking. Using this stack size, we can handle handle expressions
|
||||
// that are several times larger than the corresponding limits in existing type checkers.
|
||||
.stack_size(16 * 1024 * 1024)
|
||||
.stack_size(STACK_SIZE)
|
||||
.build_global()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -160,6 +160,65 @@ fn configuration_include() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Files without extensions can be included by adding a literal glob to `include` that matches
|
||||
/// the path exactly. A literal glob is a glob without any meta characters.
|
||||
#[test]
|
||||
fn configuration_include_no_extension() -> anyhow::Result<()> {
|
||||
let case = CliTest::with_files([(
|
||||
"src/main",
|
||||
r#"
|
||||
print(undefined_var) # error: unresolved-reference
|
||||
"#,
|
||||
)])?;
|
||||
|
||||
// By default, `src/main` is excluded because the file has no supported extension.
|
||||
case.write_file(
|
||||
"ty.toml",
|
||||
r#"
|
||||
[src]
|
||||
include = ["src"]
|
||||
"#,
|
||||
)?;
|
||||
|
||||
assert_cmd_snapshot!(case.command(), @r"
|
||||
success: true
|
||||
exit_code: 0
|
||||
----- stdout -----
|
||||
All checks passed!
|
||||
|
||||
----- stderr -----
|
||||
WARN No python files found under the given path(s)
|
||||
");
|
||||
|
||||
// The file can be included by adding an exactly matching pattern
|
||||
case.write_file(
|
||||
"ty.toml",
|
||||
r#"
|
||||
[src]
|
||||
include = ["src", "src/main"]
|
||||
"#,
|
||||
)?;
|
||||
|
||||
assert_cmd_snapshot!(case.command(), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-reference]: Name `undefined_var` used when not defined
|
||||
--> src/main:2:7
|
||||
|
|
||||
2 | print(undefined_var) # error: unresolved-reference
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
info: rule `unresolved-reference` is enabled by default
|
||||
|
||||
Found 1 diagnostic
|
||||
|
||||
----- stderr -----
|
||||
");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Test configuration file exclude functionality
|
||||
#[test]
|
||||
fn configuration_exclude() -> anyhow::Result<()> {
|
||||
|
||||
114
crates/ty/tests/cli/fixes.rs
Normal file
114
crates/ty/tests/cli/fixes.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
use insta_cmd::assert_cmd_snapshot;
|
||||
|
||||
use crate::CliTest;
|
||||
|
||||
#[test]
|
||||
fn add_ignore() -> anyhow::Result<()> {
|
||||
let case = CliTest::with_file(
|
||||
"different_violations.py",
|
||||
r#"
|
||||
import sys
|
||||
|
||||
x = 1 + a
|
||||
|
||||
if sys.does_not_exist:
|
||||
...
|
||||
|
||||
def test(a, b): ...
|
||||
|
||||
test(x = 10, b = 12)
|
||||
"#,
|
||||
)?;
|
||||
|
||||
assert_cmd_snapshot!(case.command().arg("--add-ignore"), @r"
|
||||
success: true
|
||||
exit_code: 0
|
||||
----- stdout -----
|
||||
All checks passed!
|
||||
Added 4 ignore comments
|
||||
|
||||
----- stderr -----
|
||||
");
|
||||
|
||||
// There should be no diagnostics when running ty again
|
||||
assert_cmd_snapshot!(case.command(), @r"
|
||||
success: true
|
||||
exit_code: 0
|
||||
----- stdout -----
|
||||
All checks passed!
|
||||
|
||||
----- stderr -----
|
||||
");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_ignore_unfixable() -> anyhow::Result<()> {
|
||||
let case = CliTest::with_files([
|
||||
("has_syntax_error.py", r"print(x # [unresolved-reference]"),
|
||||
(
|
||||
"different_violations.py",
|
||||
r#"
|
||||
import sys
|
||||
|
||||
x = 1 + a
|
||||
|
||||
reveal_type(x)
|
||||
|
||||
if sys.does_not_exist:
|
||||
...
|
||||
"#,
|
||||
),
|
||||
(
|
||||
"repeated_violations.py",
|
||||
r#"
|
||||
x = (
|
||||
1 +
|
||||
a * b
|
||||
)
|
||||
|
||||
y = y # ty: ignore[unresolved-reference]
|
||||
"#,
|
||||
),
|
||||
])?;
|
||||
|
||||
assert_cmd_snapshot!(case.command().arg("--add-ignore").env("RUST_BACKTRACE", "1"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
info[revealed-type]: Revealed type
|
||||
--> different_violations.py:6:13
|
||||
|
|
||||
4 | x = 1 + a # ty:ignore[unresolved-reference]
|
||||
5 |
|
||||
6 | reveal_type(x) # ty:ignore[undefined-reveal]
|
||||
| ^ `Unknown`
|
||||
7 |
|
||||
8 | if sys.does_not_exist: # ty:ignore[unresolved-attribute]
|
||||
|
|
||||
|
||||
error[unresolved-reference]: Name `x` used when not defined
|
||||
--> has_syntax_error.py:1:7
|
||||
|
|
||||
1 | print(x # [unresolved-reference]
|
||||
| ^
|
||||
|
|
||||
info: rule `unresolved-reference` is enabled by default
|
||||
|
||||
error[invalid-syntax]: unexpected EOF while parsing
|
||||
--> has_syntax_error.py:1:34
|
||||
|
|
||||
1 | print(x # [unresolved-reference]
|
||||
| ^
|
||||
|
|
||||
|
||||
Found 3 diagnostics
|
||||
Added 5 ignore comments
|
||||
|
||||
----- stderr -----
|
||||
WARN Skipping file `<temp_dir>/has_syntax_error.py` with syntax errors
|
||||
");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -2,6 +2,7 @@ mod analysis_options;
|
||||
mod config_option;
|
||||
mod exit_code;
|
||||
mod file_selection;
|
||||
mod fixes;
|
||||
mod python_environment;
|
||||
mod rule_selection;
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ auto-import-includes-modules,main.py,0,1
|
||||
auto-import-includes-modules,main.py,1,7
|
||||
auto-import-includes-modules,main.py,2,1
|
||||
auto-import-skips-current-module,main.py,0,1
|
||||
class-arg-completion,main.py,0,1
|
||||
fstring-completions,main.py,0,1
|
||||
higher-level-symbols-preferred,main.py,0,
|
||||
higher-level-symbols-preferred,main.py,1,1
|
||||
|
||||
|
@@ -0,0 +1,2 @@
|
||||
[settings]
|
||||
auto-import = false
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user