Skip to content
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

Implement remaining pycodestyle rules #2402

Open
63 of 70 tasks
charliermarsh opened this issue Jan 31, 2023 · 44 comments
Open
63 of 70 tasks

Implement remaining pycodestyle rules #2402

charliermarsh opened this issue Jan 31, 2023 · 44 comments
Labels
plugin Implementing a known but unsupported plugin

Comments

@charliermarsh
Copy link
Member

charliermarsh commented Jan 31, 2023

Note: some of the checked-off rules are still gated behind the logical_lines feature flag. To see the list of rules enabled in the current release, refer to the docs.

E1 Indentation

  • E101 ("indentation contains mixed spaces and tabs")
  • E111 ("indentation is not a multiple of four")
  • E112 ("expected an indented block")
  • E113 ("unexpected indentation")
  • E114 ("indentation is not a multiple of four (comment)")
  • E115 ("expected an indented block (comment)")
  • E116 ("unexpected indentation (comment)")
  • E117 ("over-indented")
  • E122 ("continuation line missing indentation or outdented")
  • E124 ("closing bracket does not match visual indentation")
  • E125 ("continuation line with same indent as next logical line")
  • E127 ("continuation line over-indented for visual indent")
  • E128 ("continuation line under-indented for visual indent")
  • E129 ("visually indented line with same indent as next logical line")
  • E131 ("continuation line unaligned for hanging indent")

E2 Whitespace

  • E201 ("whitespace after ‘(’")
  • E202 ("whitespace before ‘)’")
  • E203 ("whitespace before ‘,’, ‘;’, or ‘:’")
  • E211 ("whitespace before ‘(’")
  • E221 ("multiple spaces before operator")
  • E222 ("multiple spaces after operator")
  • E223 ("tab before operator")
  • E224 ("tab after operator")
  • E225 ("missing whitespace around operator")
  • E227 ("missing whitespace around bitwise or shift operator")
  • E228 ("missing whitespace around modulo operator")
  • E231 ("missing whitespace after ‘,’, ‘;’, or ‘:’")
  • E251 ("unexpected spaces around keyword / parameter equals")
  • E261 ("at least two spaces before inline comment")
  • E262 ("inline comment should start with ‘# ‘")
  • E265 ("block comment should start with ‘# ‘")
  • E266 ("too many leading ‘#’ for block comment")
  • E271 ("multiple spaces after keyword")
  • E272 ("multiple spaces before keyword")
  • E273 ("tab after keyword")
  • E274 ("tab before keyword")
  • E275 ("missing whitespace after keyword")

E3 Blank line

  • E301 ("expected 1 blank line, found 0")
  • E302 ("expected 2 blank lines, found 0")
  • E303 ("too many blank lines (3)")
  • E304 ("blank lines found after function decorator")
  • E305 ("expected 2 blank lines after end of function or class")
  • E306 ("expected 1 blank line before a nested definition")

E4 Import

  • E401 ("multiple imports on one line")
  • E402 ("module level import not at top of file")

E5 Line length

  • E501 ("line too long (82 > 79 characters)")
  • E502 ("the backslash is redundant between brackets")

E7 Statement

  • E701 ("multiple statements on one line (colon)")
  • E702 ("multiple statements on one line (semicolon)")
  • E703 ("statement ends with a semicolon")
  • E704 ("multiple statements on one line (def)")
  • E711 ("comparison to None should be ‘if cond is None:’")
  • E712 ("comparison to True should be ‘if cond is True:’ or ‘if cond:’")
  • E713 ("test for membership should be ‘not in’")
  • E714 ("test for object identity should be ‘is not’")
  • E721 ("do not compare types, use ‘isinstance()’")
  • E722 ("do not use bare except, specify exception instead")
  • E731 ("do not assign a lambda expression, use a def")
  • E741 ("do not use variables named ‘l’, ‘O’, or ‘I’")
  • E742 ("do not define classes named ‘l’, ‘O’, or ‘I’")
  • E743 ("do not define functions named ‘l’, ‘O’, or ‘I’")

E9 Runtime

  • E901 ("SyntaxError or IndentationError")
  • E902 ("IOError")

W1 Indentation warning

  • W191 ("indentation contains tabs")

W2 Whitespace warning

  • W291 ("trailing whitespace")
  • W292 ("no newline at end of file")
  • W293 ("blank line contains whitespace")

W3 Blank line warning

  • W391 ("blank line at end of file")

W5. Line break warning

  • W505 ("doc line too long (82 > 79 characters)")

W6 Deprecation warning

  • W605 ("invalid escape sequence ‘x’")
