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

refactor(tools): Switch to ruff for import sorting. #1168

Merged
merged 11 commits into from
Mar 29, 2024
14 changes: 1 addition & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,13 @@ 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.3
hooks:
- id: ruff
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 2 additions & 2 deletions disnake/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ def __init__(
self.description: Optional[str] = str(description) if description is not None else None
self.url: Optional[str] = str(url) if url is not None else None

self.timestamp = timestamp
self.timestamp = timestamp # noqa: PLE0237 # false positive
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved

# possible values:
# - MISSING: embed color will be _default_color
# - None: embed color will not be set
# - Color: embed color will be set to specified color
if colour is not MISSING:
color = colour
self.colour = color
self.colour = color # noqa: PLE0237 # false positive

self._thumbnail: Optional[EmbedThumbnailPayload] = None
self._video: Optional[EmbedVideoPayload] = None
Expand Down
6 changes: 6 additions & 0 deletions examples/ruff.toml
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: MIT

extend = "../pyproject.toml"

[lint.isort]
known-third-party = ["disnake"]
29 changes: 15 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.3",
]
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",
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved
]
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 @@ -222,9 +216,10 @@ ignore = [
"E501", # line too long
"E731", # assigning lambdas to variables
"T201", # print statements
"S311", # insecure RNG usage, we don't use these for security-related things
]

[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 +245,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
Loading