Skip to content

Commit ca445c4

Browse files
committed
Adopt Ruff and use stricter MyPy settings
1 parent dfd5da0 commit ca445c4

File tree

9 files changed

+117
-34
lines changed

9 files changed

+117
-34
lines changed

.flake8

-4
This file was deleted.

.github/workflows/test.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ jobs:
7171
runs-on: ubuntu-latest
7272
strategy:
7373
matrix:
74-
env: [flake8, mypy]
74+
env:
75+
- ruff
76+
- mypy
7577

7678
steps:
7779
- uses: actions/checkout@v3

.ruff.toml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
target-version = "py39" # Pin Ruff to Python 3.9
2+
output-format = "full"
3+
line-length = 95
4+
5+
[lint]
6+
preview = true
7+
select = [
8+
# "ANN", # flake8-annotations
9+
"C4", # flake8-comprehensions
10+
"COM", # flake8-commas
11+
"B", # flake8-bugbear
12+
"DTZ", # flake8-datetimez
13+
"E", # pycodestyle
14+
"EM", # flake8-errmsg
15+
"EXE", # flake8-executable
16+
"F", # pyflakes
17+
"FA", # flake8-future-annotations
18+
"FLY", # flynt
19+
"FURB", # refurb
20+
"G", # flake8-logging-format
21+
"I", # isort
22+
"ICN", # flake8-import-conventions
23+
"INT", # flake8-gettext
24+
"LOG", # flake8-logging
25+
"PERF", # perflint
26+
"PGH", # pygrep-hooks
27+
"PIE", # flake8-pie
28+
"PT", # flake8-pytest-style
29+
"SIM", # flake8-simplify
30+
"SLOT", # flake8-slots
31+
"TCH", # flake8-type-checking
32+
"UP", # pyupgrade
33+
"W", # pycodestyle
34+
"YTT", # flake8-2020
35+
]
36+
ignore = [
37+
"E116",
38+
"E241",
39+
"E251",
40+
]
41+
42+
[lint.per-file-ignores]
43+
"tests/*" = [
44+
"ANN", # tests don't need annotations
45+
]
46+
47+
[lint.isort]
48+
forced-separate = [
49+
"tests",
50+
]
51+
required-imports = [
52+
"from __future__ import annotations",
53+
]

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ clean-mypyfiles:
4747

4848
.PHONY: style-check
4949
style-check:
50-
@flake8
50+
@ruff check
5151

5252
.PHONY: type-check
5353
type-check:

pyproject.toml

+33-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ test = [
4747
"pytest",
4848
]
4949
lint = [
50-
"flake8",
50+
"ruff==0.5.5",
5151
"mypy",
52-
"docutils-stubs",
52+
"types-docutils",
5353
]
5454
standalone = [
5555
"Sphinx>=5",
@@ -72,4 +72,34 @@ include = [
7272
]
7373

7474
[tool.mypy]
75-
ignore_missing_imports = true
75+
python_version = "3.9"
76+
packages = [
77+
"sphinxcontrib",
78+
"tests",
79+
]
80+
exclude = [
81+
"tests/roots",
82+
]
83+
check_untyped_defs = true
84+
disallow_any_generics = true
85+
disallow_incomplete_defs = true
86+
disallow_subclassing_any = true
87+
disallow_untyped_calls = true
88+
disallow_untyped_decorators = true
89+
disallow_untyped_defs = true
90+
explicit_package_bases = true
91+
extra_checks = true
92+
no_implicit_reexport = true
93+
show_column_numbers = true
94+
show_error_context = true
95+
strict_optional = true
96+
warn_redundant_casts = true
97+
warn_unused_configs = true
98+
warn_unused_ignores = true
99+
enable_error_code = [
100+
"type-arg",
101+
"redundant-self",
102+
"truthy-iterable",
103+
"ignore-without-code",
104+
"unused-awaitable",
105+
]

sphinxcontrib/devhelp/__init__.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@
99
import os
1010
import re
1111
from os import path
12-
from typing import Any
12+
from typing import TYPE_CHECKING, Any
1313

1414
from docutils import nodes
1515
from sphinx import addnodes
16-
from sphinx.application import Sphinx
1716
from sphinx.builders.html import StandaloneHTMLBuilder
1817
from sphinx.environment.adapters.indexentries import IndexEntries
1918
from sphinx.locale import get_translation
2019
from sphinx.util import logging
2120
from sphinx.util.nodes import NodeMatcher
2221
from sphinx.util.osutil import make_filename
2322

24-
try:
25-
import xml.etree.ElementTree as etree
26-
except ImportError:
27-
import lxml.etree as etree # type: ignore
23+
if TYPE_CHECKING:
24+
from sphinx.application import Sphinx
25+
26+
import xml.etree.ElementTree as etree
2827

2928
__version__ = '1.0.6'
3029
__version_info__ = (1, 0, 6)
@@ -80,8 +79,7 @@ def build_devhelp(self, outdir: str | os.PathLike[str], outname: str) -> None:
8079
self.config.master_doc, self, prune_toctrees=False)
8180

