Compare commits

...

5 Commits

Author SHA1 Message Date
Charlie Marsh
0e6a38e6d8 Bump version to 0.0.146 2022-11-28 22:27:41 -05:00
Charlie Marsh
058fd8748d Re-generate check code prefix and rules table 2022-11-28 22:26:32 -05:00
Charlie Marsh
e8247e3ed9 Run cargo fmt 2022-11-28 22:25:09 -05:00
Charlie Marsh
ea73c717be Remove pre-commit note in README.md (#956) 2022-11-28 22:18:59 -05:00
Charlie Marsh
427e0c3158 Allow preservation of external check codes (#955) 2022-11-28 22:16:17 -05:00
19 changed files with 157 additions and 59 deletions

View File

@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.145
rev: v0.0.146
hooks:
- id: ruff

6
Cargo.lock generated
View File

@@ -700,7 +700,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8-to-ruff"
version = "0.0.145-dev.0"
version = "0.0.146-dev.0"
dependencies = [
"anyhow",
"clap 4.0.22",
@@ -1805,7 +1805,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.145"
version = "0.0.146"
dependencies = [
"annotate-snippets 0.9.1",
"anyhow",
@@ -1856,7 +1856,7 @@ dependencies = [
[[package]]
name = "ruff_dev"
version = "0.0.145"
version = "0.0.146"
dependencies = [
"anyhow",
"clap 4.0.22",

View File

@@ -6,7 +6,7 @@ members = [
[package]
name = "ruff"
version = "0.0.145"
version = "0.0.146"
edition = "2021"
rust-version = "1.65.0"

View File

@@ -135,14 +135,11 @@ Ruff also works with [pre-commit](https://pre-commit.com):
```yaml
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.145
rev: v0.0.146
hooks:
- id: ruff
```
<!-- TODO(charlie): Remove this message a few versions after v0.0.86. -->
_Note: prior to `v0.0.86`, `ruff-pre-commit` used `lint` (rather than `ruff`) as the hook ID._
## Configuration
Ruff is configurable both via `pyproject.toml` and the command line. For a full list of configurable
@@ -540,7 +537,7 @@ For more, see [eradicate](https://pypi.org/project/eradicate/2.1.0/) on PyPI.
| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| ERA001 | CommentedCode | Found commented-out code | 🛠 |
| ERA001 | CommentedOutCode | Found commented-out code | 🛠 |
### flake8-bandit
@@ -1348,6 +1345,27 @@ extend-select = ["B", "Q"]
---
#### [`external`](#external)
A list of check codes that are unsupported by Ruff, but should be preserved when (e.g.)
validating `# noqa` directives. Useful for retaining `# noqa` directives that cover plugins not
yet implemented in Ruff.
**Default value**: `[]`
**Type**: `Vec<String>`
**Example usage**:
```toml
[tool.ruff]
# Avoiding flagging (and removing) `V101` from any `# noqa` directives, despite Ruff's lack of
# support for `vulture`.
external = ["V101"]
```
---
#### [`fix`](#fix)
Enable autofix behavior by-default when running `ruff` (overridden by the `--fix` and `--no-fix`

View File

@@ -771,7 +771,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8_to_ruff"
version = "0.0.145"
version = "0.0.146"
dependencies = [
"anyhow",
"clap",
@@ -1975,7 +1975,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.145"
version = "0.0.146"
dependencies = [
"anyhow",
"bincode",

View File

@@ -1,6 +1,6 @@
[package]
name = "flake8-to-ruff"
version = "0.0.145-dev.0"
version = "0.0.146-dev.0"
edition = "2021"
[lib]

View File

@@ -248,6 +248,7 @@ mod tests {
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: Some(vec![]),
@@ -287,6 +288,7 @@ mod tests {
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: Some(vec![]),
@@ -326,6 +328,7 @@ mod tests {
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: Some(vec![]),
@@ -365,6 +368,7 @@ mod tests {
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: Some(vec![]),
@@ -404,6 +408,7 @@ mod tests {
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: Some(vec![]),
@@ -451,6 +456,7 @@ mod tests {
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: Some(vec![]),
@@ -525,6 +531,7 @@ mod tests {
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: Some(vec![]),

View File

@@ -18,6 +18,9 @@ def f() -> None:
# Invalid (and unimplemented)
d = 1 # noqa: F841, W191
# Invalid (but external)
d = 1 # noqa: F841, V101
# fmt: off
# Invalid - no space before #
d = 1# noqa: E501

View File

@@ -5,6 +5,7 @@ extend-exclude = [
"migrations",
"with_excluded_file/other_excluded_file.py",
]
external = ["V101"]
per-file-ignores = { "__init__.py" = ["F401"] }
[tool.ruff.flake8-bugbear]

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff_dev"
version = "0.0.145"
version = "0.0.146"
edition = "2021"
[dependencies]

View File

@@ -212,7 +212,7 @@ pub fn check_lines(
let mut invalid_codes = vec![];
let mut valid_codes = vec![];
for code in codes {
if matches.contains(&code) {
if matches.contains(&code) || settings.external.contains(code) {
valid_codes.push(code.to_string());
} else {
invalid_codes.push(code.to_string());

View File

@@ -177,8 +177,8 @@ pub enum CheckCodePrefix {
E99,
E999,
ERA,
ERA8,
ERA80,
ERA0,
ERA00,
ERA001,
F,
F4,
@@ -868,8 +868,8 @@ impl CheckCodePrefix {
CheckCodePrefix::E99 => vec![CheckCode::E999],
CheckCodePrefix::E999 => vec![CheckCode::E999],
CheckCodePrefix::ERA => vec![CheckCode::ERA001],
CheckCodePrefix::ERA8 => vec![CheckCode::ERA001],
CheckCodePrefix::ERA80 => vec![CheckCode::ERA001],
CheckCodePrefix::ERA0 => vec![CheckCode::ERA001],
CheckCodePrefix::ERA00 => vec![CheckCode::ERA001],
CheckCodePrefix::ERA001 => vec![CheckCode::ERA001],
CheckCodePrefix::F => vec![
CheckCode::F401,
@@ -1503,8 +1503,8 @@ impl CheckCodePrefix {
CheckCodePrefix::E99 => PrefixSpecificity::Tens,
CheckCodePrefix::E999 => PrefixSpecificity::Explicit,
CheckCodePrefix::ERA => PrefixSpecificity::Category,
CheckCodePrefix::ERA8 => PrefixSpecificity::Hundreds,
CheckCodePrefix::ERA80 => PrefixSpecificity::Tens,
CheckCodePrefix::ERA0 => PrefixSpecificity::Hundreds,
CheckCodePrefix::ERA00 => PrefixSpecificity::Tens,
CheckCodePrefix::ERA001 => PrefixSpecificity::Explicit,
CheckCodePrefix::F => PrefixSpecificity::Category,
CheckCodePrefix::F4 => PrefixSpecificity::Hundreds,

View File

@@ -262,7 +262,13 @@ pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result<usize> {
true,
)?;
add_noqa(&checks, &contents, &directives.noqa_line_for, path)
add_noqa(
path,
&checks,
&contents,
&directives.noqa_line_for,
&settings.external,
)
}
/// Apply autoformatting to the source code at the given `Path`.

View File

@@ -3,6 +3,7 @@ use std::fs;
use std::path::Path;
use anyhow::Result;
use itertools::Itertools;
use nohash_hasher::IntMap;
use once_cell::sync::Lazy;
use regex::Regex;
@@ -52,12 +53,13 @@ pub fn extract_noqa_directive(line: &str) -> Directive {
}
pub fn add_noqa(
path: &Path,
checks: &[Check],
contents: &str,
noqa_line_for: &IntMap<usize, usize>,
path: &Path,
external: &BTreeSet<String>,
) -> Result<usize> {
let (count, output) = add_noqa_inner(checks, contents, noqa_line_for);
let (count, output) = add_noqa_inner(checks, contents, noqa_line_for, external);
fs::write(path, output)?;
Ok(count)
}
@@ -66,6 +68,7 @@ fn add_noqa_inner(
checks: &[Check],
contents: &str,
noqa_line_for: &IntMap<usize, usize>,
external: &BTreeSet<String>,
) -> (usize, String) {
let lines: Vec<&str> = contents.lines().collect();
let mut matches_by_line: BTreeMap<usize, BTreeSet<&CheckCode>> = BTreeMap::new();
@@ -112,25 +115,47 @@ fn add_noqa_inner(
output.push('\n');
count += 1;
}
Directive::All(_, start, _) | Directive::Codes(_, start, ..) => {
let mut new_line = String::new();
Directive::All(_, start, _) => {
// Add existing content.
new_line.push_str(line[..start].trim_end());
output.push_str(line[..start].trim_end());
// Add `noqa` directive.
new_line.push_str(" # noqa: ");
output.push_str(" # noqa: ");
// Add codes.
let codes: Vec<&str> = codes.iter().map(AsRef::as_ref).collect();
let codes: Vec<&str> =
codes.iter().map(AsRef::as_ref).sorted_unstable().collect();
let suffix = codes.join(", ");
new_line.push_str(&suffix);
output.push_str(&suffix);
output.push('\n');
count += 1;
}
Directive::Codes(_, start, _, existing) => {
// Reconstruct the line based on the preserved check codes.
// This enables us to tally the number of edits.
let mut formatted = String::new();
output.push_str(&new_line);
// Add existing content.
formatted.push_str(line[..start].trim_end());
// Add `noqa` directive.
formatted.push_str(" # noqa: ");
// Add codes.
let codes: Vec<&str> = codes
.iter()
.map(AsRef::as_ref)
.chain(existing.into_iter().filter(|code| external.contains(*code)))
.sorted_unstable()
.collect();
let suffix = codes.join(", ");
formatted.push_str(&suffix);
output.push_str(&formatted);
output.push('\n');
// Only count if the new line is an actual edit.
if &new_line != line {
if &formatted != line {
count += 1;
}
}
@@ -144,6 +169,7 @@ fn add_noqa_inner(
#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use nohash_hasher::IntMap;
use rustpython_parser::ast::Location;
@@ -171,7 +197,8 @@ mod tests {
let checks = vec![];
let contents = "x = 1";
let noqa_line_for = IntMap::default();
let (count, output) = add_noqa_inner(&checks, contents, &noqa_line_for);
let external = BTreeSet::default();
let (count, output) = add_noqa_inner(&checks, contents, &noqa_line_for, &external);
assert_eq!(count, 0);
assert_eq!(output.trim(), contents.trim());
@@ -184,7 +211,8 @@ mod tests {
)];
let contents = "x = 1";
let noqa_line_for = IntMap::default();
let (count, output) = add_noqa_inner(&checks, contents, &noqa_line_for);
let external = BTreeSet::default();
let (count, output) = add_noqa_inner(&checks, contents, &noqa_line_for, &external);
assert_eq!(count, 1);
assert_eq!(output.trim(), "x = 1 # noqa: F841".trim());
@@ -206,7 +234,8 @@ mod tests {
];
let contents = "x = 1 # noqa: E741";
let noqa_line_for = IntMap::default();
let (count, output) = add_noqa_inner(&checks, contents, &noqa_line_for);
let external = BTreeSet::default();
let (count, output) = add_noqa_inner(&checks, contents, &noqa_line_for, &external);
assert_eq!(count, 1);
assert_eq!(output.trim(), "x = 1 # noqa: E741, F841".trim());
@@ -228,7 +257,8 @@ mod tests {
];
let contents = "x = 1 # noqa";
let noqa_line_for = IntMap::default();
let (count, output) = add_noqa_inner(&checks, contents, &noqa_line_for);
let external = BTreeSet::default();
let (count, output) = add_noqa_inner(&checks, contents, &noqa_line_for, &external);
assert_eq!(count, 1);
assert_eq!(output.trim(), "x = 1 # noqa: E741, F841".trim());
}

View File

@@ -24,6 +24,7 @@ pub struct Configuration {
pub extend_exclude: Vec<FilePattern>,
pub extend_ignore: Vec<CheckCodePrefix>,
pub extend_select: Vec<CheckCodePrefix>,
pub external: Vec<String>,
pub fix: bool,
pub fixable: Vec<CheckCodePrefix>,
pub ignore: Vec<CheckCodePrefix>,
@@ -116,6 +117,7 @@ impl Configuration {
.select
.unwrap_or_else(|| vec![CheckCodePrefix::E, CheckCodePrefix::F]),
extend_select: options.extend_select.unwrap_or_default(),
external: options.external.unwrap_or_default(),
fix: options.fix.unwrap_or_default(),
fixable: options.fixable.unwrap_or_else(|| CATEGORIES.to_vec()),
unfixable: options.unfixable.unwrap_or_default(),

View File

@@ -32,6 +32,7 @@ pub struct Settings {
pub enabled: FxHashSet<CheckCode>,
pub exclude: GlobSet,
pub extend_exclude: GlobSet,
pub external: BTreeSet<String>,
pub fixable: FxHashSet<CheckCode>,
pub line_length: usize,
pub per_file_ignores: Vec<(GlobMatcher, GlobMatcher, BTreeSet<CheckCode>)>,
@@ -69,6 +70,7 @@ impl Settings {
),
exclude: resolve_globset(config.exclude, project_root)?,
extend_exclude: resolve_globset(config.extend_exclude, project_root)?,
external: BTreeSet::from_iter(config.external),
fixable: resolve_codes(&config.fixable, &config.unfixable),
flake8_annotations: config.flake8_annotations,
flake8_bugbear: config.flake8_bugbear,
@@ -92,6 +94,7 @@ impl Settings {
fixable: FxHashSet::from_iter([check_code]),
exclude: GlobSet::empty(),
extend_exclude: GlobSet::empty(),
external: BTreeSet::default(),
line_length: 88,
per_file_ignores: vec![],
src: vec![path_dedot::CWD.clone()],
@@ -114,6 +117,7 @@ impl Settings {
fixable: FxHashSet::from_iter(check_codes),
exclude: GlobSet::empty(),
extend_exclude: GlobSet::empty(),
external: BTreeSet::default(),
line_length: 88,
per_file_ignores: vec![],
src: vec![path_dedot::CWD.clone()],
@@ -137,6 +141,7 @@ impl Hash for Settings {
for value in &self.enabled {
value.hash(state);
}
self.external.hash(state);
for value in &self.fixable {
value.hash(state);
}

View File

@@ -18,6 +18,7 @@ pub struct Options {
pub extend_exclude: Option<Vec<String>>,
pub extend_ignore: Option<Vec<CheckCodePrefix>>,
pub extend_select: Option<Vec<CheckCodePrefix>>,
pub external: Option<Vec<String>>,
pub fix: Option<bool>,
pub fixable: Option<Vec<CheckCodePrefix>>,
pub ignore: Option<Vec<CheckCodePrefix>>,

View File

@@ -138,6 +138,7 @@ mod tests {
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: None,
@@ -175,6 +176,7 @@ line-length = 79
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: None,
@@ -213,6 +215,7 @@ exclude = ["foo.py"]
extend_exclude: None,
select: None,
extend_select: None,
external: None,
ignore: None,
extend_ignore: None,
fixable: None,
@@ -249,6 +252,7 @@ select = ["E501"]
extend_exclude: None,
extend_ignore: None,
extend_select: None,
external: None,
fix: None,
fixable: None,
ignore: None,
@@ -287,6 +291,7 @@ ignore = ["E501"]
extend_exclude: None,
extend_ignore: None,
extend_select: Some(vec![CheckCodePrefix::M001]),
external: None,
fix: None,
fixable: None,
ignore: Some(vec![CheckCodePrefix::E501]),
@@ -369,6 +374,7 @@ other-attribute = 1
]),
select: None,
extend_select: None,
external: None,
ignore: None,
extend_ignore: None,
fixable: None,

View File

@@ -75,102 +75,121 @@ expression: checks
end_location:
row: 19
column: 29
- kind:
UnusedNOQA:
- F841
- V101
location:
row: 22
column: 11
end_location:
row: 22
column: 29
fix:
patch:
content: ""
location:
row: 22
column: 9
end_location:
row: 22
column: 29
- kind:
UnusedNOQA:
- E501
location:
row: 23
row: 26
column: 9
end_location:
row: 23
row: 26
column: 21
fix:
patch:
content: ""
location:
row: 23
row: 26
column: 9
end_location:
row: 23
row: 26
column: 21
- kind:
UnusedVariable: d
location:
row: 26
row: 29
column: 4
end_location:
row: 26
row: 29
column: 5
fix: ~
- kind:
UnusedNOQA:
- E501
location:
row: 26
row: 29
column: 32
end_location:
row: 26
row: 29
column: 44
fix:
patch:
content: ""
location:
row: 26
row: 29
column: 9
end_location:
row: 26
row: 29
column: 44
- kind:
UnusedNOQA:
- F841
location:
row: 52
row: 55
column: 5
end_location:
row: 52
row: 55
column: 23
fix:
patch:
content: "# noqa: E501"
location:
row: 52
row: 55
column: 5
end_location:
row: 52
row: 55
column: 23
- kind:
UnusedNOQA:
- E501
location:
row: 60
row: 63
column: 5
end_location:
row: 60
row: 63
column: 17
fix:
patch:
content: ""
location:
row: 60
row: 63
column: 3
end_location:
row: 60
row: 63
column: 17
- kind:
UnusedNOQA: ~
location:
row: 68
row: 71
column: 5
end_location:
row: 68
row: 71
column: 11
fix:
patch:
content: ""
location:
row: 68
row: 71
column: 3
end_location:
row: 68
row: 71
column: 11