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

[E231] Inconsistent catch compared to pycodestyle, such as when dict nested in list #10113

Closed
dfrtz opened this issue Feb 24, 2024 · 2 comments · Fixed by #10469
Closed

[E231] Inconsistent catch compared to pycodestyle, such as when dict nested in list #10113

dfrtz opened this issue Feb 24, 2024 · 2 comments · Fixed by #10469
Labels
bug Something isn't working

Comments

@dfrtz
Copy link

dfrtz commented Feb 24, 2024

Searching through closed and opened issues, I did not see anywhere that this is intentional or known, apologies if I missed something. It appears that in certain scenarios E231 is not triggering as expected, whereas in pycodestyle is it being caught. It may impact other scenarios, but the primary one I could find is it not working on dicts inside a list. Strangely enough it does work in tuples, and outside lists, just not in lists. Even stranger, only if not the first key/value; it does work if it is the first key/value in the dict in the list.

pyproject.toml

[tool.ruff.lint]
select = [
  "E",
  "W",
]

Python file

"""Minimal repo."""


def main() -> None:
    """Primary function."""
    results = {
        "k1": [1],
        "k2":[2],
    }
    results_in_tuple = (
        {
            "k1": [1],
            "k2":[2],
        },
    )
    results_in_list = [
        {
            "k1": [1],
            "k2":[2],
        }
    ]
    results_in_list_first = [
        {
            "k2":[2],
        }
    ]


main()

pycodestyle 2.11.1 catching all 4

$ pycodestyle --count min_repo.py
min_repo.py:8:13: E231 missing whitespace after ':'
min_repo.py:13:17: E231 missing whitespace after ':'
min_repo.py:19:17: E231 missing whitespace after ':'
min_repo.py:24:17: E231 missing whitespace after ':'
4

ruff 0.2.2 catching 3/4

$ ruff --version
ruff 0.2.2
$ ruff check --preview min_repo.py
min_repo.py:8:13: E231 [*] Missing whitespace after ':'
   |
 6 |     results = {
 7 |         "k1": [1],
 8 |         "k2":[2],
   |             ^ E231
 9 |     }
10 |     results_in_tuple = (
   |
   = help: Add missing whitespace

min_repo.py:13:17: E231 [*] Missing whitespace after ':'
   |
11 |         {
12 |             "k1": [1],
13 |             "k2":[2],
   |                 ^ E231
14 |         },
15 |     )
   |
   = help: Add missing whitespace

min_repo.py:24:17: E231 [*] Missing whitespace after ':'
   |
22 |     results_in_list_first = [
23 |         {
24 |             "k2":[2],
   |                 ^ E231
25 |         }
26 |     ]
   |
   = help: Add missing whitespace

Found 3 errors.
[*] 3 fixable with the `--fix` option.

To note, if using ruff format, it would fix this, so not a blocker if you are also using format along with check. If you are only using ruff to replace pycodestyle without format (for whatever reason, such as your project not being ready/compatible with auto format), then you would hit this miss if only using ruff and not pycodestyle.

$ ruff format --check min_repo.py --diff
--- min_repo.py
+++ min_repo.py
@@ -5,23 +5,23 @@
     """Primary function."""
     results = {
         "k1": [1],
-        "k2":[2],
+        "k2": [2],
     }
     results_in_tuple = (
         {
             "k1": [1],
-            "k2":[2],
+            "k2": [2],
         },
     )
     results_in_list = [
         {
             "k1": [1],
-            "k2":[2],
+            "k2": [2],
         }
     ]
     results_in_list_first = [
         {
-            "k2":[2],
+            "k2": [2],
         }
     ]


1 file would be reformatted

I know E231 is still preview, and this is a pretty minor miss, but I assume the goal is for parity with pycodestyle (and consistency across nested objects).

@charliermarsh
Copy link
Member

Nice, thanks!

@charliermarsh charliermarsh added the bug Something isn't working label Feb 24, 2024
@charliermarsh
Copy link
Member

Great write-up.

MichaReiser pushed a commit that referenced this issue Mar 21, 2024
…hen dict nested in list (#10469)

<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

Fix `E231` bug: Inconsistent catch compared to pycodestyle, such as when
dict nested in list. Resolves #10113.

## Test Plan

Example from #10113 added to test fixture.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants