Skip to content

Use Ruff linting #277

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

Merged
merged 1 commit into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.5
hooks:
- id: ruff
- id: ruff-format

- repo: https://github.com/python-jsonschema/check-jsonschema
Expand Down
22 changes: 22 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,25 @@ output-format = "full"
[format]
preview = true
docstring-code-format = true

[lint]
preview = true
select = [
"C4", # flake8-comprehensions
"B", # flake8-bugbear
"E", # pycodestyle
"F", # pyflakes
"FA", # flake8-future-annotations
"FLY", # flynt
"I", # isort
"N", # pep8-naming
"PERF", # perflint
"PGH", # pygrep-hooks
"PT", # flake8-pytest-style
"TC", # flake8-type-checking
"UP", # pyupgrade
"W", # pycodestyle
]
ignore = [
"E501", # Ignore line length errors (we use auto-formatting)
]
42 changes: 22 additions & 20 deletions build_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,19 @@
from urllib.parse import urljoin

import jinja2
import platformdirs
import tomlkit
import urllib3
import zc.lockfile
from platformdirs import user_config_path, site_config_path

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Collection, Iterator, Sequence, Set
from typing import Literal

try:
from os import EX_OK, EX_SOFTWARE as EX_FAILURE
from os import EX_OK
from os import EX_SOFTWARE as EX_FAILURE
except ImportError:
EX_OK, EX_FAILURE = 0, 1

Expand Down Expand Up @@ -279,7 +280,7 @@
"""Filter a sequence of languages according to --languages."""
if language_tags:
language_tags = frozenset(language_tags)
return [l for l in self if l.tag in language_tags]
return [l for l in self if l.tag in language_tags] # NoQA: E741

Check warning on line 283 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L283

Added line #L283 was not covered by tests
return list(self)


Expand Down Expand Up @@ -480,7 +481,7 @@
- Cross-link various languages in a language switcher
- Cross-link various versions in a version switcher
"""
language_pairs = sorted((l.tag, l.switcher_label) for l in languages if l.in_prod)
language_pairs = sorted((l.tag, l.switcher_label) for l in languages if l.in_prod) # NoQA: E741

Check warning on line 484 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L484

Added line #L484 was not covered by tests
version_pairs = [(v.name, v.picker_label) for v in reversed(versions)]

switchers_template_file = HERE / "templates" / "switchers.js"
Expand Down Expand Up @@ -1057,28 +1058,29 @@


def load_environment_variables() -> None:
_user_config_path = user_config_path("docsbuild-scripts")
_site_config_path = site_config_path("docsbuild-scripts")
if _user_config_path.is_file():
ENV_CONF_FILE = _user_config_path
elif _site_config_path.is_file():
ENV_CONF_FILE = _site_config_path
dbs_user_config = platformdirs.user_config_path("docsbuild-scripts")
dbs_site_config = platformdirs.site_config_path("docsbuild-scripts")
if dbs_user_config.is_file():
env_conf_file = dbs_user_config
elif dbs_site_config.is_file():
env_conf_file = dbs_site_config

Check warning on line 1066 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L1061-L1066

Added lines #L1061 - L1066 were not covered by tests
else:
logging.info(
"No environment variables configured. "
f"Configure in {_site_config_path} or {_user_config_path}."
f"Configure in {dbs_site_config} or {dbs_user_config}."
)
return

logging.info(f"Reading environment variables from {ENV_CONF_FILE}.")
if ENV_CONF_FILE == _site_config_path:
logging.info(f"You can override settings in {_user_config_path}.")
elif _site_config_path.is_file():
logging.info(f"Overriding {_site_config_path}.")
with open(ENV_CONF_FILE, "r") as f:
for key, value in tomlkit.parse(f.read()).get("env", {}).items():
logging.debug(f"Setting {key} in environment.")
os.environ[key] = value
logging.info(f"Reading environment variables from {env_conf_file}.")
if env_conf_file == dbs_site_config:
logging.info(f"You can override settings in {dbs_user_config}.")
elif dbs_site_config.is_file():
logging.info(f"Overriding {dbs_site_config}.")

Check warning on line 1078 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L1074-L1078

Added lines #L1074 - L1078 were not covered by tests

env_config = env_conf_file.read_text(encoding="utf-8")
for key, value in tomlkit.parse(env_config).get("env", {}).items():
logging.debug(f"Setting {key} in environment.")
os.environ[key] = value

Check warning on line 1083 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L1080-L1083

Added lines #L1080 - L1083 were not covered by tests


def build_docs_with_lock(args: argparse.Namespace, lockfile_name: str) -> int:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_build_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


@pytest.mark.parametrize(
"seconds, expected",
("seconds", "expected"),
[
(0.4, "0s"),
(0.5, "0s"),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_build_docs_versions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from build_docs import Versions, Version
from build_docs import Version, Versions


def test_filter_default() -> None:
Expand Down