Skip to content

Commit b0dabb0

Browse files
authored
Allow pydocstyle ruff rules without enabling them (#193)
2 parents ca3deba + 71f91f3 commit b0dabb0

File tree

5 files changed

+77
-12
lines changed

5 files changed

+77
-12
lines changed

.github/pages/make_switcher.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Make switcher.json to allow docs to switch between different versions."""
2+
13
import json
24
import logging
35
from argparse import ArgumentParser
@@ -6,6 +8,7 @@
68

79

810
def report_output(stdout: bytes, label: str) -> list[str]:
11+
"""Print and return something received frm stdout."""
912
ret = stdout.decode().strip().split("\n")
1013
print(f"{label}: {ret}")
1114
return ret
@@ -52,21 +55,20 @@ def get_versions(ref: str, add: str | None) -> list[str]:
5255
return versions
5356

5457

55-
def write_json(path: Path, repository: str, versions: str):
58+
def write_json(path: Path, repository: str, versions: list[str]):
59+
"""Write the JSON switcher to path."""
5660
org, repo_name = repository.split("/")
57-
pages_url = f"https://{org}.github.io"
58-
if repo_name != f"{org}.github.io":
59-
# Only add the repo name if it isn't the source for the org pages site
60-
pages_url += f"/{repo_name}"
6161
struct = [
62-
{"version": version, "url": f"{pages_url}/{version}/"} for version in versions
62+
{"version": version, "url": f"https://{org}.github.io/{repo_name}/{version}/"}
63+
for version in versions
6364
]
6465
text = json.dumps(struct, indent=2)
6566
print(f"JSON switcher:\n{text}")
6667
path.write_text(text, encoding="utf-8")
6768

6869

6970
def main(args=None):
71+
"""Parse args and write switcher."""
7072
parser = ArgumentParser(
7173
description="Make a versions.json file from gh-pages directories"
7274
)

docs/how-to/check-docs-style.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Check docs for style
2+
3+
Ruff has the ability to check that you have:
4+
5+
- Documented all public modules, classes, methods and functions
6+
- Written your docstrings according to a particular style
7+
- Not missed parameters from method and function docstrings
8+
9+
This is not turned on by default, as it is not able to [distinguish between a missing docstring, and one that is inherited from a parent class](https://github.com/astral-sh/ruff/issues/9149)
10+
11+
## Enabling docstring checking
12+
13+
There are a number of competing docstring styles, ruff supports numpy, google and pep257. If you would like to check for the google docstring style, you can configure in ``pyproject.toml`` by:
14+
15+
- Turning on the checker
16+
17+
```toml
18+
[tool.ruff.lint]
19+
extend-select = [
20+
# ...
21+
"D", # pydocstyle - https://docs.astral.sh/ruff/rules/#pydocstyle-d
22+
# ...
23+
]
24+
```
25+
26+
- Selecting a convention
27+
28+
```toml
29+
[tool.ruff.lint.pydocstyle]
30+
convention = "google"
31+
```
32+
33+
- Ignoring docstring checking in tests
34+
35+
```toml
36+
[tool.ruff.lint.per-file-ignores]
37+
"tests/**/*" = [
38+
# ...
39+
"D", # Don't check docstrings in tests
40+
# ...
41+
]
42+
```

template/src/{{ package_name }}/__main__.py.jinja

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Interface for ``python -m {{package_name}}``."""
12
from argparse import ArgumentParser
23
from collections.abc import Sequence
34

@@ -7,6 +8,7 @@ __all__ = ["main"]
78

89

910
def main(args: Sequence[str] | None = None) -> None:
11+
"""Argument parser for the CLI."""
1012
parser = ArgumentParser()
1113
parser.add_argument(
1214
"-v",
@@ -17,6 +19,5 @@ def main(args: Sequence[str] | None = None) -> None:
1719
parser.parse_args(args)
1820

1921

20-
# test with: python -m {{package_name}}
2122
if __name__ == "__main__":
2223
main()

template/{% if sphinx %}docs{% endif %}/conf.py.jinja

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Configuration file for the Sphinx documentation builder.
2-
#
3-
# This file only contains a selection of the most common options. For a full
4-
# list see the documentation:
5-
# https://www.sphinx-doc.org/en/master/usage/configuration.html
1+
"""Configuration file for the Sphinx documentation builder.
2+
3+
This file only contains a selection of the most common options. For a full
4+
list see the documentation:
5+
https://www.sphinx-doc.org/en/master/usage/configuration.html
6+
"""
67

78
import sys
89
from pathlib import Path

tests/test_example.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,25 @@ def test_works_in_pyright_strict_mode(tmp_path: Path):
166166
run(f"./venv/bin/pyright {tmp_path}")
167167

168168

169+
def test_works_with_pydocstyle(tmp_path: Path):
170+
copy_project(tmp_path)
171+
pyproject_toml = tmp_path / "pyproject.toml"
172+
text = (
173+
pyproject_toml.read_text()
174+
.replace('"C4",', '"C4", "D",') # Enable all pydocstyle
175+
.replace(
176+
'"tests/**/*" = ["SLF001"]',
177+
# But exclude on tests and allow o put their own docstring on __init__.py
178+
'"tests/**/*" = ["SLF001", "D"]\n"__init__.py" = ["D104"]',
179+
)
180+
)
181+
pyproject_toml.write_text(text)
182+
183+
# Ensure ruff is still happy
184+
run = make_venv(tmp_path)
185+
run("ruff check")
186+
187+
169188
def test_catalog_info(tmp_path: Path):
170189
copy_project(tmp_path)
171190
catalog_info_path = tmp_path / "catalog-info.yaml"

0 commit comments

Comments
 (0)