@charliermarsh charliermarsh added the plugin Implementing a known but unsupported plugin label Jan 31, 2023
@charliermarsh
Copy link
Member Author

I'm happy to support these, but I likely won't enable them by default -- I'd like them to be opt-in.

@charliermarsh
Copy link
Member Author

(Also that tabulation is incomplete, but I'll finish it up later!)

@charliermarsh
Copy link
Member Author

\cc @saadmk11

@charliermarsh
Copy link
Member Author

I've updated this lint to include all the current pydocstyle rules, less those that are ignored in the default configuration and those that deal with pre-Python 3.7 code.

@charliermarsh
Copy link
Member Author

First we need #1130.

@charliermarsh
Copy link
Member Author

So far I have seven more rules done in #1130.

@pierrepo
Copy link

pierrepo commented Feb 2, 2023

Thanks for this @charliermarsh
I'm using a lot pycodestyle and pydocstyle with my students to teach them PEP8 and PEP257. Using ruff will be a great plus.

@charliermarsh
Copy link
Member Author

#1130 is up to 14 rules. Just trying to knock out a few each days.

@charliermarsh
Copy link
Member Author

Knocked out eight more rules behind the feature flag.

@charliermarsh
Copy link
Member Author

Four more done.

@charliermarsh
Copy link
Member Author

One more in #3225.

@charliermarsh
Copy link
Member Author

One more in #3249.

@charliermarsh
Copy link
Member Author

One more in #3344.

@kapilt
Copy link

kapilt commented Mar 14, 2023

rust newb, if someone wants to try this feature set, how do we build ruff with logical-line feature flag support and these style rules enabled?

[update]
this seems to do it, following along on contributing.md

git clone https://github.com/charliermarsh/ruff.git
cd ruff
cargo build --all-features --release
cp target/release/ruff ~/bin

@spaceone
Copy link
Contributor

spaceone commented Mar 14, 2023

rust newb, if someone wants to try this feature set, how do we build ruff with logical-line feature flag support and these style rules enabled?

@charliermarsh I would like to have a pip release for ruff with that special feature as pip/setup.py "package option" / "extra"/ "extras_require" enabled. So that you can call pip install ruff[logical-line] and have this enabled. Maybe it's possible to achieve that?

@charliermarsh
Copy link
Member Author

I'm hoping that we can finish and release this soon enough that it's not worth shipping an extra_requires variant. (And if not, I'd probably rather ship with a command-line or feature flag rather than an extras, so that it's available regardless of how you install Ruff.)

@hoel-bagard
Copy link
Contributor

@charliermarsh Since #9266 got merged, the E3 rules can be checked here.

@spaceone
Copy link
Contributor

spaceone commented Feb 18, 2024

Are meanwhile all above checked rules now available, at least behind the preview flag?

@charliermarsh
Copy link
Member Author

@spaceone - Yup, they're all behind --preview.

@buhtz
Copy link
Contributor

buhtz commented Feb 28, 2024

How to handle problems with --preview error codes? Report them here or open an extra issue?

E303 (too-many-blank-lines) not working on this file for example:

# foo.py
class Foo:
    def __init__(self):
        pass


    def two(self):
        pass

Calling ruff 0.2.2 via ruff foo.py --preview.

@hoel-bagard
Copy link
Contributor

@buhtz Have you selected the E303 rule ? For example with:

ruff foo.py --preview --select E3

@buhtz
Copy link
Contributor

buhtz commented Feb 29, 2024

@buhtz Have you selected the E303 rule ? For example with:

ruff foo.py --preview --select E3

No, I have not. As discussed here and how I understand the docs the --preview flag to select all "preview" (unstable, new) rules.

EDIT: IMHO the docs are misleading.

$ ruff foo.py --preview --select E303
foo.py:7:5: E303 [*] Too many blank lines (2)
  |
7 |     def two(self):
  |     ^^^ E303
8 |         pass
  |
  = help: Remove extraneous blank line(s)

Found 1 error.
[*] 1 fixable with the `--fix` option.

@baco
Copy link

baco commented Feb 29, 2024

@buhtz Have you selected the E303 rule ? For example with:

ruff foo.py --preview --select E3

No, I have not. As discussed here and how I understand the docs the --preview flag to select all "preview" (unstable, new) rules.

Well... that's the issue. As for now, all the E3 rules have just arrived and are on their testing period, hence, the only way to try them is with the --preview flag.

You can see that here, in the Rules, and see that all the rules available under the --preview are marked with 🧪 (a test tube emoji). And the rules you are looking for:
image
all have the test tube emoji

@buhtz
Copy link
Contributor

buhtz commented Feb 29, 2024

the only way to try them is with the --preview flag.

No, you are wrong. That flag alone does not help. You have to combine --preview with --select. That is the missing key fact here. I know you know it. But some readers might misunderstand that. Wording is very important here.

@baco
Copy link

baco commented Feb 29, 2024

Ahhh, ok, ok, that's right, the combination of both flags. --preview doesn't turn on all the “unstable rules”. The flag rather stops filtering out rules matching the “selection rules” (if that's a thing) that happen to be unstable.

Is that what you are pointing out here?

@buhtz
Copy link
Contributor

buhtz commented Feb 29, 2024

Is that what you are pointing out here?

Exactly.

charliermarsh pushed a commit that referenced this issue Mar 12, 2024
## Summary

Implements the
[redundant-backslash](https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes)
rule (E502) from pycodestyle.

## Test Plan

New fixture has been added

Part of #2402
charliermarsh pushed a commit that referenced this issue Mar 12, 2024
## Summary

Implements the [blank line at end of
file](https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes)
rule (W391) from pycodestyle. Renamed to TooManyNewlinesAtEndOfFile for
clarity.

## Test Plan

New fixtures have been added

Part of #2402
@kaddkaka
Copy link

Note: some of the checked-off rules are still gated behind the logical_lines feature flag. To see the list of rules enabled in the current release, refer to the docs.

Is this still true? Is there a better way than cross referencing https://docs.astral.sh/ruff/rules/#pycodestyle-e-w with the list in this issue?

Where can I find info about "logical_lines"? A search in the docs gave 0 matches.

@MichaReiser
Copy link
Member

Note: some of the checked-off rules are still gated behind the logical_lines feature flag. To see the list of rules enabled in the current release, refer to the docs.

No, that's no longer the case. You can opt in by using --preview and enabling the desired rules (e.g. --extend-select=E3)

All checked rules in this issue are implemented as stable or preview rules and the rules use the same codes as pycodestyle. So there should be no need to cross-reference except if you need to know if the rule is preview only.

@shikari08
Copy link

Anyone know how to use the preview E3 Blank line rules in vscode with the ruff extension. I have the preview version installed in vscode.

@dhruvmanila
Copy link
Member

@Daku24 By preview, we mean https://docs.astral.sh/ruff/preview/, you don't need to have the preview version of ruff-vscode extension. If you have a local config for Ruff, you can enable preview and include E3:

[tool.ruff.lint]
preview = true
extend-select = ["E3"]

If you don't have a Ruff config file or can't create or edit it, you can use lint.args.

I hope this helps :)

@Alex-ley-scrub
Copy link

This is awesome work 👏

Once E11, E12, and E13 rules are added with automatic fixes, if we run something like this:

ruff check foo.py --fix --preview --select E1

would we would get the ~same result as something like this?:

autopep8 foo.py --in-place [--aggressive] 

If so, that would be amazing! autopep8 is really good at fixing broken indentation and adding this to ruff would be 😍

E1 rules don't have automatic fixes at the minute - is that just a case of priority/time or is there a technical challenge / performance concern / reluctance for this to be default behaviour etc. as somewhat discussed in #1130

charliermarsh added a commit that referenced this issue Jul 4, 2024
## Summary

<!-- What's the purpose of the change? What does it do, and why? -->
This is the implementation for the new rule of `pycodestyle (E204)`. It
follows the guidlines described in the contributing site, and as such it
has a new file named `whitespace_after_decorator.rs`, a new test file
called `E204.py`, and as such invokes the `function` in the `AST
statement checker` for functions and functions in classes. Linking #2402
because it has all the pycodestyle rules.

## Test Plan

<!-- How was it tested? -->
The file E204.py, has a `decorator` defined called wrapper, and this
decorator is used for 2 cases. The first one is when a `function` which
has a `decorator` is called in the file, and the second one is when
there is a `class` and 2 `methods` are defined for the `class` with a
`decorator` attached it.

Test file:

``` python
def foo(fun):
    def wrapper():
        print('before')
        fun()
        print('after')
    return wrapper

# No error
@foo
def bar():
    print('bar')

# E204
@ foo
def baz():
    print('baz')

class Test:
    # No error
    @foo
    def bar(self):
        print('bar')

    # E204
    @ foo
    def baz(self):
        print('baz')
```

I am still new to rust and any suggestion is appreciated. Specially with
the way im using native ruff utilities.

---------

Co-authored-by: Charlie Marsh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin Implementing a known but unsupported plugin
Projects
None yet
Development

No branches or pull requests