Skip to content

Commit

Permalink
Allow compound statements of single ellipsis (#2837)
Browse files Browse the repository at this point in the history
This allows `class C: ...`-style compound statements in stub files.

Closes #2835.
  • Loading branch information
charliermarsh authored Feb 12, 2023
1 parent 83f6e52 commit 7dab480
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/ruff/resources/test/fixtures/pycodestyle/E70.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ class C: pass
pass
#:
func = lambda x: x** 2 if cond else lambda x:x
#:
class C: ...
#:
def f(): ...
#: E701:1:8 E702:1:13
class C: ...; x = 1
#: E701:1:8 E702:1:13
class C: ...; ...
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub fn compound_statements(lxr: &[LexResult]) -> Vec<Diagnostic> {
let mut while_ = None;
let mut with = None;

// As a special-case, track whether we're at the first token after a colon.
// This is used to allow `class C: ...`-style definitions in stubs.
let mut allow_ellipsis = false;

// Track the bracket depth.
let mut par_count = 0;
let mut sqb_count = 0;
Expand Down Expand Up @@ -118,12 +122,17 @@ pub fn compound_statements(lxr: &[LexResult]) -> Vec<Diagnostic> {
|| with.is_some()
{
colon = Some((start, end));
allow_ellipsis = true;
}
}
Tok::Semi => {
semi = Some((start, end));
}
Tok::Comment(..) | Tok::Indent | Tok::Dedent | Tok::NonLogicalNewline => {}
Tok::Ellipsis if allow_ellipsis => {
// Allow `class C: ...`-style definitions in stubs.
allow_ellipsis = false;
}
_ => {
if let Some((start, end)) = semi {
diagnostics.push(Diagnostic::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,24 @@ expression: diagnostics
column: 15
fix: ~
parent: ~
- kind:
MultipleStatementsOnOneLineColon: ~
location:
row: 54
column: 7
end_location:
row: 54
column: 8
fix: ~
parent: ~
- kind:
MultipleStatementsOnOneLineColon: ~
location:
row: 56
column: 7
end_location:
row: 56
column: 8
fix: ~
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,24 @@ expression: diagnostics
column: 11
fix: ~
parent: ~
- kind:
MultipleStatementsOnOneLineSemicolon: ~
location:
row: 54
column: 12
end_location:
row: 54
column: 13
fix: ~
parent: ~
- kind:
MultipleStatementsOnOneLineSemicolon: ~
location:
row: 56
column: 12
end_location:
row: 56
column: 13
fix: ~
parent: ~

0 comments on commit 7dab480

Please sign in to comment.