astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
31.52k stars 1.06k forks source link

Isort and Ruff Isort implementation incompatibilities #1876

Closed dbritto-dev closed 1 year ago

dbritto-dev commented 1 year ago

test_linting.py

import json
import datetime as dt

_ = json.dumps({})
__ = dt.datetime.now()

pyproject.toml

[project]
version = "1.0.0"
description = "<some-description>"
name = "<some-name>"
requires-python = ">=3.8"
dependencies = [
  "fastapi", "python-dotenv", "beanie", "auth0-python", "auth0-jwt-validator",
  "hypercorn", "slack-sdk"
]

[project.optional-dependencies]
dev = [
  "nox", "ruff", "black", "pytest", "pytest-cov", "safety", "bandit", "debugpy"
]
lint = ["ruff", "black"]
test = ["nox", "pytest", "pytest-cov"]
security-test = ["safety", "bandit"]

[tool.setuptools.packages]
find = {}

[tool.ruff]
line-length = 99
target-version = "py310"
select = ["B", "C", "E", "F", "W"]
ignore = ["E501", "B904", "B008"]
exclude = [
  "build", "configs", "*.egg-info", ".github", ".githooks", ".vscode", ".nox"
]

[tool.ruff.pydocstyle]
convention = "google"

[tool.black]
line-length = 99
target-version = ['py310']
preview = true

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = ["tests"]

[tool.coverage.run]
branch = true
source = ["app"]

Result

image

Info

image

Note:

Since ruff wasn't able to find any issue with the imports, it wasn't able to fix the sort imports

charliermarsh commented 1 year ago

You need to add "I" to your select, to enable import sorting -- like:

[tool.ruff]
line-length = 99
target-version = "py310"
select = ["B", "C", "E", "F", "W", "I"]
ignore = ["E501", "B904", "B008"]
exclude = [
  "build", "configs", "*.egg-info", ".github", ".githooks", ".vscode", ".nox"
]
charliermarsh commented 1 year ago

I think that should resolve the issue -- but let me know if you see otherwise!

❯ ruff --select I foo.py --diff
--- foo.py
+++ foo.py
@@ -1,5 +1,5 @@
+import datetime as dt
 import json
-import datetime as dt

 _ = json.dumps({})
 __ = dt.datetime.now()

Would fix 1 error(s).
dbritto-dev commented 1 year ago

Thanks, I would try it