8281
def write_toc(node: nodes.Node, parent: etree.Element) -> None:
83-
if isinstance(node, addnodes.compact_paragraph) or \
84-
isinstance(node, nodes.bullet_list):
82+
if isinstance(node, (addnodes.compact_paragraph, nodes.bullet_list)):
8583
for subnode in node:
8684
write_toc(subnode, parent)
8785
elif isinstance(node, nodes.list_item):
@@ -93,7 +91,7 @@ def write_toc(node: nodes.Node, parent: etree.Element) -> None:
9391
parent.attrib['name'] = node.astext()
9492

9593
matcher = NodeMatcher(addnodes.compact_paragraph, toctree=Any)
96-
for node in tocdoc.findall(matcher): # type: addnodes.compact_paragraph
94+
for node in tocdoc.findall(matcher):
9795
write_toc(node, chapters)
9896

9997
# Index
@@ -115,7 +113,7 @@ def write_index(title: str, refs: list[Any], subitems: Any) -> None:
115113
if subitems:
116114
parent_title = re.sub(r'\s*\(.*\)\s*$', '', title)
117115
for subitem in subitems:
118-
write_index("%s %s" % (parent_title, subitem[0]),
116+
write_index(f'{parent_title} {subitem[0]}',
119117
subitem[1], [])
120118

121119
for (_group_key, group) in index:
@@ -125,7 +123,7 @@ def write_index(title: str, refs: list[Any], subitems: Any) -> None:
125123
# Dump the XML file
126124
xmlfile = path.join(outdir, outname + '.devhelp.gz')
127125
with gzip.GzipFile(filename=xmlfile, mode='w', mtime=0) as f:
128-
tree.write(f, 'utf-8') # type: ignore
126+
tree.write(f, 'utf-8')
129127

130128

131129
def setup(app: Sphinx) -> dict[str, Any]:

tests/conftest.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24

35
import pytest
46

5-
import sphinx
6-
7-
pytest_plugins = 'sphinx.testing.fixtures'
7+
pytest_plugins = (
8+
'sphinx.testing.fixtures',
9+
)
810

911

1012
@pytest.fixture(scope='session')
11-
def rootdir():
12-
if sphinx.version_info[:2] < (7, 2):
13-
from sphinx.testing.path import path
14-
15-
return path(__file__).parent.abspath() / 'roots'
16-
13+
def rootdir() -> Path:
1714
return Path(__file__).resolve().parent / 'roots'

tests/test_devhelp.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
"""Test for devhelp extension."""
2+
3+
from __future__ import annotations
4+
25
from time import sleep
6+
from typing import TYPE_CHECKING
37

48
import pytest
59

10+
if TYPE_CHECKING:
11+
from sphinx.application import Sphinx
12+
613

714
@pytest.mark.sphinx('devhelp', testroot='basic')
8-
def test_basic(app, status, warning):
15+
def test_basic(app: Sphinx) -> None:
916
app.builder.build_all()
1017

1118

1219
@pytest.mark.sphinx('devhelp', testroot='basic', freshenv=True)
13-
def test_basic_deterministic_build(app):
20+
def test_basic_deterministic_build(app: Sphinx) -> None:
1421
app.config.devhelp_basename, output_filename = 'testing', 'testing.devhelp.gz'
1522

1623
app.builder.build_all()

tox.ini

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
minversion = 2.4.0
33
envlist =
44
py{39,310,311,312,313},
5-
flake8,
5+
ruff,
66
mypy
77
isolated_build = True
88

@@ -17,14 +17,14 @@ setenv =
1717
commands=
1818
pytest --durations 25 {posargs}
1919

20-
[testenv:flake8]
20+
[testenv:ruff]
2121
description =
2222
Run style checks.
2323
extras =
2424
test
2525
lint
2626
commands=
27-
flake8
27+
ruff check
2828

2929
[testenv:mypy]
3030
description =

0 commit comments

Comments
 (0)