Compare commits

...

5 Commits

Author SHA1 Message Date
Andrew Gallant
0263f2715e Bump version to v0.1.9 (#9231) 2023-12-21 13:19:50 -05:00
Micha Reiser
c6d8076034 Set target versions in Black tests (#9221) 2023-12-21 04:20:17 +00:00
Micha Reiser
8cb7950102 Add target_version to formatter options (#9220) 2023-12-21 04:05:58 +00:00
Micha Reiser
ef4bd8d5ff Fix: Avoid parenthesizing subscript targets and values (#9209) 2023-12-20 23:42:25 +00:00
asafamr-mm
5d41c84ef7 SIM300: CONSTANT_CASE variables are improperly flagged for yoda violation (#9164)
## Summary

fixes #6956 
details in issue

Following an advice in
https://github.com/astral-sh/ruff/issues/6956#issuecomment-1817672585,
this change separates expressions to 3 levels of "constant likelihood":
*  literals, empty dict and tuples... (definitely constant, level 2)
*  CONSTANT_CASE vars (probably constant, 1)
* all other expressions (0)

a comparison is marked yoda if the level is strictly higher on its left
hand side

following
https://github.com/astral-sh/ruff/issues/6956#issuecomment-1697107822
marking compound expressions of literals (e.g. `60 * 60` ) as constants
this change current behaviour on
`SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60)` in the fixture
from error to ok
2023-12-20 20:26:07 +00:00
92 changed files with 1925 additions and 757 deletions

View File

@@ -1,5 +1,61 @@
# Changelog
## 0.1.9
### Breaking changes
- Add site-packages to default exclusions ([#9188](https://github.com/astral-sh/ruff/pull/9188))
### Preview features
- Fix: Avoid parenthesizing subscript targets and values ([#9209](https://github.com/astral-sh/ruff/pull/9209))
- \[`pylint`\] Implement `too-many-locals` (`PLR0914`) ([#9163](https://github.com/astral-sh/ruff/pull/9163))
- Implement `reimplemented_operator` (FURB118) ([#9171](https://github.com/astral-sh/ruff/pull/9171))
- Add a rule to detect string members in runtime-evaluated unions ([#9143](https://github.com/astral-sh/ruff/pull/9143))
- Implement `no_blank_line_before_class_docstring` preview style ([#9154](https://github.com/astral-sh/ruff/pull/9154))
### Rule changes
- `CONSTANT_CASE` variables are improperly flagged for yoda violation (`SIM300`) ([#9164](https://github.com/astral-sh/ruff/pull/9164))
- \[`flake8-pyi`\] Cover ParamSpecs and TypeVarTuples (`PYI018`) ([#9198](https://github.com/astral-sh/ruff/pull/9198))
- \[`flake8-bugbear`\] Add fix for `zip-without-explicit-strict` (`B905`) ([#9176](https://github.com/astral-sh/ruff/pull/9176))
- Add fix to automatically remove `print` and `pprint` statements (`T201`, `T203`) ([#9208](https://github.com/astral-sh/ruff/pull/9208))
- Prefer `Never` to `NoReturn` in auto-typing in Python >= 3.11 (`ANN201`) ([#9213](https://github.com/astral-sh/ruff/pull/9213))
### Formatter
- `can_omit_optional_parentheses`: Exit early for unparenthesized expressions ([#9125](https://github.com/astral-sh/ruff/pull/9125))
- Fix `dynamic` mode with doctests so that it doesn't exceed configured line width ([#9129](https://github.com/astral-sh/ruff/pull/9129))
- Fix `can_omit_optional_parentheses` for expressions with a right most fstring ([#9124](https://github.com/astral-sh/ruff/pull/9124))
- Add `target_version` to formatter options ([#9220](https://github.com/astral-sh/ruff/pull/9220))
### CLI
- Update `ruff format --check` to display message for already formatted files ([#9153](https://github.com/astral-sh/ruff/pull/9153))
### Bug fixes
- Reverse order of arguments for `operator.contains` ([#9192](https://github.com/astral-sh/ruff/pull/9192))
- Iterate over lambdas in deferred type annotations ([#9175](https://github.com/astral-sh/ruff/pull/9175))
- Fix panic in `D208` with multibyte indent ([#9147](https://github.com/astral-sh/ruff/pull/9147))
- Add support for `NoReturn` in auto-return-typing ([#9206](https://github.com/astral-sh/ruff/pull/9206))
- Allow removal of `typing` from `exempt-modules` ([#9214](https://github.com/astral-sh/ruff/pull/9214))
- Avoid `mutable-class-default` violations for Pydantic subclasses ([#9187](https://github.com/astral-sh/ruff/pull/9187))
- Fix dropped union expressions for piped non-types in `PYI055` autofix ([#9161](https://github.com/astral-sh/ruff/pull/9161))
- Enable annotation quoting for multi-line expressions ([#9142](https://github.com/astral-sh/ruff/pull/9142))
- Deduplicate edits when quoting annotations ([#9140](https://github.com/astral-sh/ruff/pull/9140))
- Prevent invalid utf8 indexing in cell magic detection ([#9146](https://github.com/astral-sh/ruff/pull/9146))
- Avoid nested quotations in auto-quoting fix ([#9168](https://github.com/astral-sh/ruff/pull/9168))
- Add base-class inheritance detection to flake8-django rules ([#9151](https://github.com/astral-sh/ruff/pull/9151))
- Avoid `asyncio-dangling-task` violations on shadowed bindings ([#9215](https://github.com/astral-sh/ruff/pull/9215))
### Documentation
- Fix blog post URL in changelog ([#9119](https://github.com/astral-sh/ruff/pull/9119))
- Add error suppression hint for multi-line strings ([#9205](https://github.com/astral-sh/ruff/pull/9205))
- Fix typo in SemanticModel.parent_expression docstring ([#9167](https://github.com/astral-sh/ruff/pull/9167))
- Document link between import sorting and formatter ([#9117](https://github.com/astral-sh/ruff/pull/9117))
## 0.1.8
This release includes opt-in support for formatting Python snippets within

View File

@@ -150,7 +150,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.1.8
rev: v0.1.9
hooks:
# Run the linter.
- id: ruff

View File

@@ -1,6 +1,6 @@
[package]
name = "flake8-to-ruff"
version = "0.1.8"
version = "0.1.9"
description = """
Convert Flake8 configuration files to Ruff configuration files.
"""

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff_cli"
version = "0.1.8"
version = "0.1.9"
publish = false
authors = { workspace = true }
edition = { workspace = true }

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff_linter"
version = "0.1.8"
version = "0.1.9"
publish = false
authors = { workspace = true }
edition = { workspace = true }

View File

@@ -1,6 +1,5 @@
# Errors
"yoda" == compare # SIM300
"yoda" == compare # SIM300
42 == age # SIM300
("a", "b") == compare # SIM300
"yoda" <= compare # SIM300
@@ -13,10 +12,17 @@ YODA > age # SIM300
YODA >= age # SIM300
JediOrder.YODA == age # SIM300
0 < (number - 100) # SIM300
SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
B<A[0][0]or B
B or(B)<A[0][0]
# Errors in preview
['upper'] == UPPER_LIST
{} == DummyHandler.CONFIG
# Errors in stable
UPPER_LIST == ['upper']
DummyHandler.CONFIG == {}
# OK
compare == "yoda"
age == 42
@@ -31,3 +37,6 @@ age <= YODA
YODA == YODA
age == JediOrder.YODA
(number - 100) > 0
SECONDS_IN_DAY == 60 * 60 * 24 # Error in 0.1.8
SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # Error in 0.1.8
{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG

View File

@@ -56,6 +56,7 @@ mod tests {
}
#[test_case(Rule::InDictKeys, Path::new("SIM118.py"))]
#[test_case(Rule::YodaConditions, Path::new("SIM300.py"))]
#[test_case(Rule::IfElseBlockInsteadOfDictGet, Path::new("SIM401.py"))]
#[test_case(Rule::DictGetWithNoneDefault, Path::new("SIM910.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {

View File

@@ -1,3 +1,5 @@
use std::cmp;
use anyhow::Result;
use libcst_native::CompOp;
@@ -14,6 +16,7 @@ use crate::cst::helpers::or_space;
use crate::cst::matchers::{match_comparison, transform_expression};
use crate::fix::edits::pad;
use crate::fix::snippet::SourceCodeSnippet;
use crate::settings::types::PreviewMode;
/// ## What it does
/// Checks for conditions that position a constant on the left-hand side of the
@@ -78,18 +81,65 @@ impl Violation for YodaConditions {
}
}
/// Return `true` if an [`Expr`] is a constant or a constant-like name.
fn is_constant_like(expr: &Expr) -> bool {
match expr {
Expr::Attribute(ast::ExprAttribute { attr, .. }) => str::is_cased_uppercase(attr),
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().all(is_constant_like),
Expr::Name(ast::ExprName { id, .. }) => str::is_cased_uppercase(id),
Expr::UnaryOp(ast::ExprUnaryOp {
op: UnaryOp::UAdd | UnaryOp::USub | UnaryOp::Invert,
operand,
range: _,
}) => operand.is_literal_expr(),
_ => expr.is_literal_expr(),
/// Comparisons left-hand side must not be more [`ConstantLikelihood`] than the right-hand side.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
enum ConstantLikelihood {
/// The expression is unlikely to be a constant (e.g., `foo` or `foo(bar)`).
Unlikely = 0,
/// The expression is likely to be a constant (e.g., `FOO`).
Probably = 1,
/// The expression is definitely a constant (e.g., `42` or `"foo"`).
Definitely = 2,
}
impl ConstantLikelihood {
/// Determine the [`ConstantLikelihood`] of an expression.
fn from_expression(expr: &Expr, preview: PreviewMode) -> Self {
match expr {
_ if expr.is_literal_expr() => ConstantLikelihood::Definitely,
Expr::Attribute(ast::ExprAttribute { attr, .. }) => {
ConstantLikelihood::from_identifier(attr)
}
Expr::Name(ast::ExprName { id, .. }) => ConstantLikelihood::from_identifier(id),
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts
.iter()
.map(|expr| ConstantLikelihood::from_expression(expr, preview))
.min()
.unwrap_or(ConstantLikelihood::Definitely),
Expr::List(ast::ExprList { elts, .. }) if preview.is_enabled() => elts
.iter()
.map(|expr| ConstantLikelihood::from_expression(expr, preview))
.min()
.unwrap_or(ConstantLikelihood::Definitely),
Expr::Dict(ast::ExprDict { values: vs, .. }) if preview.is_enabled() => {
if vs.is_empty() {
ConstantLikelihood::Definitely
} else {
ConstantLikelihood::Probably
}
}
Expr::BinOp(ast::ExprBinOp { left, right, .. }) => cmp::min(
ConstantLikelihood::from_expression(left, preview),
ConstantLikelihood::from_expression(right, preview),
),
Expr::UnaryOp(ast::ExprUnaryOp {
op: UnaryOp::UAdd | UnaryOp::USub | UnaryOp::Invert,
operand,
range: _,
}) => ConstantLikelihood::from_expression(operand, preview),
_ => ConstantLikelihood::Unlikely,
}
}
/// Determine the [`ConstantLikelihood`] of an identifier.
fn from_identifier(identifier: &str) -> Self {
if str::is_cased_uppercase(identifier) {
ConstantLikelihood::Probably
} else {
ConstantLikelihood::Unlikely
}
}
}
@@ -180,7 +230,9 @@ pub(crate) fn yoda_conditions(
return;
}
if !is_constant_like(left) || is_constant_like(right) {
if ConstantLikelihood::from_expression(left, checker.settings.preview)
<= ConstantLikelihood::from_expression(right, checker.settings.preview)
{
return;
}

View File

@@ -6,8 +6,8 @@ SIM300.py:2:1: SIM300 [*] Yoda conditions are discouraged, use `compare == "yoda
1 | # Errors
2 | "yoda" == compare # SIM300
| ^^^^^^^^^^^^^^^^^ SIM300
3 | "yoda" == compare # SIM300
4 | 42 == age # SIM300
3 | 42 == age # SIM300
4 | ("a", "b") == compare # SIM300
|
= help: Replace Yoda condition with `compare == "yoda"`
@@ -15,342 +15,340 @@ SIM300.py:2:1: SIM300 [*] Yoda conditions are discouraged, use `compare == "yoda
1 1 | # Errors
2 |-"yoda" == compare # SIM300
2 |+compare == "yoda" # SIM300
3 3 | "yoda" == compare # SIM300
4 4 | 42 == age # SIM300
5 5 | ("a", "b") == compare # SIM300
3 3 | 42 == age # SIM300
4 4 | ("a", "b") == compare # SIM300
5 5 | "yoda" <= compare # SIM300
SIM300.py:3:1: SIM300 [*] Yoda conditions are discouraged, use `compare == "yoda"` instead
SIM300.py:3:1: SIM300 [*] Yoda conditions are discouraged, use `age == 42` instead
|
1 | # Errors
2 | "yoda" == compare # SIM300
3 | "yoda" == compare # SIM300
| ^^^^^^^^^^^^^^^^^ SIM300
4 | 42 == age # SIM300
5 | ("a", "b") == compare # SIM300
|
= help: Replace Yoda condition with `compare == "yoda"`
Safe fix
1 1 | # Errors
2 2 | "yoda" == compare # SIM300
3 |-"yoda" == compare # SIM300
3 |+compare == "yoda" # SIM300
4 4 | 42 == age # SIM300
5 5 | ("a", "b") == compare # SIM300
6 6 | "yoda" <= compare # SIM300
SIM300.py:4:1: SIM300 [*] Yoda conditions are discouraged, use `age == 42` instead
|
2 | "yoda" == compare # SIM300
3 | "yoda" == compare # SIM300
4 | 42 == age # SIM300
3 | 42 == age # SIM300
| ^^^^^^^^^ SIM300
5 | ("a", "b") == compare # SIM300
6 | "yoda" <= compare # SIM300
4 | ("a", "b") == compare # SIM300
5 | "yoda" <= compare # SIM300
|
= help: Replace Yoda condition with `age == 42`
Safe fix
1 1 | # Errors
2 2 | "yoda" == compare # SIM300
3 3 | "yoda" == compare # SIM300
4 |-42 == age # SIM300
4 |+age == 42 # SIM300
5 5 | ("a", "b") == compare # SIM300
6 6 | "yoda" <= compare # SIM300
7 7 | "yoda" < compare # SIM300
3 |-42 == age # SIM300
3 |+age == 42 # SIM300
4 4 | ("a", "b") == compare # SIM300
5 5 | "yoda" <= compare # SIM300
6 6 | "yoda" < compare # SIM300
SIM300.py:5:1: SIM300 [*] Yoda conditions are discouraged, use `compare == ("a", "b")` instead
SIM300.py:4:1: SIM300 [*] Yoda conditions are discouraged, use `compare == ("a", "b")` instead
|
3 | "yoda" == compare # SIM300
4 | 42 == age # SIM300
5 | ("a", "b") == compare # SIM300
2 | "yoda" == compare # SIM300
3 | 42 == age # SIM300
4 | ("a", "b") == compare # SIM300
| ^^^^^^^^^^^^^^^^^^^^^ SIM300
6 | "yoda" <= compare # SIM300
7 | "yoda" < compare # SIM300
5 | "yoda" <= compare # SIM300
6 | "yoda" < compare # SIM300
|
= help: Replace Yoda condition with `compare == ("a", "b")`
Safe fix
1 1 | # Errors
2 2 | "yoda" == compare # SIM300
3 3 | "yoda" == compare # SIM300
4 4 | 42 == age # SIM300
5 |-("a", "b") == compare # SIM300
5 |+compare == ("a", "b") # SIM300
6 6 | "yoda" <= compare # SIM300
7 7 | "yoda" < compare # SIM300
8 8 | 42 > age # SIM300
3 3 | 42 == age # SIM300
4 |-("a", "b") == compare # SIM300
4 |+compare == ("a", "b") # SIM300
5 5 | "yoda" <= compare # SIM300
6 6 | "yoda" < compare # SIM300
7 7 | 42 > age # SIM300
SIM300.py:6:1: SIM300 [*] Yoda conditions are discouraged, use `compare >= "yoda"` instead
SIM300.py:5:1: SIM300 [*] Yoda conditions are discouraged, use `compare >= "yoda"` instead
|
4 | 42 == age # SIM300
5 | ("a", "b") == compare # SIM300
6 | "yoda" <= compare # SIM300
3 | 42 == age # SIM300
4 | ("a", "b") == compare # SIM300
5 | "yoda" <= compare # SIM300
| ^^^^^^^^^^^^^^^^^ SIM300
7 | "yoda" < compare # SIM300
8 | 42 > age # SIM300
6 | "yoda" < compare # SIM300
7 | 42 > age # SIM300
|
= help: Replace Yoda condition with `compare >= "yoda"`
Safe fix
3 3 | "yoda" == compare # SIM300
4 4 | 42 == age # SIM300
5 5 | ("a", "b") == compare # SIM300
6 |-"yoda" <= compare # SIM300
6 |+compare >= "yoda" # SIM300
7 7 | "yoda" < compare # SIM300
8 8 | 42 > age # SIM300
9 9 | -42 > age # SIM300
2 2 | "yoda" == compare # SIM300
3 3 | 42 == age # SIM300
4 4 | ("a", "b") == compare # SIM300
5 |-"yoda" <= compare # SIM300
5 |+compare >= "yoda" # SIM300
6 6 | "yoda" < compare # SIM300
7 7 | 42 > age # SIM300
8 8 | -42 > age # SIM300
SIM300.py:7:1: SIM300 [*] Yoda conditions are discouraged, use `compare > "yoda"` instead
SIM300.py:6:1: SIM300 [*] Yoda conditions are discouraged, use `compare > "yoda"` instead
|
5 | ("a", "b") == compare # SIM300
6 | "yoda" <= compare # SIM300
7 | "yoda" < compare # SIM300
4 | ("a", "b") == compare # SIM300
5 | "yoda" <= compare # SIM300
6 | "yoda" < compare # SIM300
| ^^^^^^^^^^^^^^^^ SIM300
8 | 42 > age # SIM300
9 | -42 > age # SIM300
7 | 42 > age # SIM300
8 | -42 > age # SIM300
|
= help: Replace Yoda condition with `compare > "yoda"`
Safe fix
4 4 | 42 == age # SIM300
5 5 | ("a", "b") == compare # SIM300
6 6 | "yoda" <= compare # SIM300
7 |-"yoda" < compare # SIM300
7 |+compare > "yoda" # SIM300
8 8 | 42 > age # SIM300
9 9 | -42 > age # SIM300
10 10 | +42 > age # SIM300
3 3 | 42 == age # SIM300
4 4 | ("a", "b") == compare # SIM300
5 5 | "yoda" <= compare # SIM300
6 |-"yoda" < compare # SIM300
6 |+compare > "yoda" # SIM300
7 7 | 42 > age # SIM300
8 8 | -42 > age # SIM300
9 9 | +42 > age # SIM300
SIM300.py:8:1: SIM300 [*] Yoda conditions are discouraged, use `age < 42` instead
|
6 | "yoda" <= compare # SIM300
7 | "yoda" < compare # SIM300
8 | 42 > age # SIM300
| ^^^^^^^^ SIM300
9 | -42 > age # SIM300
10 | +42 > age # SIM300
|
= help: Replace Yoda condition with `age < 42`
SIM300.py:7:1: SIM300 [*] Yoda conditions are discouraged, use `age < 42` instead
|
5 | "yoda" <= compare # SIM300
6 | "yoda" < compare # SIM300
7 | 42 > age # SIM300
| ^^^^^^^^ SIM300
8 | -42 > age # SIM300
9 | +42 > age # SIM300
|
= help: Replace Yoda condition with `age < 42`
Safe fix
5 5 | ("a", "b") == compare # SIM300
6 6 | "yoda" <= compare # SIM300
7 7 | "yoda" < compare # SIM300
8 |-42 > age # SIM300
8 |+age < 42 # SIM300
9 9 | -42 > age # SIM300
10 10 | +42 > age # SIM300
11 11 | YODA == age # SIM300
4 4 | ("a", "b") == compare # SIM300
5 5 | "yoda" <= compare # SIM300
6 6 | "yoda" < compare # SIM300
7 |-42 > age # SIM300
7 |+age < 42 # SIM300
8 8 | -42 > age # SIM300
9 9 | +42 > age # SIM300
10 10 | YODA == age # SIM300
SIM300.py:9:1: SIM300 [*] Yoda conditions are discouraged, use `age < -42` instead
SIM300.py:8:1: SIM300 [*] Yoda conditions are discouraged, use `age < -42` instead
|
7 | "yoda" < compare # SIM300
8 | 42 > age # SIM300
9 | -42 > age # SIM300
6 | "yoda" < compare # SIM300
7 | 42 > age # SIM300
8 | -42 > age # SIM300
| ^^^^^^^^^ SIM300
10 | +42 > age # SIM300
11 | YODA == age # SIM300
9 | +42 > age # SIM300
10 | YODA == age # SIM300
|
= help: Replace Yoda condition with `age < -42`
Safe fix
6 6 | "yoda" <= compare # SIM300
7 7 | "yoda" < compare # SIM300
8 8 | 42 > age # SIM300
9 |--42 > age # SIM300
9 |+age < -42 # SIM300
10 10 | +42 > age # SIM300
11 11 | YODA == age # SIM300
12 12 | YODA > age # SIM300
5 5 | "yoda" <= compare # SIM300
6 6 | "yoda" < compare # SIM300
7 7 | 42 > age # SIM300
8 |--42 > age # SIM300
8 |+age < -42 # SIM300
9 9 | +42 > age # SIM300
10 10 | YODA == age # SIM300
11 11 | YODA > age # SIM300
SIM300.py:10:1: SIM300 [*] Yoda conditions are discouraged, use `age < +42` instead
SIM300.py:9:1: SIM300 [*] Yoda conditions are discouraged, use `age < +42` instead
|
8 | 42 > age # SIM300
9 | -42 > age # SIM300
10 | +42 > age # SIM300
7 | 42 > age # SIM300
8 | -42 > age # SIM300
9 | +42 > age # SIM300
| ^^^^^^^^^ SIM300
11 | YODA == age # SIM300
12 | YODA > age # SIM300
10 | YODA == age # SIM300
11 | YODA > age # SIM300
|
= help: Replace Yoda condition with `age < +42`
Safe fix
7 7 | "yoda" < compare # SIM300
8 8 | 42 > age # SIM300
9 9 | -42 > age # SIM300
10 |-+42 > age # SIM300
10 |+age < +42 # SIM300
11 11 | YODA == age # SIM300
12 12 | YODA > age # SIM300
13 13 | YODA >= age # SIM300
6 6 | "yoda" < compare # SIM300
7 7 | 42 > age # SIM300
8 8 | -42 > age # SIM300
9 |-+42 > age # SIM300
9 |+age < +42 # SIM300
10 10 | YODA == age # SIM300
11 11 | YODA > age # SIM300
12 12 | YODA >= age # SIM300
SIM300.py:11:1: SIM300 [*] Yoda conditions are discouraged, use `age == YODA` instead
SIM300.py:10:1: SIM300 [*] Yoda conditions are discouraged, use `age == YODA` instead
|
9 | -42 > age # SIM300
10 | +42 > age # SIM300
11 | YODA == age # SIM300
8 | -42 > age # SIM300
9 | +42 > age # SIM300
10 | YODA == age # SIM300
| ^^^^^^^^^^^ SIM300
12 | YODA > age # SIM300
13 | YODA >= age # SIM300
11 | YODA > age # SIM300
12 | YODA >= age # SIM300
|
= help: Replace Yoda condition with `age == YODA`
Safe fix
8 8 | 42 > age # SIM300
9 9 | -42 > age # SIM300
10 10 | +42 > age # SIM300
11 |-YODA == age # SIM300
11 |+age == YODA # SIM300
12 12 | YODA > age # SIM300
13 13 | YODA >= age # SIM300
14 14 | JediOrder.YODA == age # SIM300
7 7 | 42 > age # SIM300
8 8 | -42 > age # SIM300
9 9 | +42 > age # SIM300
10 |-YODA == age # SIM300
10 |+age == YODA # SIM300
11 11 | YODA > age # SIM300
12 12 | YODA >= age # SIM300
13 13 | JediOrder.YODA == age # SIM300
SIM300.py:12:1: SIM300 [*] Yoda conditions are discouraged, use `age < YODA` instead
SIM300.py:11:1: SIM300 [*] Yoda conditions are discouraged, use `age < YODA` instead
|
10 | +42 > age # SIM300
11 | YODA == age # SIM300
12 | YODA > age # SIM300
9 | +42 > age # SIM300
10 | YODA == age # SIM300
11 | YODA > age # SIM300
| ^^^^^^^^^^ SIM300
13 | YODA >= age # SIM300
14 | JediOrder.YODA == age # SIM300
12 | YODA >= age # SIM300
13 | JediOrder.YODA == age # SIM300
|
= help: Replace Yoda condition with `age < YODA`
Safe fix
9 9 | -42 > age # SIM300
10 10 | +42 > age # SIM300
11 11 | YODA == age # SIM300
12 |-YODA > age # SIM300
12 |+age < YODA # SIM300
13 13 | YODA >= age # SIM300
14 14 | JediOrder.YODA == age # SIM300
15 15 | 0 < (number - 100) # SIM300
8 8 | -42 > age # SIM300
9 9 | +42 > age # SIM300
10 10 | YODA == age # SIM300
11 |-YODA > age # SIM300
11 |+age < YODA # SIM300
12 12 | YODA >= age # SIM300
13 13 | JediOrder.YODA == age # SIM300
14 14 | 0 < (number - 100) # SIM300
SIM300.py:13:1: SIM300 [*] Yoda conditions are discouraged, use `age <= YODA` instead
SIM300.py:12:1: SIM300 [*] Yoda conditions are discouraged, use `age <= YODA` instead
|
11 | YODA == age # SIM300
12 | YODA > age # SIM300
13 | YODA >= age # SIM300
10 | YODA == age # SIM300
11 | YODA > age # SIM300
12 | YODA >= age # SIM300
| ^^^^^^^^^^^ SIM300
14 | JediOrder.YODA == age # SIM300
15 | 0 < (number - 100) # SIM300
13 | JediOrder.YODA == age # SIM300
14 | 0 < (number - 100) # SIM300
|
= help: Replace Yoda condition with `age <= YODA`
Safe fix
10 10 | +42 > age # SIM300
11 11 | YODA == age # SIM300
12 12 | YODA > age # SIM300
13 |-YODA >= age # SIM300
13 |+age <= YODA # SIM300
14 14 | JediOrder.YODA == age # SIM300
15 15 | 0 < (number - 100) # SIM300
16 16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
9 9 | +42 > age # SIM300
10 10 | YODA == age # SIM300
11 11 | YODA > age # SIM300
12 |-YODA >= age # SIM300
12 |+age <= YODA # SIM300
13 13 | JediOrder.YODA == age # SIM300
14 14 | 0 < (number - 100) # SIM300
15 15 | B<A[0][0]or B
SIM300.py:14:1: SIM300 [*] Yoda conditions are discouraged, use `age == JediOrder.YODA` instead
SIM300.py:13:1: SIM300 [*] Yoda conditions are discouraged, use `age == JediOrder.YODA` instead
|
12 | YODA > age # SIM300
13 | YODA >= age # SIM300
14 | JediOrder.YODA == age # SIM300
11 | YODA > age # SIM300
12 | YODA >= age # SIM300
13 | JediOrder.YODA == age # SIM300
| ^^^^^^^^^^^^^^^^^^^^^ SIM300
15 | 0 < (number - 100) # SIM300
16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
14 | 0 < (number - 100) # SIM300
15 | B<A[0][0]or B
|
= help: Replace Yoda condition with `age == JediOrder.YODA`
Safe fix
11 11 | YODA == age # SIM300
12 12 | YODA > age # SIM300
13 13 | YODA >= age # SIM300
14 |-JediOrder.YODA == age # SIM300
14 |+age == JediOrder.YODA # SIM300
15 15 | 0 < (number - 100) # SIM300
16 16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
17 17 | B<A[0][0]or B
10 10 | YODA == age # SIM300
11 11 | YODA > age # SIM300
12 12 | YODA >= age # SIM300
13 |-JediOrder.YODA == age # SIM300
13 |+age == JediOrder.YODA # SIM300
14 14 | 0 < (number - 100) # SIM300
15 15 | B<A[0][0]or B
16 16 | B or(B)<A[0][0]
SIM300.py:15:1: SIM300 [*] Yoda conditions are discouraged, use `(number - 100) > 0` instead
SIM300.py:14:1: SIM300 [*] Yoda conditions are discouraged, use `(number - 100) > 0` instead
|
13 | YODA >= age # SIM300
14 | JediOrder.YODA == age # SIM300
15 | 0 < (number - 100) # SIM300
12 | YODA >= age # SIM300
13 | JediOrder.YODA == age # SIM300
14 | 0 < (number - 100) # SIM300
| ^^^^^^^^^^^^^^^^^^ SIM300
16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
17 | B<A[0][0]or B
15 | B<A[0][0]or B
16 | B or(B)<A[0][0]
|
= help: Replace Yoda condition with `(number - 100) > 0`
Safe fix
12 12 | YODA > age # SIM300
13 13 | YODA >= age # SIM300
14 14 | JediOrder.YODA == age # SIM300
15 |-0 < (number - 100) # SIM300
15 |+(number - 100) > 0 # SIM300
16 16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
17 17 | B<A[0][0]or B
18 18 | B or(B)<A[0][0]
11 11 | YODA > age # SIM300
12 12 | YODA >= age # SIM300
13 13 | JediOrder.YODA == age # SIM300
14 |-0 < (number - 100) # SIM300
14 |+(number - 100) > 0 # SIM300
15 15 | B<A[0][0]or B
16 16 | B or(B)<A[0][0]
17 17 |
SIM300.py:16:1: SIM300 [*] Yoda conditions are discouraged
SIM300.py:15:1: SIM300 [*] Yoda conditions are discouraged, use `A[0][0] > B` instead
|
14 | JediOrder.YODA == age # SIM300
15 | 0 < (number - 100) # SIM300
16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM300
17 | B<A[0][0]or B
18 | B or(B)<A[0][0]
|
= help: Replace Yoda condition
Safe fix
13 13 | YODA >= age # SIM300
14 14 | JediOrder.YODA == age # SIM300
15 15 | 0 < (number - 100) # SIM300
16 |-SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
16 |+(60 * 60) < SomeClass().settings.SOME_CONSTANT_VALUE # SIM300
17 17 | B<A[0][0]or B
18 18 | B or(B)<A[0][0]
19 19 |
SIM300.py:17:1: SIM300 [*] Yoda conditions are discouraged, use `A[0][0] > B` instead
|
15 | 0 < (number - 100) # SIM300
16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
17 | B<A[0][0]or B
13 | JediOrder.YODA == age # SIM300
14 | 0 < (number - 100) # SIM300
15 | B<A[0][0]or B
| ^^^^^^^^^ SIM300
18 | B or(B)<A[0][0]
16 | B or(B)<A[0][0]
|
= help: Replace Yoda condition with `A[0][0] > B`
Safe fix
14 14 | JediOrder.YODA == age # SIM300
15 15 | 0 < (number - 100) # SIM300
16 16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
17 |-B<A[0][0]or B
17 |+A[0][0] > B or B
18 18 | B or(B)<A[0][0]
19 19 |
20 20 | # OK
12 12 | YODA >= age # SIM300
13 13 | JediOrder.YODA == age # SIM300
14 14 | 0 < (number - 100) # SIM300
15 |-B<A[0][0]or B
15 |+A[0][0] > B or B
16 16 | B or(B)<A[0][0]
17 17 |
18 18 | # Errors in preview
SIM300.py:18:5: SIM300 [*] Yoda conditions are discouraged, use `A[0][0] > (B)` instead
SIM300.py:16:5: SIM300 [*] Yoda conditions are discouraged, use `A[0][0] > (B)` instead
|
16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
17 | B<A[0][0]or B
18 | B or(B)<A[0][0]
14 | 0 < (number - 100) # SIM300
15 | B<A[0][0]or B
16 | B or(B)<A[0][0]
| ^^^^^^^^^^^ SIM300
19 |
20 | # OK
17 |
18 | # Errors in preview
|
= help: Replace Yoda condition with `A[0][0] > (B)`
Safe fix
15 15 | 0 < (number - 100) # SIM300
16 16 | SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # SIM300
17 17 | B<A[0][0]or B
18 |-B or(B)<A[0][0]
18 |+B or A[0][0] > (B)
19 19 |
20 20 | # OK
21 21 | compare == "yoda"
13 13 | JediOrder.YODA == age # SIM300
14 14 | 0 < (number - 100) # SIM300
15 15 | B<A[0][0]or B
16 |-B or(B)<A[0][0]
16 |+B or A[0][0] > (B)
17 17 |
18 18 | # Errors in preview
19 19 | ['upper'] == UPPER_LIST
SIM300.py:23:1: SIM300 [*] Yoda conditions are discouraged, use `['upper'] == UPPER_LIST` instead
|
22 | # Errors in stable
23 | UPPER_LIST == ['upper']
| ^^^^^^^^^^^^^^^^^^^^^^^ SIM300
24 | DummyHandler.CONFIG == {}
|
= help: Replace Yoda condition with `['upper'] == UPPER_LIST`
Safe fix
20 20 | {} == DummyHandler.CONFIG
21 21 |
22 22 | # Errors in stable
23 |-UPPER_LIST == ['upper']
23 |+['upper'] == UPPER_LIST
24 24 | DummyHandler.CONFIG == {}
25 25 |
26 26 | # OK
SIM300.py:24:1: SIM300 [*] Yoda conditions are discouraged, use `{} == DummyHandler.CONFIG` instead
|
22 | # Errors in stable
23 | UPPER_LIST == ['upper']
24 | DummyHandler.CONFIG == {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ SIM300
25 |
26 | # OK
|
= help: Replace Yoda condition with `{} == DummyHandler.CONFIG`
Safe fix
21 21 |
22 22 | # Errors in stable
23 23 | UPPER_LIST == ['upper']
24 |-DummyHandler.CONFIG == {}
24 |+{} == DummyHandler.CONFIG
25 25 |
26 26 | # OK
27 27 | compare == "yoda"

View File

@@ -0,0 +1,354 @@
---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
SIM300.py:2:1: SIM300 [*] Yoda conditions are discouraged, use `compare == "yoda"` instead
|
1 | # Errors
2 | "yoda" == compare # SIM300
| ^^^^^^^^^^^^^^^^^ SIM300
3 | 42 == age # SIM300
4 | ("a", "b") == compare # SIM300
|
= help: Replace Yoda condition with `compare == "yoda"`
Safe fix
1 1 | # Errors
2 |-"yoda" == compare # SIM300
2 |+compare == "yoda" # SIM300
3 3 | 42 == age # SIM300
4 4 | ("a", "b") == compare # SIM300
5 5 | "yoda" <= compare # SIM300
SIM300.py:3:1: SIM300 [*] Yoda conditions are discouraged, use `age == 42` instead
|
1 | # Errors
2 | "yoda" == compare # SIM300
3 | 42 == age # SIM300
| ^^^^^^^^^ SIM300
4 | ("a", "b") == compare # SIM300
5 | "yoda" <= compare # SIM300
|
= help: Replace Yoda condition with `age == 42`
Safe fix
1 1 | # Errors
2 2 | "yoda" == compare # SIM300
3 |-42 == age # SIM300
3 |+age == 42 # SIM300
4 4 | ("a", "b") == compare # SIM300
5 5 | "yoda" <= compare # SIM300
6 6 | "yoda" < compare # SIM300
SIM300.py:4:1: SIM300 [*] Yoda conditions are discouraged, use `compare == ("a", "b")` instead
|
2 | "yoda" == compare # SIM300
3 | 42 == age # SIM300
4 | ("a", "b") == compare # SIM300
| ^^^^^^^^^^^^^^^^^^^^^ SIM300
5 | "yoda" <= compare # SIM300
6 | "yoda" < compare # SIM300
|
= help: Replace Yoda condition with `compare == ("a", "b")`
Safe fix
1 1 | # Errors
2 2 | "yoda" == compare # SIM300
3 3 | 42 == age # SIM300
4 |-("a", "b") == compare # SIM300
4 |+compare == ("a", "b") # SIM300
5 5 | "yoda" <= compare # SIM300
6 6 | "yoda" < compare # SIM300
7 7 | 42 > age # SIM300
SIM300.py:5:1: SIM300 [*] Yoda conditions are discouraged, use `compare >= "yoda"` instead
|
3 | 42 == age # SIM300
4 | ("a", "b") == compare # SIM300
5 | "yoda" <= compare # SIM300
| ^^^^^^^^^^^^^^^^^ SIM300
6 | "yoda" < compare # SIM300
7 | 42 > age # SIM300
|
= help: Replace Yoda condition with `compare >= "yoda"`
Safe fix
2 2 | "yoda" == compare # SIM300
3 3 | 42 == age # SIM300
4 4 | ("a", "b") == compare # SIM300
5 |-"yoda" <= compare # SIM300
5 |+compare >= "yoda" # SIM300
6 6 | "yoda" < compare # SIM300
7 7 | 42 > age # SIM300
8 8 | -42 > age # SIM300
SIM300.py:6:1: SIM300 [*] Yoda conditions are discouraged, use `compare > "yoda"` instead
|
4 | ("a", "b") == compare # SIM300
5 | "yoda" <= compare # SIM300
6 | "yoda" < compare # SIM300
| ^^^^^^^^^^^^^^^^ SIM300
7 | 42 > age # SIM300
8 | -42 > age # SIM300
|
= help: Replace Yoda condition with `compare > "yoda"`
Safe fix
3 3 | 42 == age # SIM300
4 4 | ("a", "b") == compare # SIM300
5 5 | "yoda" <= compare # SIM300
6 |-"yoda" < compare # SIM300
6 |+compare > "yoda" # SIM300
7 7 | 42 > age # SIM300
8 8 | -42 > age # SIM300
9 9 | +42 > age # SIM300
SIM300.py:7:1: SIM300 [*] Yoda conditions are discouraged, use `age < 42` instead
|
5 | "yoda" <= compare # SIM300
6 | "yoda" < compare # SIM300
7 | 42 > age # SIM300
| ^^^^^^^^ SIM300
8 | -42 > age # SIM300
9 | +42 > age # SIM300
|
= help: Replace Yoda condition with `age < 42`
Safe fix
4 4 | ("a", "b") == compare # SIM300
5 5 | "yoda" <= compare # SIM300
6 6 | "yoda" < compare # SIM300
7 |-42 > age # SIM300
7 |+age < 42 # SIM300
8 8 | -42 > age # SIM300
9 9 | +42 > age # SIM300
10 10 | YODA == age # SIM300
SIM300.py:8:1: SIM300 [*] Yoda conditions are discouraged, use `age < -42` instead
|
6 | "yoda" < compare # SIM300
7 | 42 > age # SIM300
8 | -42 > age # SIM300
| ^^^^^^^^^ SIM300
9 | +42 > age # SIM300
10 | YODA == age # SIM300
|
= help: Replace Yoda condition with `age < -42`
Safe fix
5 5 | "yoda" <= compare # SIM300
6 6 | "yoda" < compare # SIM300
7 7 | 42 > age # SIM300
8 |--42 > age # SIM300
8 |+age < -42 # SIM300
9 9 | +42 > age # SIM300
10 10 | YODA == age # SIM300
11 11 | YODA > age # SIM300
SIM300.py:9:1: SIM300 [*] Yoda conditions are discouraged, use `age < +42` instead
|
7 | 42 > age # SIM300
8 | -42 > age # SIM300
9 | +42 > age # SIM300
| ^^^^^^^^^ SIM300
10 | YODA == age # SIM300
11 | YODA > age # SIM300
|
= help: Replace Yoda condition with `age < +42`
Safe fix
6 6 | "yoda" < compare # SIM300
7 7 | 42 > age # SIM300
8 8 | -42 > age # SIM300
9 |-+42 > age # SIM300
9 |+age < +42 # SIM300
10 10 | YODA == age # SIM300
11 11 | YODA > age # SIM300
12 12 | YODA >= age # SIM300
SIM300.py:10:1: SIM300 [*] Yoda conditions are discouraged, use `age == YODA` instead
|
8 | -42 > age # SIM300
9 | +42 > age # SIM300
10 | YODA == age # SIM300
| ^^^^^^^^^^^ SIM300
11 | YODA > age # SIM300
12 | YODA >= age # SIM300
|
= help: Replace Yoda condition with `age == YODA`
Safe fix
7 7 | 42 > age # SIM300
8 8 | -42 > age # SIM300
9 9 | +42 > age # SIM300
10 |-YODA == age # SIM300
10 |+age == YODA # SIM300
11 11 | YODA > age # SIM300
12 12 | YODA >= age # SIM300
13 13 | JediOrder.YODA == age # SIM300
SIM300.py:11:1: SIM300 [*] Yoda conditions are discouraged, use `age < YODA` instead
|
9 | +42 > age # SIM300
10 | YODA == age # SIM300
11 | YODA > age # SIM300
| ^^^^^^^^^^ SIM300
12 | YODA >= age # SIM300
13 | JediOrder.YODA == age # SIM300
|
= help: Replace Yoda condition with `age < YODA`
Safe fix
8 8 | -42 > age # SIM300
9 9 | +42 > age # SIM300
10 10 | YODA == age # SIM300
11 |-YODA > age # SIM300
11 |+age < YODA # SIM300
12 12 | YODA >= age # SIM300
13 13 | JediOrder.YODA == age # SIM300
14 14 | 0 < (number - 100) # SIM300
SIM300.py:12:1: SIM300 [*] Yoda conditions are discouraged, use `age <= YODA` instead
|
10 | YODA == age # SIM300
11 | YODA > age # SIM300
12 | YODA >= age # SIM300
| ^^^^^^^^^^^ SIM300
13 | JediOrder.YODA == age # SIM300
14 | 0 < (number - 100) # SIM300
|
= help: Replace Yoda condition with `age <= YODA`
Safe fix
9 9 | +42 > age # SIM300
10 10 | YODA == age # SIM300
11 11 | YODA > age # SIM300
12 |-YODA >= age # SIM300
12 |+age <= YODA # SIM300
13 13 | JediOrder.YODA == age # SIM300
14 14 | 0 < (number - 100) # SIM300
15 15 | B<A[0][0]or B
SIM300.py:13:1: SIM300 [*] Yoda conditions are discouraged, use `age == JediOrder.YODA` instead
|
11 | YODA > age # SIM300
12 | YODA >= age # SIM300
13 | JediOrder.YODA == age # SIM300
| ^^^^^^^^^^^^^^^^^^^^^ SIM300
14 | 0 < (number - 100) # SIM300
15 | B<A[0][0]or B
|
= help: Replace Yoda condition with `age == JediOrder.YODA`
Safe fix
10 10 | YODA == age # SIM300
11 11 | YODA > age # SIM300
12 12 | YODA >= age # SIM300
13 |-JediOrder.YODA == age # SIM300
13 |+age == JediOrder.YODA # SIM300
14 14 | 0 < (number - 100) # SIM300
15 15 | B<A[0][0]or B
16 16 | B or(B)<A[0][0]
SIM300.py:14:1: SIM300 [*] Yoda conditions are discouraged, use `(number - 100) > 0` instead
|
12 | YODA >= age # SIM300
13 | JediOrder.YODA == age # SIM300
14 | 0 < (number - 100) # SIM300
| ^^^^^^^^^^^^^^^^^^ SIM300
15 | B<A[0][0]or B
16 | B or(B)<A[0][0]
|
= help: Replace Yoda condition with `(number - 100) > 0`
Safe fix
11 11 | YODA > age # SIM300
12 12 | YODA >= age # SIM300
13 13 | JediOrder.YODA == age # SIM300
14 |-0 < (number - 100) # SIM300
14 |+(number - 100) > 0 # SIM300
15 15 | B<A[0][0]or B
16 16 | B or(B)<A[0][0]
17 17 |
SIM300.py:15:1: SIM300 [*] Yoda conditions are discouraged, use `A[0][0] > B` instead
|
13 | JediOrder.YODA == age # SIM300
14 | 0 < (number - 100) # SIM300
15 | B<A[0][0]or B
| ^^^^^^^^^ SIM300
16 | B or(B)<A[0][0]
|
= help: Replace Yoda condition with `A[0][0] > B`
Safe fix
12 12 | YODA >= age # SIM300
13 13 | JediOrder.YODA == age # SIM300
14 14 | 0 < (number - 100) # SIM300
15 |-B<A[0][0]or B
15 |+A[0][0] > B or B
16 16 | B or(B)<A[0][0]
17 17 |
18 18 | # Errors in preview
SIM300.py:16:5: SIM300 [*] Yoda conditions are discouraged, use `A[0][0] > (B)` instead
|
14 | 0 < (number - 100) # SIM300
15 | B<A[0][0]or B
16 | B or(B)<A[0][0]
| ^^^^^^^^^^^ SIM300
17 |
18 | # Errors in preview
|
= help: Replace Yoda condition with `A[0][0] > (B)`
Safe fix
13 13 | JediOrder.YODA == age # SIM300
14 14 | 0 < (number - 100) # SIM300
15 15 | B<A[0][0]or B
16 |-B or(B)<A[0][0]
16 |+B or A[0][0] > (B)
17 17 |
18 18 | # Errors in preview
19 19 | ['upper'] == UPPER_LIST
SIM300.py:19:1: SIM300 [*] Yoda conditions are discouraged, use `UPPER_LIST == ['upper']` instead
|
18 | # Errors in preview
19 | ['upper'] == UPPER_LIST
| ^^^^^^^^^^^^^^^^^^^^^^^ SIM300
20 | {} == DummyHandler.CONFIG
|
= help: Replace Yoda condition with `UPPER_LIST == ['upper']`
Safe fix
16 16 | B or(B)<A[0][0]
17 17 |
18 18 | # Errors in preview
19 |-['upper'] == UPPER_LIST
19 |+UPPER_LIST == ['upper']
20 20 | {} == DummyHandler.CONFIG
21 21 |
22 22 | # Errors in stable
SIM300.py:20:1: SIM300 [*] Yoda conditions are discouraged, use `DummyHandler.CONFIG == {}` instead
|
18 | # Errors in preview
19 | ['upper'] == UPPER_LIST
20 | {} == DummyHandler.CONFIG
| ^^^^^^^^^^^^^^^^^^^^^^^^^ SIM300
21 |
22 | # Errors in stable
|
= help: Replace Yoda condition with `DummyHandler.CONFIG == {}`
Safe fix
17 17 |
18 18 | # Errors in preview
19 19 | ['upper'] == UPPER_LIST
20 |-{} == DummyHandler.CONFIG
20 |+DummyHandler.CONFIG == {}
21 21 |
22 22 | # Errors in stable
23 23 | UPPER_LIST == ['upper']

View File

@@ -40,6 +40,8 @@ use crate::rule_selector::RuleSelector;
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum PythonVersion {
Py37,
// Make sure to also change the default for `ruff_python_formatter::PythonVersion`
// when changing the default here.
#[default]
Py38,
Py39,

View File

@@ -0,0 +1,2 @@
# split out from comments2 as it does not work with line-length=1, losing the comment
a = "type comment with trailing space" # type: str

View File

@@ -0,0 +1,2 @@
# split out from comments2 as it does not work with line-length=1, losing the comment
a = "type comment with trailing space" # type: str

View File

@@ -155,8 +155,6 @@ class Test:
pass
a = "type comment with trailing space" # type: str
#######################
### SECTION COMMENT ###
#######################

View File

@@ -162,8 +162,6 @@ class Test:
pass
a = "type comment with trailing space" # type: str
#######################
### SECTION COMMENT ###
#######################

View File

@@ -1,3 +1,4 @@
# l2 loses the comment with line-length=1 in preview mode
l1 = ["This list should be broken up", "into multiple lines", "because it is way too long"]
l2 = ["But this list shouldn't", "even though it also has", "way too many characters in it"] # fmt: skip
l3 = ["I have", "trailing comma", "so I should be braked",]

View File

@@ -1,3 +1,4 @@
# l2 loses the comment with line-length=1 in preview mode
l1 = [
"This list should be broken up",
"into multiple lines",

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py310"}

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py38"}

View File

@@ -0,0 +1 @@
{"target_version": "py38"}

View File

@@ -0,0 +1 @@
{"target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py39"}

View File

@@ -0,0 +1 @@
{"target_version": "py38"}

View File

@@ -0,0 +1 @@
{"target_version": "py311"}

View File

@@ -0,0 +1 @@
{"target_version": "py311"}

View File

@@ -0,0 +1 @@
{"target_version": "py311"}

View File

@@ -0,0 +1 @@
{"preview": "enabled"}

View File

@@ -0,0 +1,62 @@
def foo():
"""
Docstring
"""
# Here we go
if x:
# This is also now fine
a = 123
else:
# But not necessary
a = 123
if y:
while True:
"""
Long comment here
"""
a = 123
if z:
for _ in range(100):
a = 123
else:
try:
# this should be ok
a = 123
except:
"""also this"""
a = 123
def bar():
if x:
a = 123
def baz():
# OK
if x:
a = 123
def quux():
new_line = here
class Cls:
def method(self):
pass

View File

@@ -0,0 +1,62 @@
def foo():
"""
Docstring
"""
# Here we go
if x:
# This is also now fine
a = 123
else:
# But not necessary
a = 123
if y:
while True:
"""
Long comment here
"""
a = 123
if z:
for _ in range(100):
a = 123
else:
try:
# this should be ok
a = 123
except:
"""also this"""
a = 123
def bar():
if x:
a = 123
def baz():
# OK
if x:
a = 123
def quux():
new_line = here
class Cls:
def method(self):
pass

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py38"}

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py39"}

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py310"}

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py311"}

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py39"}

View File

@@ -1,8 +1,10 @@
from typing import NoReturn, Protocol, Union, overload
class Empty:
...
def dummy(a): ...
def other(b): ...
async def other(b): ...
@overload
@@ -46,3 +48,11 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
def has_comment():
... # still a dummy
if some_condition:
...
if already_dummy: ...

View File

@@ -1,8 +1,11 @@
from typing import NoReturn, Protocol, Union, overload
class Empty: ...
def dummy(a): ...
def other(b): ...
async def other(b): ...
@overload
@@ -46,3 +49,13 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
def has_comment(): ... # still a dummy
if some_condition:
...
if already_dummy:
...

View File

@@ -74,6 +74,7 @@ pass
# form feeds are prohibited inside blocks, or on a line with nonwhitespace
def bar(a=1, b: bool = False):
pass

View File

@@ -124,23 +124,6 @@ func([x for x in "short line"])
func([x for x in "long line long line long line long line long line long line long line"])
func([x for x in [x for x in "long line long line long line long line long line long line long line"]])
func({"short line"})
func({"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"})
func({{"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"}})
func(("long line", "long long line", "long long long line", "long long long long line", "long long long long long line"))
func((("long line", "long long line", "long long long line", "long long long long line", "long long long long long line")))
func([["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]])
# Do not hug if the argument fits on a single line.
func({"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"})
func(("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"))
func(["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"])
func(**{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"})
func(*("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----"))
array = [{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}]
array = [("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")]
array = [["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]]
foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
)
@@ -150,14 +133,11 @@ baaaaaaaaaaaaar(
)
nested_mapping = {"key": [{"a very long key 1": "with a very long value", "a very long key 2": "with a very long value"}]}
nested_array = [[["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]]]
explicit_exploding = [[["short", "line",],],]
single_item_do_not_explode = Context({
"version": get_docs_version(),
})
foo(*["long long long long long line", "long long long long long line", "long long long long long line"])
foo(*[str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)])
foo(

View File

@@ -122,69 +122,6 @@ func([
]
])
func({"short line"})
func({
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
})
func({{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}})
func((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
))
func(((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
)))
func([[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]])
# Do not hug if the argument fits on a single line.
func(
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
)
func(
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
)
func(
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
)
func(
**{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"}
)
func(
*("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----")
)
array = [
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
]
array = [
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
]
array = [
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
]
foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
)
@@ -199,13 +136,6 @@ nested_mapping = {
"a very long key 2": "with a very long value",
}]
}
nested_array = [[[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]]]
explicit_exploding = [
[
[
@@ -218,12 +148,6 @@ single_item_do_not_explode = Context({
"version": get_docs_version(),
})
foo(*[
"long long long long long line",
"long long long long long line",
"long long long long long line",
])
foo(*[
str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)
])

View File

@@ -0,0 +1,23 @@
# split out from preview_hug_parens_with_brackes_and_square_brackets, as it produces
# different code on the second pass with line-length 1 in many cases.
# Seems to be about whether the last string in a sequence gets wrapped in parens or not.
foo(*["long long long long long line", "long long long long long line", "long long long long long line"])
func({"short line"})
func({"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"})
func({{"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"}})
func(("long line", "long long line", "long long long line", "long long long long line", "long long long long long line"))
func((("long line", "long long line", "long long long line", "long long long long line", "long long long long long line")))
func([["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]])
# Do not hug if the argument fits on a single line.
func({"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"})
func(("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"))
func(["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"])
func(**{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"})
func(*("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----"))
array = [{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}]
array = [("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")]
array = [["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]]
nested_array = [[["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]]]

View File

@@ -0,0 +1,79 @@
# split out from preview_hug_parens_with_brackes_and_square_brackets, as it produces
# different code on the second pass with line-length 1 in many cases.
# Seems to be about whether the last string in a sequence gets wrapped in parens or not.
foo(*[
"long long long long long line",
"long long long long long line",
"long long long long long line",
])
func({"short line"})
func({
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
})
func({{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}})
func((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
))
func(((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
)))
func([[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]])
# Do not hug if the argument fits on a single line.
func(
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
)
func(
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
)
func(
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
)
func(
**{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"}
)
func(
*("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----")
)
array = [
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
]
array = [
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
]
array = [
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
]
nested_array = [[[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]]]

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py310"}

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py310"}

View File

@@ -1 +1 @@
{"preview": "enabled"}
{"preview": "enabled", "target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py37"}

View File

@@ -0,0 +1 @@
{"target_version": "py38"}

View File

@@ -0,0 +1 @@
{"target_version": "py39"}

View File

@@ -0,0 +1 @@
{"target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py39"}

View File

@@ -0,0 +1 @@
{"target_version": "py310"}

View File

@@ -0,0 +1 @@
{"target_version": "py312"}

View File

@@ -51,6 +51,11 @@ def import_fixture(fixture: Path, fixture_set: str):
length = int(length)
options["line_width"] = 1 if length == 0 else length
if "--minimum-version=" in flags:
[_, version] = flags.split("--minimum-version=", 1)
# Convert 3.10 to py310
options["target_version"] = f"py{version.strip().replace('.', '')}"
if "--skip-magic-trailing-comma" in flags:
options["magic_trailing_comma"] = "ignore"

View File

@@ -153,6 +153,23 @@ this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use = (
function().b().c([1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3)
)
#######
# Subscripts and non-fluent attribute chains
a = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
xxxxx
].bbvbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb[
yyyyyyyyyy[aaaa]
] = ccccccccccccccccccccccccccccccccccc["aaaaaaa"]
a = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
xxxxx
].bbvbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = ccccccccccccccccccccccccccccccccccc[
"aaaaaaa"
]
label_thresholds[label_id] = label_quantiles[label_id][
min(int(tolerance * num_thresholds), num_thresholds - 1)
]
#######
# Test comment inlining

View File

@@ -17,7 +17,7 @@ use crate::comments::{
pub use crate::context::PyFormatContext;
pub use crate::options::{
DocstringCode, DocstringCodeLineWidth, MagicTrailingComma, PreviewMode, PyFormatOptions,
QuoteStyle,
PythonVersion, QuoteStyle,
};
pub use crate::shared_traits::{AsFormat, FormattedIter, FormattedIterExt, IntoFormat};
use crate::verbatim::suppressed_node;

View File

@@ -17,6 +17,10 @@ pub struct PyFormatOptions {
/// Whether we're in a `.py` file or `.pyi` file, which have different rules.
source_type: PySourceType,
/// The (minimum) Python version used to run the formatted code. This is used
/// to determine the supported Python syntax.
target_version: PythonVersion,
/// Specifies the indent style:
/// * Either a tab
/// * or a specific amount of spaces
@@ -74,6 +78,7 @@ impl Default for PyFormatOptions {
fn default() -> Self {
Self {
source_type: PySourceType::default(),
target_version: PythonVersion::default(),
indent_style: default_indent_style(),
line_width: default_line_width(),
indent_width: default_indent_width(),
@@ -101,31 +106,35 @@ impl PyFormatOptions {
}
}
pub fn magic_trailing_comma(&self) -> MagicTrailingComma {
pub const fn target_version(&self) -> PythonVersion {
self.target_version
}
pub const fn magic_trailing_comma(&self) -> MagicTrailingComma {
self.magic_trailing_comma
}
pub fn quote_style(&self) -> QuoteStyle {
pub const fn quote_style(&self) -> QuoteStyle {
self.quote_style
}
pub fn source_type(&self) -> PySourceType {
pub const fn source_type(&self) -> PySourceType {
self.source_type
}
pub fn source_map_generation(&self) -> SourceMapGeneration {
pub const fn source_map_generation(&self) -> SourceMapGeneration {
self.source_map_generation
}
pub fn line_ending(&self) -> LineEnding {
pub const fn line_ending(&self) -> LineEnding {
self.line_ending
}
pub fn docstring_code(&self) -> DocstringCode {
pub const fn docstring_code(&self) -> DocstringCode {
self.docstring_code
}
pub fn docstring_code_line_width(&self) -> DocstringCodeLineWidth {
pub const fn docstring_code_line_width(&self) -> DocstringCodeLineWidth {
self.docstring_code_line_width
}
@@ -133,6 +142,12 @@ impl PyFormatOptions {
self.preview
}
#[must_use]
pub fn with_target_version(mut self, target_version: PythonVersion) -> Self {
self.target_version = target_version;
self
}
#[must_use]
pub fn with_indent_width(mut self, indent_width: IndentWidth) -> Self {
self.indent_width = indent_width;
@@ -349,3 +364,22 @@ where
)),
}
}
#[derive(CacheKey, Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Default)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum PythonVersion {
Py37,
// Make sure to also change the default for `ruff_linter::settings::types::PythonVersion`
// when changing the default here.
#[default]
Py38,
Py39,
Py310,
Py311,
Py312,
}

View File

@@ -1,5 +1,7 @@
use ruff_formatter::{format_args, write, FormatError};
use ruff_python_ast::{AnyNodeRef, Expr, Operator, StmtAssign, TypeParams};
use ruff_python_ast::{
AnyNodeRef, Expr, ExprAttribute, ExprCall, Operator, StmtAssign, TypeParams,
};
use crate::builders::parenthesize_if_expands;
use crate::comments::{
@@ -9,7 +11,7 @@ use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::parentheses::{
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize,
};
use crate::expression::{has_own_parentheses, maybe_parenthesize_expression};
use crate::expression::{has_own_parentheses, has_parentheses, maybe_parenthesize_expression};
use crate::prelude::*;
use crate::preview::is_prefer_splitting_right_hand_side_of_assignments_enabled;
use crate::statement::trailing_semicolon;
@@ -56,7 +58,7 @@ impl FormatNodeRule<StmtAssign> for FormatStmtAssign {
}
.fmt(f)?;
}
// Avoid parenthesizing the value for single-target assignments that where the
// Avoid parenthesizing the value for single-target assignments where the
// target has its own parentheses (list, dict, tuple, ...) and the target expands.
else if has_target_own_parentheses(first, f.context())
&& !is_expression_parenthesized(
@@ -193,14 +195,14 @@ impl Format<PyFormatContext<'_>> for FormatTargetWithEqualOperator<'_> {
|| f.context().comments().has_trailing(self.target)
{
self.target.format().fmt(f)?;
} else if has_target_own_parentheses(self.target, f.context()) {
} else if should_parenthesize_target(self.target, f.context()) {
parenthesize_if_expands(&self.target.format().with_options(Parentheses::Never))
.fmt(f)?;
} else {
self.target
.format()
.with_options(Parentheses::Never)
.fmt(f)?;
} else {
parenthesize_if_expands(&self.target.format().with_options(Parentheses::Never))
.fmt(f)?;
}
write!(f, [space(), token("="), space()])
@@ -554,7 +556,9 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
// For call expressions, prefer breaking after the call expression's opening parentheses
// over parenthesizing the entire call expression.
if value.is_call_expr() {
// For subscripts, try breaking the subscript first
// For attribute chains that contain any parenthesized value: Try expanding the parenthesized value first.
if value.is_call_expr() || value.is_subscript_expr() || value.is_attribute_expr() {
best_fitting![
format_flat,
// Avoid parenthesizing the call expression if the `(` fit on the line
@@ -681,11 +685,11 @@ impl Format<PyFormatContext<'_>> for AnyBeforeOperator<'_> {
.fmt(f)
}
// Never parenthesize targets that come with their own parentheses, e.g. don't parenthesize lists or dictionary literals.
else if has_target_own_parentheses(expression, f.context()) {
expression.format().with_options(Parentheses::Never).fmt(f)
} else {
else if should_parenthesize_target(expression, f.context()) {
parenthesize_if_expands(&expression.format().with_options(Parentheses::Never))
.fmt(f)
} else {
expression.format().with_options(Parentheses::Never).fmt(f)
}
}
// Never parenthesize type params
@@ -717,7 +721,7 @@ fn should_inline_comments(
}
}
/// Tests whether an expression that for which comments shouldn't be inlined should use the best fit layout
/// Tests whether an expression for which comments shouldn't be inlined should use the best fit layout
fn should_non_inlineable_use_best_fit(
expr: &Expr,
parent: AnyNodeRef,
@@ -728,12 +732,32 @@ fn should_non_inlineable_use_best_fit(
attribute.needs_parentheses(parent, context) == OptionalParentheses::BestFit
}
Expr::Call(call) => call.needs_parentheses(parent, context) == OptionalParentheses::BestFit,
Expr::Subscript(subscript) => {
subscript.needs_parentheses(parent, context) == OptionalParentheses::BestFit
}
_ => false,
}
}
/// Returns `true` for targets that should not be parenthesized if they split because their expanded
/// layout comes with their own set of parentheses.
/// Returns `true` for targets that have their own set of parentheses when they split,
/// in which case we want to avoid parenthesizing the assigned value.
pub(super) fn has_target_own_parentheses(target: &Expr, context: &PyFormatContext) -> bool {
matches!(target, Expr::Tuple(_)) || has_own_parentheses(target, context).is_some()
}
pub(super) fn should_parenthesize_target(target: &Expr, context: &PyFormatContext) -> bool {
!(has_target_own_parentheses(target, context)
|| is_attribute_with_parenthesized_value(target, context))
}
fn is_attribute_with_parenthesized_value(target: &Expr, context: &PyFormatContext) -> bool {
match target {
Expr::Attribute(ExprAttribute { value, .. }) => {
has_parentheses(value.as_ref(), context).is_some()
|| is_attribute_with_parenthesized_value(value, context)
}
Expr::Subscript(_) => true,
Expr::Call(ExprCall { arguments, .. }) => !arguments.is_empty(),
_ => false,
}
}

View File

@@ -355,7 +355,8 @@ line-ending = {line_ending:?}
magic-trailing-comma = {magic_trailing_comma:?}
docstring-code = {docstring_code:?}
docstring-code-line-width = {docstring_code_line_width:?}
preview = {preview:?}"#,
preview = {preview:?}
target_version = {target_version:?}"#,
indent_style = self.0.indent_style(),
indent_width = self.0.indent_width().value(),
line_width = self.0.line_width().value(),
@@ -364,7 +365,8 @@ preview = {preview:?}"#,
magic_trailing_comma = self.0.magic_trailing_comma(),
docstring_code = self.0.docstring_code(),
docstring_code_line_width = self.0.docstring_code_line_width(),
preview = self.0.preview()
preview = self.0.preview(),
target_version = self.0.target_version()
)
}
}

View File

@@ -162,8 +162,6 @@ class Test:
pass
a = "type comment with trailing space" # type: str
#######################
### SECTION COMMENT ###
#######################
@@ -427,8 +425,6 @@ class Test:
pass
a = "type comment with trailing space" # type: str
#######################
### SECTION COMMENT ###
#######################
@@ -607,8 +603,6 @@ class Test:
pass
a = "type comment with trailing space" # type: str
#######################
### SECTION COMMENT ###
#######################

View File

@@ -0,0 +1,260 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/preview_allow_empty_first_line.py
---
## Input
```python
def foo():
"""
Docstring
"""
# Here we go
if x:
# This is also now fine
a = 123
else:
# But not necessary
a = 123
if y:
while True:
"""
Long comment here
"""
a = 123
if z:
for _ in range(100):
a = 123
else:
try:
# this should be ok
a = 123
except:
"""also this"""
a = 123
def bar():
if x:
a = 123
def baz():
# OK
if x:
a = 123
def quux():
new_line = here
class Cls:
def method(self):
pass
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -5,7 +5,6 @@
# Here we go
if x:
-
# This is also now fine
a = 123
@@ -14,49 +13,39 @@
a = 123
if y:
-
while True:
-
"""
Long comment here
"""
a = 123
if z:
-
for _ in range(100):
a = 123
else:
-
try:
-
# this should be ok
a = 123
except:
-
"""also this"""
a = 123
def bar():
-
if x:
a = 123
def baz():
-
# OK
if x:
a = 123
def quux():
-
new_line = here
class Cls:
def method(self):
-
pass
```
## Ruff Output
```python
def foo():
"""
Docstring
"""
# Here we go
if x:
# This is also now fine
a = 123
else:
# But not necessary
a = 123
if y:
while True:
"""
Long comment here
"""
a = 123
if z:
for _ in range(100):
a = 123
else:
try:
# this should be ok
a = 123
except:
"""also this"""
a = 123
def bar():
if x:
a = 123
def baz():
# OK
if x:
a = 123
def quux():
new_line = here
class Cls:
def method(self):
pass
```
## Black Output
```python
def foo():
"""
Docstring
"""
# Here we go
if x:
# This is also now fine
a = 123
else:
# But not necessary
a = 123
if y:
while True:
"""
Long comment here
"""
a = 123
if z:
for _ in range(100):
a = 123
else:
try:
# this should be ok
a = 123
except:
"""also this"""
a = 123
def bar():
if x:
a = 123
def baz():
# OK
if x:
a = 123
def quux():
new_line = here
class Cls:
def method(self):
pass
```

View File

@@ -7,9 +7,11 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/pre
```python
from typing import NoReturn, Protocol, Union, overload
class Empty:
...
def dummy(a): ...
def other(b): ...
async def other(b): ...
@overload
@@ -53,6 +55,14 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
def has_comment():
... # still a dummy
if some_condition:
...
if already_dummy: ...
```
## Black Differences
@@ -60,13 +70,13 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
```diff
--- Black
+++ Ruff
@@ -2,15 +2,23 @@
@@ -5,15 +5,23 @@
def dummy(a): ...
+
+
def other(b): ...
async def other(b): ...
@overload
@@ -84,7 +94,7 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
def a(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
@@ -21,10 +29,13 @@
@@ -24,10 +32,13 @@
def foo(self, a: int) -> int: ...
def bar(self, b: str) -> str: ...
@@ -98,7 +108,7 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
@dummy
def dummy_three(): ...
@@ -38,6 +49,8 @@
@@ -41,6 +52,8 @@
@overload
def b(arg: str) -> str: ...
@@ -107,6 +117,17 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
@overload
def b(arg: object) -> NoReturn: ...
@@ -54,8 +67,6 @@
def has_comment(): ... # still a dummy
-if some_condition:
- ...
+if some_condition: ...
-if already_dummy:
- ...
+if already_dummy: ...
```
## Ruff Output
@@ -115,10 +136,13 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
from typing import NoReturn, Protocol, Union, overload
class Empty: ...
def dummy(a): ...
def other(b): ...
async def other(b): ...
@overload
@@ -173,6 +197,14 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
def has_comment(): ... # still a dummy
if some_condition: ...
if already_dummy: ...
```
## Black Output
@@ -181,8 +213,11 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
from typing import NoReturn, Protocol, Union, overload
class Empty: ...
def dummy(a): ...
def other(b): ...
async def other(b): ...
@overload
@@ -226,6 +261,16 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg
def has_comment(): ... # still a dummy
if some_condition:
...
if already_dummy:
...
```

View File

@@ -211,7 +211,7 @@ a = [
pass
@@ -68,7 +68,7 @@
@@ -68,13 +68,12 @@
def foo():
pass
@@ -220,7 +220,13 @@ a = [
pass
@@ -84,7 +84,7 @@
# form feeds are prohibited inside blocks, or on a line with nonwhitespace
def bar(a=1, b: bool = False):
-
pass
@@ -85,7 +84,7 @@
def something(self):
pass
@@ -416,6 +422,7 @@ pass
# form feeds are prohibited inside blocks, or on a line with nonwhitespace
def bar(a=1, b: bool = False):
pass

View File

@@ -131,23 +131,6 @@ func([x for x in "short line"])
func([x for x in "long line long line long line long line long line long line long line"])
func([x for x in [x for x in "long line long line long line long line long line long line long line"]])
func({"short line"})
func({"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"})
func({{"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"}})
func(("long line", "long long line", "long long long line", "long long long long line", "long long long long long line"))
func((("long line", "long long line", "long long long line", "long long long long line", "long long long long long line")))
func([["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]])
# Do not hug if the argument fits on a single line.
func({"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"})
func(("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"))
func(["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"])
func(**{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"})
func(*("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----"))
array = [{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}]
array = [("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")]
array = [["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]]
foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
)
@@ -157,14 +140,11 @@ baaaaaaaaaaaaar(
)
nested_mapping = {"key": [{"a very long key 1": "with a very long value", "a very long key 2": "with a very long value"}]}
nested_array = [[["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]]]
explicit_exploding = [[["short", "line",],],]
single_item_do_not_explode = Context({
"version": get_docs_version(),
})
foo(*["long long long long long line", "long long long long long line", "long long long long long line"])
foo(*[str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)])
foo(
@@ -262,115 +242,7 @@ for foo in ["a", "b"]:
x
for x in [
x
@@ -130,13 +136,15 @@
"long long long long line",
"long long long long long line",
})
-func({{
- "long line",
- "long long line",
- "long long long line",
- "long long long long line",
- "long long long long long line",
-}})
+func({
+ {
+ "long line",
+ "long long line",
+ "long long long line",
+ "long long long long line",
+ "long long long long long line",
+ }
+})
func((
"long line",
"long long line",
@@ -151,30 +159,62 @@
"long long long long line",
"long long long long long line",
)))
-func([[
- "long line",
- "long long line",
- "long long long line",
- "long long long long line",
- "long long long long long line",
-]])
+func([
+ [
+ "long line",
+ "long long line",
+ "long long long line",
+ "long long long long line",
+ "long long long long long line",
+ ]
+])
# Do not hug if the argument fits on a single line.
-func(
- {"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
-)
-func(
- ("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
-)
-func(
- ["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
-)
-func(
- **{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"}
-)
-func(
- *("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----")
-)
+func({
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+})
+func((
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+))
+func([
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+])
+func(**{
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit---",
+})
+func(*(
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit----",
+))
array = [
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
]
@@ -194,18 +234,24 @@
@@ -131,10 +137,12 @@
)
nested_mapping = {
@@ -385,28 +257,9 @@ for foo in ["a", "b"]:
+ }
+ ]
}
-nested_array = [[[
- "long line",
- "long long line",
- "long long long line",
- "long long long long line",
- "long long long long long line",
-]]]
+nested_array = [
+ [
+ [
+ "long line",
+ "long long line",
+ "long long long line",
+ "long long long long line",
+ "long long long long long line",
+ ]
+ ]
+]
explicit_exploding = [
[
[
@@ -240,9 +286,9 @@
@@ -164,9 +172,9 @@
})
# Edge case when deciding whether to hug the brackets without inner content.
@@ -554,103 +407,6 @@ func([
]
])
func({"short line"})
func({
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
})
func({
{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}
})
func((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
))
func(((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
)))
func([
[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]
])
# Do not hug if the argument fits on a single line.
func({
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
})
func((
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
))
func([
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
])
func(**{
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit---",
})
func(*(
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit----",
))
array = [
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
]
array = [
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
]
array = [
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
]
foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
)
@@ -667,17 +423,6 @@ nested_mapping = {
}
]
}
nested_array = [
[
[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]
]
]
explicit_exploding = [
[
[
@@ -690,12 +435,6 @@ single_item_do_not_explode = Context({
"version": get_docs_version(),
})
foo(*[
"long long long long long line",
"long long long long long line",
"long long long long long line",
])
foo(*[
str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)
])
@@ -854,69 +593,6 @@ func([
]
])
func({"short line"})
func({
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
})
func({{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}})
func((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
))
func(((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
)))
func([[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]])
# Do not hug if the argument fits on a single line.
func(
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
)
func(
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
)
func(
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
)
func(
**{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"}
)
func(
*("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----")
)
array = [
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
]
array = [
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
]
array = [
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
]
foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
)
@@ -931,13 +607,6 @@ nested_mapping = {
"a very long key 2": "with a very long value",
}]
}
nested_array = [[[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]]]
explicit_exploding = [
[
[
@@ -950,12 +619,6 @@ single_item_do_not_explode = Context({
"version": get_docs_version(),
})
foo(*[
"long long long long long line",
"long long long long long line",
"long long long long long line",
])
foo(*[
str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)
])

View File

@@ -0,0 +1,377 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/preview_hug_parens_with_braces_and_square_brackets_no_ll1.py
---
## Input
```python
# split out from preview_hug_parens_with_brackes_and_square_brackets, as it produces
# different code on the second pass with line-length 1 in many cases.
# Seems to be about whether the last string in a sequence gets wrapped in parens or not.
foo(*["long long long long long line", "long long long long long line", "long long long long long line"])
func({"short line"})
func({"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"})
func({{"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"}})
func(("long line", "long long line", "long long long line", "long long long long line", "long long long long long line"))
func((("long line", "long long line", "long long long line", "long long long long line", "long long long long long line")))
func([["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]])
# Do not hug if the argument fits on a single line.
func({"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"})
func(("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"))
func(["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"])
func(**{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"})
func(*("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----"))
array = [{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}]
array = [("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")]
array = [["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]]
nested_array = [[["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]]]
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -14,13 +14,15 @@
"long long long long line",
"long long long long long line",
})
-func({{
- "long line",
- "long long line",
- "long long long line",
- "long long long long line",
- "long long long long long line",
-}})
+func({
+ {
+ "long line",
+ "long long line",
+ "long long long line",
+ "long long long long line",
+ "long long long long long line",
+ }
+})
func((
"long line",
"long long line",
@@ -35,31 +37,63 @@
"long long long long line",
"long long long long long line",
)))
-func([[
- "long line",
- "long long line",
- "long long long line",
- "long long long long line",
- "long long long long long line",
-]])
+func([
+ [
+ "long line",
+ "long long line",
+ "long long long line",
+ "long long long long line",
+ "long long long long long line",
+ ]
+])
# Do not hug if the argument fits on a single line.
-func(
- {"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
-)
-func(
- ("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
-)
-func(
- ["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
-)
-func(
- **{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"}
-)
-func(
- *("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----")
-)
+func({
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+})
+func((
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+))
+func([
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+])
+func(**{
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit---",
+})
+func(*(
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit line",
+ "fit----",
+))
array = [
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
]
@@ -70,10 +104,14 @@
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
]
-nested_array = [[[
- "long line",
- "long long line",
- "long long long line",
- "long long long long line",
- "long long long long long line",
-]]]
+nested_array = [
+ [
+ [
+ "long line",
+ "long long line",
+ "long long long line",
+ "long long long long line",
+ "long long long long long line",
+ ]
+ ]
+]
```
## Ruff Output
```python
# split out from preview_hug_parens_with_brackes_and_square_brackets, as it produces
# different code on the second pass with line-length 1 in many cases.
# Seems to be about whether the last string in a sequence gets wrapped in parens or not.
foo(*[
"long long long long long line",
"long long long long long line",
"long long long long long line",
])
func({"short line"})
func({
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
})
func({
{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}
})
func((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
))
func(((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
)))
func([
[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]
])
# Do not hug if the argument fits on a single line.
func({
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
})
func((
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
))
func([
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
])
func(**{
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit---",
})
func(*(
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit line",
"fit----",
))
array = [
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
]
array = [
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
]
array = [
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
]
nested_array = [
[
[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]
]
]
```
## Black Output
```python
# split out from preview_hug_parens_with_brackes_and_square_brackets, as it produces
# different code on the second pass with line-length 1 in many cases.
# Seems to be about whether the last string in a sequence gets wrapped in parens or not.
foo(*[
"long long long long long line",
"long long long long long line",
"long long long long long line",
])
func({"short line"})
func({
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
})
func({{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}})
func((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
))
func(((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
)))
func([[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]])
# Do not hug if the argument fits on a single line.
func(
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
)
func(
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
)
func(
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
)
func(
**{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit---"}
)
func(
*("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit----")
)
array = [
{"fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"}
]
array = [
("fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line")
]
array = [
["fit line", "fit line", "fit line", "fit line", "fit line", "fit line", "fit line"]
]
nested_array = [[[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]]]
```

View File

@@ -56,6 +56,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py38
```
```python

View File

@@ -175,6 +175,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -349,6 +350,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -523,6 +525,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -697,6 +700,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -871,6 +875,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -1368,6 +1368,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -2738,6 +2739,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -4108,6 +4110,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -5478,6 +5481,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -6848,6 +6852,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -8215,6 +8220,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -9582,6 +9588,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -10958,6 +10965,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -12325,6 +12333,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = 60
preview = Disabled
target_version = Py38
```
```python
@@ -13701,6 +13710,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -27,6 +27,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -239,6 +239,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -545,6 +546,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -841,6 +843,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -1147,6 +1150,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -138,6 +138,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -290,6 +291,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -153,6 +153,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -329,6 +330,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -37,6 +37,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -73,6 +74,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -18,6 +18,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -35,6 +36,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -52,6 +54,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -33,6 +33,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -66,6 +67,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -99,6 +101,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -84,6 +84,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -166,6 +167,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py38
```
```python

View File

@@ -68,6 +68,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -139,6 +140,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -210,6 +212,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -51,6 +51,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -107,6 +108,7 @@ magic-trailing-comma = Ignore
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -159,6 +159,23 @@ this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use = (
function().b().c([1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3)
)
#######
# Subscripts and non-fluent attribute chains
a = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
xxxxx
].bbvbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb[
yyyyyyyyyy[aaaa]
] = ccccccccccccccccccccccccccccccccccc["aaaaaaa"]
a = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
xxxxx
].bbvbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = ccccccccccccccccccccccccccccccccccc[
"aaaaaaa"
]
label_thresholds[label_id] = label_quantiles[label_id][
min(int(tolerance * num_thresholds), num_thresholds - 1)
]
#######
# Test comment inlining
@@ -236,6 +253,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py38
```
```python
@@ -394,6 +412,23 @@ this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use = (
function().b().c([1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3)
)
#######
# Subscripts and non-fluent attribute chains
a = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
xxxxx
].bbvbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb[
yyyyyyyyyy[aaaa]
] = ccccccccccccccccccccccccccccccccccc["aaaaaaa"]
a = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
xxxxx
].bbvbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = ccccccccccccccccccccccccccccccccccc[
"aaaaaaa"
]
label_thresholds[label_id] = label_quantiles[label_id][
min(int(tolerance * num_thresholds), num_thresholds - 1)
]
#######
# Test comment inlining

View File

@@ -26,6 +26,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -51,6 +52,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -79,6 +81,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff_shrinking"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -175,6 +175,14 @@ impl Configuration {
let formatter = FormatterSettings {
exclude: FilePatternSet::try_from_iter(format.exclude.unwrap_or_default())?,
preview: format_preview,
target_version: match target_version {
PythonVersion::Py37 => ruff_python_formatter::PythonVersion::Py37,
PythonVersion::Py38 => ruff_python_formatter::PythonVersion::Py38,
PythonVersion::Py39 => ruff_python_formatter::PythonVersion::Py39,
PythonVersion::Py310 => ruff_python_formatter::PythonVersion::Py310,
PythonVersion::Py311 => ruff_python_formatter::PythonVersion::Py311,
PythonVersion::Py312 => ruff_python_formatter::PythonVersion::Py312,
},
line_width: self
.line_length
.map_or(format_defaults.line_width, |length| {

View File

@@ -117,6 +117,7 @@ impl FileResolverSettings {
pub struct FormatterSettings {
pub exclude: FilePatternSet,
pub preview: PreviewMode,
pub target_version: ruff_python_formatter::PythonVersion,
pub line_width: LineWidth,
@@ -157,6 +158,7 @@ impl FormatterSettings {
};
PyFormatOptions::from_source_type(source_type)
.with_target_version(self.target_version)
.with_indent_style(self.indent_style)
.with_indent_width(self.indent_width)
.with_quote_style(self.quote_style)
@@ -175,6 +177,7 @@ impl Default for FormatterSettings {
Self {
exclude: FilePatternSet::default(),
target_version: default_options.target_version(),
preview: PreviewMode::Disabled,
line_width: default_options.line_width(),
line_ending: LineEnding::Auto,

View File

@@ -14,7 +14,7 @@ Ruff can be used as a [pre-commit](https://pre-commit.com) hook via [`ruff-pre-c
```yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.8
rev: v0.1.9
hooks:
# Run the linter.
- id: ruff
@@ -27,7 +27,7 @@ To enable lint fixes, add the `--fix` argument to the lint hook:
```yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.8
rev: v0.1.9
hooks:
# Run the linter.
- id: ruff
@@ -41,7 +41,7 @@ To run the hooks over Jupyter Notebooks too, add `jupyter` to the list of allowe
```yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.8
rev: v0.1.9
hooks:
# Run the linter.
- id: ruff

View File

@@ -4,7 +4,7 @@ build-backend = "maturin"
[project]
name = "ruff"
version = "0.1.8"
version = "0.1.9"
description = "An extremely fast Python linter and code formatter, written in Rust."
authors = [{ name = "Astral Software Inc.", email = "hey@astral.sh" }]
readme = "README.md"
@@ -74,7 +74,7 @@ force-exclude = '''
major_labels = [] # Ruff never uses the major version number
minor_labels = ["breaking"] # Bump the minor version on breaking changes
changelog_ignore_labels = ["internal"]
changelog_ignore_labels = ["internal", "ci"]
changelog_sections.breaking = "Breaking changes"
changelog_sections.preview = "Preview features"
@@ -91,7 +91,7 @@ changelog_sections.__unknown__ = "Other changes"
changelog_contributors = false
version_files = [
"README.md",
"README.md",
"docs/integrations.md",
"crates/flake8_to_ruff/Cargo.toml",
"crates/ruff_cli/Cargo.toml",

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "scripts"
version = "0.1.8"
version = "0.1.9"
description = ""
authors = ["Charles Marsh <charlie.r.marsh@gmail.com>"]