diff --git a/crates/ruff/resources/test/fixtures/pycodestyle/E731.py b/crates/ruff/resources/test/fixtures/pycodestyle/E731.py index 2cb4c7ea3a..7c01e74a03 100644 --- a/crates/ruff/resources/test/fixtures/pycodestyle/E731.py +++ b/crates/ruff/resources/test/fixtures/pycodestyle/E731.py @@ -138,3 +138,12 @@ def scope(): class TemperatureScales(Enum): CELSIUS = (lambda deg_c: deg_c) FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32) + + +# Regression test for: https://github.com/astral-sh/ruff/issues/7141 +def scope(): + # E731 + + f = lambda: ( + i := 1, + ) diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E731_E731.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E731_E731.py.snap index 39603bd0c2..976423061a 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E731_E731.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E731_E731.py.snap @@ -339,6 +339,8 @@ E731.py:139:5: E731 [*] Do not assign a `lambda` expression, use a `def` 139 |+ def CELSIUS(deg_c): 140 |+ return deg_c 140 141 | FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32) +141 142 | +142 143 | E731.py:140:5: E731 [*] Do not assign a `lambda` expression, use a `def` | @@ -356,5 +358,30 @@ E731.py:140:5: E731 [*] Do not assign a `lambda` expression, use a `def` 140 |- FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32) 140 |+ def FAHRENHEIT(deg_c): 141 |+ return deg_c * 9 / 5 + 32 +141 142 | +142 143 | +143 144 | # Regression test for: https://github.com/astral-sh/ruff/issues/7141 + +E731.py:147:5: E731 [*] Do not assign a `lambda` expression, use a `def` + | +145 | # E731 +146 | +147 | f = lambda: ( + | _____^ +148 | | i := 1, +149 | | ) + | |_____^ E731 + | + = help: Rewrite `f` as a `def` + +ℹ Suggested fix +144 144 | def scope(): +145 145 | # E731 +146 146 | +147 |- f = lambda: ( +148 |- i := 1, +149 |- ) + 147 |+ def f(): + 148 |+ return (i := 1), diff --git a/crates/ruff_python_codegen/src/generator.rs b/crates/ruff_python_codegen/src/generator.rs index ff9d4b4b21..e741c89326 100644 --- a/crates/ruff_python_codegen/src/generator.rs +++ b/crates/ruff_python_codegen/src/generator.rs @@ -14,6 +14,7 @@ use ruff_source_file::LineEnding; use super::stylist::{Indentation, Quote, Stylist}; mod precedence { + pub(crate) const NAMED_EXPR: u8 = 1; pub(crate) const ASSIGN: u8 = 3; pub(crate) const ANN_ASSIGN: u8 = 5; pub(crate) const AUG_ASSIGN: u8 = 5; @@ -30,7 +31,6 @@ mod precedence { pub(crate) const TUPLE: u8 = 19; pub(crate) const FORMATTED_VALUE: u8 = 19; pub(crate) const COMMA: u8 = 21; - pub(crate) const NAMED_EXPR: u8 = 23; pub(crate) const ASSERT: u8 = 23; pub(crate) const COMPREHENSION_ELEMENT: u8 = 27; pub(crate) const LAMBDA: u8 = 27; @@ -1639,6 +1639,13 @@ class Foo: "class SchemaItem(NamedTuple): fields: ((\"property_key\", str),)" ); + assert_round_trip!( + "def func(): + return (i := 1)" + ); + assert_round_trip!("yield (i := 1)"); + assert_round_trip!("x = (i := 1)"); + assert_round_trip!("x += (i := 1)"); // Type aliases assert_round_trip!(r#"type Foo = int | str"#);