-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[pycodestyle
] Avoid blank line rules for the first logical line in cell
#10291
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
E302 | 62 | 0 | 62 | 0 | 0 |
E305 | 49 | 0 | 49 | 0 | 0 |
@@ -505,6 +517,15 @@ impl<'a> Iterator for LinePreprocessor<'a> { | |||
// Set the start for the next logical line. | |||
self.line_start = range.end(); | |||
|
|||
if self | |||
.cell_offsets | |||
.is_some_and(|cell_offsets| cell_offsets.contains(&self.line_start)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to use containing_range
here. contains
is O(n)
, containing_range
is O(log(n))
. But I wonder if we could do better by keeping an iterator with the cell offsets (they're sorted) and peek at the first offset and:
- while it is smaller than the
line_start
, call next - if it is not
None
, you know its inside of the cell.
This gives youO(n)
performance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MichaReiser It's not exactly what you said, but I gave it a try in 3f703af. What do you think ?
if self | ||
.cell_offsets | ||
.is_some_and(|cell_offsets| cell_offsets.contains(&self.line_start)) | ||
{ | ||
self.is_cell_first_non_comment_line = true; | ||
} else if !line_is_comment_only { | ||
self.is_cell_first_non_comment_line = false; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it okay that we set is_cell_first_non_comment_line
to true
even if line_is_comment_only
is true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine since if a cell starts with a comment, we don't want to run the check on that comment (since it's the first line).
However the naming is pretty bad indeed, I'll try to improve it.
Edit: renamed in f29a53d.
@dhruvmanila would you mind taking a look |
(Sorry, will look at it today.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've started looking into it and testing out the code for the E30
rules. I've noticed a few things:
E303
For the following two cells:
# There are two blank lines after `function1`
def function1():
pass
def function2():
pass
# There is one blank line before `function2`
This is still raising the E303
error. I guess this is because we continue
without checking for the cell boundary here:
ruff/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs
Lines 430 to 437 in 461cdad
// An empty line | |
if token_kind == TokenKind::NonLogicalNewline { | |
blank_lines.add(*range); | |
self.line_start = range.end(); | |
continue; | |
} |
I think the intent for the fix should be to reset the count of blank lines such that the number of empty lines before the function2
gives us 1 instead of 3.
E304
I don't really have a good way of testing this unless we actually define the decorator and function definition in separate cells like so:
# One empty line after the decorator
@decorator
def function():
pass
But this is highly unlikely to come up in a real world and in the future we would be making sure that this raises a syntax error (#10264). Nevertheless this suffers from the same problem as mentioned earlier. I'm assuming the root cause is the same.
c5c648f
to
559b030
Compare
5216bcb
to
a2cea4a
Compare
@dhruvmanila Thanks for the review. I've fixed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
pycodestyle
] Fix blank line rules adding blank lines at the beginning of cells.pycodestyle
] Avoid blank line rules for the first logical line in cell
Summary
Closes #10228
The PR makes the blank lines rules keep track of the cell status when running on a notebook, and makes the rules not trigger when the line is the first of the cell.
Test Plan
The example given in #10228 is added as a fixture, along with a few tests from the main blank lines fixtures.