Skip to content

Commit c7a1ca2

Browse files
committed
Adopt Ruff and use stricter MyPy settings
1 parent 3161d47 commit c7a1ca2

File tree

11 files changed

+128
-33
lines changed

11 files changed

+128
-33
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
@@ -48,9 +48,9 @@ test = [
4848
"pytest",
4949
]
5050
lint = [
51-
"flake8",
51+
"ruff==0.5.5",
5252
"mypy",
53-
"docutils-stubs",
53+
"types-docutils",
5454
]
5555
standalone = [
5656
"Sphinx>=5",
@@ -76,4 +76,34 @@ exclude = [
7676
]
7777

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

sphinxcontrib/serializinghtml/__init__.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pickle
55
import types
66
from os import path
7-
from typing import Any
7+
from typing import TYPE_CHECKING
88

99
from sphinx.application import ENV_PICKLE_FILENAME, Sphinx
1010
from sphinx.builders.html import BuildInfo, StandaloneHTMLBuilder
@@ -13,6 +13,16 @@
1313

1414
from sphinxcontrib.serializinghtml import jsonimpl
1515

16+
if TYPE_CHECKING:
17+
from collections.abc import Sequence
18+
from typing import Any, Protocol
19+
20+
class SerialisingImplementation(Protocol):
21+
def dump(self, obj: Any, file: Any, *args: Any, **kwargs: Any) -> None: ...
22+
def dumps(self, obj: Any, *args: Any, **kwargs: Any) -> str | bytes: ...
23+
def load(self, file: Any, *args: Any, **kwargs: Any) -> Any: ...
24+
def loads(self, data: Any, *args: Any, **kwargs: Any) -> Any: ...
25+
1626
__version__ = '1.1.10'
1727
__version_info__ = (1, 1, 10)
1828

@@ -31,11 +41,11 @@ class SerializingHTMLBuilder(StandaloneHTMLBuilder):
3141
"""
3242
#: the serializing implementation to use. Set this to a module that
3343
#: implements a `dump`, `load`, `dumps` and `loads` functions
34-
#: (pickle, simplejson etc.)
35-
implementation: Any = None
44+
#: (pickle, json etc.)
45+
implementation: SerialisingImplementation
3646
implementation_dumps_unicode = False
3747
#: additional arguments for dump()
38-
additional_dump_args: tuple = ()
48+
additional_dump_args: Sequence[Any] = ()
3949

4050
#: the filename for the global context file
4151
globalcontext_filename: str = ''
@@ -62,7 +72,7 @@ def get_target_uri(self, docname: str, typ: str | None = None) -> str:
6272
return docname[:-5] # up to sep
6373
return docname + SEP
6474

65-
def dump_context(self, context: dict, filename: str | os.PathLike[str]) -> None:
75+
def dump_context(self, context: dict[str, Any], filename: str | os.PathLike[str]) -> None:
6676
context = context.copy()
6777
if 'css_files' in context:
6878
context['css_files'] = [css.filename for css in context['css_files']]
@@ -75,7 +85,7 @@ def dump_context(self, context: dict, filename: str | os.PathLike[str]) -> None:
7585
with open(filename, 'wb') as fb:
7686
self.implementation.dump(context, fb, *self.additional_dump_args)
7787

78-
def handle_page(self, pagename: str, ctx: dict, templatename: str = 'page.html',
88+
def handle_page(self, pagename: str, ctx: dict[str, Any], templatename: str = 'page.html',
7989
outfilename: str | None = None, event_arg: Any = None) -> None:
8090
ctx['current_page_name'] = pagename
8191
ctx.setdefault('pathto', lambda p: p)
@@ -132,7 +142,7 @@ class PickleHTMLBuilder(SerializingHTMLBuilder):
132142

133143
implementation = pickle
134144
implementation_dumps_unicode = False
135-
additional_dump_args = (pickle.HIGHEST_PROTOCOL,)
145+
additional_dump_args: tuple[Any] = (pickle.HIGHEST_PROTOCOL,)
136146
indexer_format = pickle
137147
indexer_dumps_unicode = False
138148
out_suffix = '.fpickle'

sphinxcontrib/serializinghtml/jsonimpl.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import json
66
from collections import UserString
7-
from typing import Any, IO
7+
from typing import IO, Any
88

99

1010
class SphinxJSONEncoder(json.JSONEncoder):
@@ -15,9 +15,9 @@ def default(self, obj: Any) -> str:
1515
return super().default(obj)
1616

1717

18-
def dump(obj: Any, fp: IO, *args: Any, **kwds: Any) -> None:
18+
def dump(obj: Any, file: IO[str] | IO[bytes], *args: Any, **kwds: Any) -> None:
1919
kwds['cls'] = SphinxJSONEncoder
20-
json.dump(obj, fp, *args, **kwds)
20+
json.dump(obj, file, *args, **kwds)
2121

2222

2323
def dumps(obj: Any, *args: Any, **kwds: Any) -> str:

sphinxcontrib/serializinghtml/py.typed

Whitespace-only changes.

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_serializinghtml.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
"""Test for serializinghtml extension."""
22

3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
37
import pytest
48

9+
if TYPE_CHECKING:
10+
from sphinx.application import Sphinx
11+
512

613
@pytest.mark.sphinx('json', testroot='basic')
7-
def test_json(app, status, warning):
14+
def test_json(app: Sphinx) -> None:
815
app.builder.build_all()
916

1017

1118
@pytest.mark.sphinx('pickle', testroot='basic')
12-
def test_pickle(app, status, warning):
19+
def test_pickle(app: Sphinx) -> None:
1320
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

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

21-
[testenv:flake8]
21+
[testenv:ruff]
2222
description =
2323
Run style checks.
2424
extras =
2525
test
2626
lint
2727
commands=
28-
flake8
28+
ruff check
2929

3030
[testenv:mypy]
3131
description =

0 commit comments

Comments
 (0)