-
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
] Add blank line(s) rules (E301
, E302
, E303
, E304
, E305
, E306
)
#4694
Conversation
Made blank_characters into a u32 again to avoid all the try_from.
I looked into merging this but it doesn't seem to properly handle nested definitions -- this snippet is clean under pycodestyle, but yields violations here: class C:
def f():
pass
def f():
def f():
pass Yields:
|
@charliermarsh Yes, the rules and examples of pycodestyles do not match the actual implementation. For example, the rule E306's example is not detected by pycodestyle (as you've pointed out above), but this false positive is. |
I haven't had time to look into tracking the number of blank lines in the logical lines builder yet, but I'll try to do it once I have time. |
…r in order to not emit empty lines as logical lines.
I've moved the tracking of blank lines into the logical lines builder, so blank lines are no longer emitted as logical lines. |
I started reviewing the output on the pycodestyle fixture, and found a major issue with this PR. Pycodestyle's E3 rules take into account comments. For example: a = 1
# a
# a
class A:
pass Pycodestyle output:
However, since comment only lines are not logical lines, I can't reproduce this output. Moreover, this causes the implementation of the autofix to mess up the code. Without making empty lines/comment only lines into logical lines (or using physical lines), I don't think these rules can be implemented. |
This reverts commit 071849e.
@charliermarsh what do you think? |
Could we move this forward somehow? I was about to start implementing these rules but luckily checked whether there was momentum on it already :) |
I'll finish cleaning the fixture to make it easier to check that the implementation works (or not). But without implementing something like the proposed TokenVisitor, it's difficult to progress. |
@MichaReiser Did something like the # comment
# comment |
Not the way I described it but there is a set of token-based rules that operate only on the tokens/indexer. Would that meet your needs? ruff/crates/ruff_linter/src/checkers/tokens.rs Lines 22 to 197 in b34278e
|
I think so, thank you. |
Try to perform as many operations as possible on the tokens directly. Only fall back to inspect the source code (or token content) if you must. |
Closed in favor of #8720. |
Summary
This PR is part of #2402, it adds the
E301
,E302
,E303
,E304
,E305
,E306
error rules along with their fixes.A first attempt at implementing
E305
using physical lines was done here, however since some operations are expensive when using physical lines, this implementation uses logical lines.To be able to use
NonLogicalNewline
to detect blank lines, I have removed the check explicitly skipping them. I have modified the existing rules so that their behavior remains unchanged.Test Plan
The test fixture uses the one from pycodestyle as is, but the notes in the file about which line should generate which error do not fully match the rules nor the implementation. I therefore did my best to manually check that what I did matches the rules/implementation in a way that makes sense.
For example, the rule E306's example is not detected by pycodestyle, but this false positive is.