Skip to content

Commit

Permalink
Allow collapsed-ellipsis bodies in other statements (#8499)
Browse files Browse the repository at this point in the history
## Summary

Black and Ruff's preview styles now collapse statements like:

```python
from contextlib import nullcontext

ctx = nullcontext()
with ctx: ...
```

Historically, we made an exception here for classes
(#2837). This PR extends it to
other statement kinds for consistency with the formatter.

Closes #8496.
  • Loading branch information
charliermarsh authored Nov 6, 2023
1 parent 4170ef0 commit 8c146bb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pycodestyle/E70.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ class Foo:
#: E701:2:3
a = \
5;
#:
with x(y) as z: ...
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use ruff_source_file::Locator;
/// if foo == "blah":
/// do_blah_thing()
/// ```
///
/// [PEP 8]: https://peps.python.org/pep-0008/#other-recommendations
#[violation]
pub struct MultipleStatementsOnOneLineColon;
Expand Down Expand Up @@ -206,12 +206,13 @@ pub(crate) fn compound_statements(
{
colon = Some((range.start(), range.end()));

// Allow `class C: ...`-style definitions in stubs.
allow_ellipsis = class.is_some();
// Allow `class C: ...`-style definitions.
allow_ellipsis = true;
}
}
Tok::Semi => {
semi = Some((range.start(), range.end()));
allow_ellipsis = false;
}
Tok::Comment(..) | Tok::Indent | Tok::Dedent | Tok::NonLogicalNewline => {}
_ => {
Expand All @@ -223,6 +224,7 @@ pub(crate) fn compound_statements(

// Reset.
semi = None;
allow_ellipsis = false;
}

if let Some((start, end)) = colon {
Expand All @@ -245,6 +247,7 @@ pub(crate) fn compound_statements(
try_ = None;
while_ = None;
with = None;
allow_ellipsis = false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ E70.py:71:4: E703 [*] Statement ends with an unnecessary semicolon
70 | a = \
71 | 5;
| ^ E703
72 | #:
73 | with x(y) as z: ...
|
= help: Remove unnecessary semicolon

Expand All @@ -101,5 +103,7 @@ E70.py:71:4: E703 [*] Statement ends with an unnecessary semicolon
70 70 | a = \
71 |- 5;
71 |+ 5
72 72 | #:
73 73 | with x(y) as z: ...


0 comments on commit 8c146bb

Please sign in to comment.