Skip to content

Commit

Permalink
refactor(tools): Switch to ruff for import sorting. (#1168)
Browse files Browse the repository at this point in the history
Signed-off-by: lena <[email protected]>
Co-authored-by: shiftinv <[email protected]>
  • Loading branch information
elenakrittik and shiftinv authored Mar 29, 2024
1 parent 81c4c1f commit 2919c61
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
15 changes: 2 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,14 @@ repos:
exclude_types: [json, pofile]
exclude: 'changelog/|py.typed|disnake/bin/COPYING|.github/PULL_REQUEST_TEMPLATE.md|.github/CODEOWNERS|LICENSE|MANIFEST.in'

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black", "--extend-skip", "examples"]
name: "run isort in all files"
exclude: ^examples/
- id: isort
args: ["--profile", "black", "--thirdparty", "disnake"]
name: "run isort in examples"
files: ^examples/

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.1
hooks:
- id: black
name: "run black in all files"

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.292
rev: v0.3.4
hooks:
- id: ruff
args: [--fix, --fixable=I]
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Once PDM is installed, use the following command to initialize a virtual environ
$ pdm run setup_env
```

Other tools used in this project include [black](https://black.readthedocs.io/en/stable/) + [isort](https://pycqa.github.io/isort/) (formatters), [ruff](https://beta.ruff.rs/docs/) (linter), and [pyright](https://microsoft.github.io/pyright/#/) (type-checker). For the most part, these automatically run on every commit with no additional action required - see below for details.
Other tools used in this project include [black](https://black.readthedocs.io/en/stable/) (formatter), [ruff](https://beta.ruff.rs/docs/) (linter), and [pyright](https://microsoft.github.io/pyright/#/) (type-checker). For the most part, these automatically run on every commit with no additional action required - see below for details.

All of the following checks also automatically run for every PR on GitHub, so don't worry if you're not sure whether you missed anything. A PR cannot be merged as long as there are any failing checks.

Expand Down
7 changes: 7 additions & 0 deletions examples/.ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: MIT

extend = "../pyproject.toml"
src = ["../"]

[lint.isort]
known-third-party = ["disnake"]
31 changes: 17 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ tools = [
"slotscheck~=0.16.4",
"python-dotenv~=1.0.0",
"check-manifest==0.49",
"ruff==0.0.292",
"ruff==0.3.4",
]
changelog = [
"towncrier==23.6.0",
Expand Down Expand Up @@ -106,7 +106,6 @@ build = [
[tool.pdm.scripts]
black = { composite = ["lint black"], help = "Run black" }
docs = { cmd = "nox -Rs docs --", help = "Build the documentation for development" }
isort = { composite = ["lint isort"], help = "Run isort" }
lint = { cmd = "nox -Rs lint --", help = "Check all files for linting errors" }
pyright = { cmd = "nox -Rs pyright --", help = "Run pyright" }
setup_env = { cmd = "pdm install -d -G speed -G docs -G voice", help = "Set up the local environment and all dependencies" }
Expand All @@ -117,7 +116,6 @@ test = { cmd = "nox -Rs test --", help = "Run pytest" }
[tool.taskipy.tasks]
black = { cmd = "black", help = "Run black" }
docs = { cmd = "docs", help = "Build the documentation for development" }
isort = { cmd = "isort", help = "Run isort" }
lint = { cmd = "lint", help = "Check all files for linting errors" }
pyright = { cmd = "pyright", help = "Run pyright" }
setup_env = { cmd = "setup_env", help = "Setup the local environment and set up all dependencies" }
Expand All @@ -130,16 +128,11 @@ runner = "pdm run"
line-length = 100
target-version = ["py38", "py39", "py310", "py311", "py312"]

[tool.isort]
profile = "black"
py_version = 38
line_length = 100
combine_as_imports = true
filter_files = true

[tool.ruff]
line-length = 100
target-version = "py38"

[tool.ruff.lint]
select = [
# commented out codes are intended to be enabled in future prs
"F", # pyflakes
Expand Down Expand Up @@ -169,6 +162,7 @@ select = [
# "PLR", # pylint refactor
"PLW", # pylint warnings
"TRY002", "TRY004", "TRY201", # tryceratops
"I", # isort
]
ignore = [
# star imports
Expand Down Expand Up @@ -200,7 +194,7 @@ ignore = [
# typevar names don't match variance (we don't always want this)
"PLC0105",

# import aliases are fixed by isort
# import aliases are fixed by ruff
"PLC0414",

# outer loop variables are overwritten by inner assignment target, these are mostly intentional
Expand All @@ -213,6 +207,9 @@ ignore = [
"TCH002",
"TCH003",

"S311", # insecure RNG usage, we don't use these for security-related things
"PLE0237", # pyright seems to catch this already

# temporary disables, to fix later
"D205", # blank line required between summary and description
"D401", # first line of docstring should be in imperative mood
Expand All @@ -224,7 +221,7 @@ ignore = [
"T201", # print statements
]

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"disnake/__main__.py" = ["T201"] # print statements are okay in our simple cli
"disnake/i18n.py" = [
"B027", # lib bug. Excluded here because ruff does not have a --disable-noqa flag yet
Expand All @@ -250,11 +247,17 @@ ignore = [
"examples/basic_voice.py" = ["S104"] # possible binding to all interfaces
"examples/views/tic_tac_toe.py" = ["E741"] # ambigious variable name: `O`

[tool.ruff.flake8-pytest-style]
[tool.ruff.lint.isort]
combine-as-imports = true

[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.ruff.lint.flake8-pytest-style]
fixture-parentheses = false
mark-parentheses = false

[tool.ruff.flake8-tidy-imports.banned-api]
[tool.ruff.lint.flake8-tidy-imports.banned-api]
"subprocess".msg = "Consider possible security implications associated with the subprocess module." # replaces S404

[tool.towncrier]
Expand Down

0 comments on commit 2919c61

Please sign in to comment.