Allow collapsed-ellipsis bodies in other statements (#8499)

## 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
(https://github.com/astral-sh/ruff/pull/2837). This PR extends it to
other statement kinds for consistency with the formatter.

Closes https://github.com/astral-sh/ruff/issues/8496.
This commit is contained in:
Charlie Marsh 2023-11-05 16:42:34 -08:00 committed by GitHub
parent 4170ef0508
commit 8c146bbf11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 3 deletions

View file

@ -69,3 +69,5 @@ while 1:
#: E701:2:3
a = \
5;
#:
with x(y) as z: ...

View file

@ -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;
@ -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 => {}
_ => {
@ -223,6 +224,7 @@ pub(crate) fn compound_statements(
// Reset.
semi = None;
allow_ellipsis = false;
}
if let Some((start, end)) = colon {
@ -245,6 +247,7 @@ pub(crate) fn compound_statements(
try_ = None;
while_ = None;
with = None;
allow_ellipsis = false;
}
}
}

View file

@ -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
@ -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: ...