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

Discrepancies between isort and Ruff's I lint check #1718

Closed
AA-Turner opened this issue Jan 7, 2023 · 12 comments · Fixed by #1722
Closed

Discrepancies between isort and Ruff's I lint check #1718

AA-Turner opened this issue Jan 7, 2023 · 12 comments · Fixed by #1722
Labels
isort Related to import sorting

Comments

@AA-Turner
Copy link
Contributor

AA-Turner commented Jan 7, 2023

It seems Ruff expects a space after isort:skip (i.e. Ruff wants isort: skip), and Ruff seems to ignore F401 for the purposes of import sorting, whereas isort honours them as skip markers(?).

Ruff also reports 1 error in the initial call, yet the --diff call reports three errors.

Ruff version 213; isort version 5.11.4

Sample file:

(sphinx) PS I:\Development\sphinx> type bug.py            
from operator import add  # noqa: F401
from operator import sub  # noqa: F401

from math import pi  # noqa: F401  isort:skip
from math import e  # noqa: F401  isort:skip

from string import (  # noqa: E402  # isort:skip
    ascii_lowercase
)

from pprint import pprint  # isort:skip
(sphinx) PS I:\Development\sphinx> isort bug.py           
(sphinx) PS I:\Development\sphinx> ruff bug.py --select I
bug.py:1:1: I001 Import block is un-sorted or un-formatted
Found 1 error(s).
1 potentially fixable with the --fix option.
(sphinx) PS I:\Development\sphinx> ruff bug.py --diff    
--- bug.py
+++ bug.py
@@ -1,11 +1,8 @@
-from operator import add  # noqa: F401
-from operator import sub  # noqa: F401
-
-from math import pi  # noqa: F401  isort:skip
-from math import e  # noqa: F401  isort:skip
-
-from string import (  # noqa: E402  # isort:skip
-    ascii_lowercase
+from math import (
+    e,  # noqa: F401  isort:skip
+    pi,  # noqa: F401  isort:skip
+)
+from operator import (
+    add,  # noqa: F401
+    sub,  # noqa: F401
 )
-
-from pprint import pprint  # isort:skip

Would fix 3 error(s).
(sphinx) PS I:\Development\sphinx> ruff -V           
ruff 0.0.213
(sphinx) PS I:\Development\sphinx> 

A

@charliermarsh
Copy link
Member

Yeah this was sort of a known thing when I implemented it, but maybe that was a bad decision. (Both Ruff and isort should respect the version with the space, but Ruff doesn't respect the version without the space, IIUC.)

@charliermarsh
Copy link
Member

I fixed the # isort:skip part. Will look at the error count too.

@AA-Turner
Copy link
Contributor Author

Thanks for the quick fix!

A

@charliermarsh
Copy link
Member

When you ran ruff bug.py --diff , what other error codes were enabled? (Since there's no --select I.)

@AA-Turner
Copy link
Contributor Author

Ahh, sorry:

tool.ruff.select:

select = [
    "E",   # pycodestyle
    "F",   # Pyflakes
    "W",   # pycodestyle
    # plugins:
    "B",   # flake8-bugbear
    "C4",  # flake8-comprehensions
    "PGH", # pygrep-hooks
    "PLC", # pylint
    "PLE", # pylint
    "PLR", # pylint
    "PLW", # pylint
    "S",   # flake8-bandit
    "SIM", # flake8-simplify
    "T10", # flake8-debugger
    "UP",  # pyupgrade
    "RUF100",  # yesqa
]

@AA-Turner
Copy link
Contributor Author

Full Ruff section:
[tool.ruff]
target-version = "py38"  # Pin Ruff to Python 3.8
line-length = 95
exclude = [
    '.git',
    '.tox',
    '.venv',
    'tests/roots/*',
    'build/*',
    'doc/_build/*',
    'sphinx/search/*',
    'doc/usage/extensions/example*.py',
]
ignore = [
    # pycodestyle
    'E741',
    # flake8-bugbear
    'B006',
    'B023',
    # flake8-bugbear opinionated (disabled by default in flake8)
    'B904',
    'B905',
    # pygrep-hooks
    "PGH003",
    # flake8-bandit
    "S101",  # assert used
    "S105",  # possible hardcoded password
    "S113",  # probable use of requests call without timeout
    "S324",  # probable use of insecure hash functions
    # flake8-simplify
    "SIM102", # nested 'if' statements
    "SIM103", # return condition directly
    "SIM105", # use contextlib.suppress
    "SIM108", # use ternary operator
    "SIM117", # use single 'with' statement
]
external = [  # Whitelist for RUF100 unkown code warnings
    "E704",
    "W291",
    "W293",
    "SIM110",
    "SIM113",
]
select = [
    "E",   # pycodestyle
    "F",   # Pyflakes
    "W",   # pycodestyle
    # plugins:
    "B",   # flake8-bugbear
    "C4",  # flake8-comprehensions
    "PGH", # pygrep-hooks
    "PLC", # pylint
    "PLE", # pylint
    "PLR", # pylint
    "PLW", # pylint
    "S",   # flake8-bandit
    "SIM", # flake8-simplify
    "T10", # flake8-debugger
    "UP",  # pyupgrade
    "RUF100",  # yesqa
]

@AA-Turner
Copy link
Contributor Author

When you ran ruff bug.py --diff , what other error codes were enabled? (Since there's no --select I.)

flake8's --isolated would be useful in general for bug reporting, as it means hidden config state can't affect the output of Ruff -- I reached for it the first few times I wanted to report a bug but it doesn't exist (yet!).

A

@charliermarsh
Copy link
Member

Would that force Ruff to use the default settings (+ whatever's passed in on the command-line)?

@charliermarsh
Copy link
Member

Ahh cool. Yes, seeing that now in Flake8. That would be straightforward to support.

@charliermarsh
Copy link
Member

Tracking here: #1724.

@charliermarsh
Copy link
Member

With regards to the above: I'm just gonna chalk it up to Ruff fixing other errors (like the unused pprint) for now. If you see other confusing behavior, feel free to open another issue :)

@AA-Turner
Copy link
Contributor Author

Brilliant, thank you!

A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
isort Related to import sorting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants