Skip to content

Commit fdf0e47

Browse files
committed
ci: Quality
1 parent c8aacd7 commit fdf0e47

File tree

8 files changed

+46
-26
lines changed

8 files changed

+46
-26
lines changed

config/mypy.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ ignore_missing_imports = true
33
exclude = tests/fixtures/
44
warn_unused_ignores = true
55
show_error_codes = true
6+
namespace_packages = true
7+
explicit_package_bases = true

duties.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def check_types(ctx: Context) -> None:
124124
Parameters:
125125
ctx: The context instance (passed automatically).
126126
"""
127+
os.environ["MYPYPATH"] = "src"
127128
ctx.run(
128129
mypy.run(*PY_SRC_LIST, config_file="config/mypy.ini"),
129130
title=pyprefix("Type-checking"),

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ site_url: "https://mkdocstrings.github.io/python-legacy"
44
repo_url: "https://github.com/mkdocstrings/python-legacy"
55
repo_name: "mkdocstrings/python-legacy"
66
site_dir: "site"
7-
watch: [mkdocs.yml, README.md, CONTRIBUTING.md, CHANGELOG.md, src/mkdocstrings]
7+
watch: [mkdocs.yml, README.md, CONTRIBUTING.md, CHANGELOG.md, src/mkdocstrings_handlers]
88
copyright: Copyright © 2021 Timothée Mazzucotelli
99
edit_uri: edit/main/docs/
1010

src/mkdocstrings_handlers/python/handler.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import traceback
1313
from collections import ChainMap
1414
from subprocess import PIPE, Popen
15-
from typing import TYPE_CHECKING, Any, BinaryIO, Iterator
15+
from typing import TYPE_CHECKING, Any, BinaryIO, ClassVar, Iterator, Mapping, MutableMapping
1616

1717
from mkdocstrings_handlers.python.rendering import (
1818
do_brief_xref,
@@ -48,7 +48,7 @@ class PythonHandler(BaseHandler):
4848
enable_inventory: bool = True
4949

5050
fallback_theme = "material"
51-
fallback_config = {"docstring_style": "markdown", "filters": ["!.*"]}
51+
fallback_config: ClassVar[dict] = {"docstring_style": "markdown", "filters": ["!.*"]}
5252
"""The configuration used when falling back to re-collecting an object to get its anchor.
5353
5454
This configuration is used in [`Handlers.get_anchors`][mkdocstrings.handlers.base.Handlers.get_anchors].
@@ -63,7 +63,7 @@ class PythonHandler(BaseHandler):
6363
which we know will not generate any warnings.
6464
"""
6565

66-
default_config: dict = {
66+
default_config: ClassVar[dict] = {
6767
"filters": ["!^_[^_]"],
6868
"show_root_heading": False,
6969
"show_root_toc_entry": True,
@@ -122,11 +122,11 @@ class PythonHandler(BaseHandler):
122122

123123
def __init__(
124124
self,
125-
*args,
125+
*args: Any,
126126
setup_commands: list[str] | None = None,
127127
config_file_path: str | None = None,
128128
paths: list[str] | None = None,
129-
**kwargs,
129+
**kwargs: Any,
130130
) -> None:
131131
"""Initialize the handler.
132132
@@ -153,7 +153,7 @@ def __init__(
153153
search_paths = []
154154
for path in paths:
155155
if not os.path.isabs(path) and config_file_path:
156-
path = os.path.abspath(os.path.join(os.path.dirname(config_file_path), path))
156+
path = os.path.abspath(os.path.join(os.path.dirname(config_file_path), path)) # noqa: PLW2901
157157
if path not in search_paths:
158158
search_paths.append(path)
159159
self._paths = search_paths
@@ -188,7 +188,7 @@ def __init__(
188188
cmd = [sys.executable, "-m", "pytkdocs", "--line-by-line"]
189189

190190
self.process = Popen(
191-
cmd,
191+
cmd, # noqa: S603
192192
universal_newlines=True,
193193
stdout=PIPE,
194194
stdin=PIPE,
@@ -203,7 +203,7 @@ def load_inventory(
203203
in_file: BinaryIO,
204204
url: str,
205205
base_url: str | None = None,
206-
**kwargs,
206+
**kwargs: Any, # noqa: ARG003
207207
) -> Iterator[tuple[str, str]]:
208208
"""Yield items and their URLs from an inventory file streamed from `in_file`.
209209
@@ -224,7 +224,7 @@ def load_inventory(
224224
for item in Inventory.parse_sphinx(in_file, domain_filter=("py",)).values():
225225
yield item.name, posixpath.join(base_url, item.uri)
226226

227-
def collect(self, identifier: str, config: dict) -> CollectorItem:
227+
def collect(self, identifier: str, config: MutableMapping[str, Any]) -> CollectorItem:
228228
"""Collect the documentation tree given an identifier and selection options.
229229
230230
In this method, we feed one line of JSON to the standard input of the subprocess that was opened
@@ -265,11 +265,11 @@ def collect(self, identifier: str, config: dict) -> CollectorItem:
265265
json_input = json.dumps({"objects": [{"path": identifier, **final_config}]})
266266

267267
logger.debug("Writing to process' stdin")
268-
self.process.stdin.write(json_input + "\n") # type: ignore
269-
self.process.stdin.flush() # type: ignore
268+
self.process.stdin.write(json_input + "\n") # type: ignore[union-attr]
269+
self.process.stdin.flush() # type: ignore[union-attr]
270270

271271
logger.debug("Reading process' stdout")
272-
stdout = self.process.stdout.readline() # type: ignore
272+
stdout = self.process.stdout.readline() # type: ignore[union-attr]
273273

274274
logger.debug("Loading JSON output as Python object")
275275
try:
@@ -304,8 +304,8 @@ def teardown(self) -> None:
304304
logger.debug("Tearing process down")
305305
self.process.terminate()
306306

307-
def render(self, data: CollectorItem, config: dict) -> str: # noqa: D102 (ignore missing docstring)
308-
final_config = ChainMap(config, self.default_config)
307+
def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str: # noqa: D102 (ignore missing docstring)
308+
final_config = ChainMap(config, self.default_config) # type: ignore[arg-type]
309309

310310
template = self.env.get_template(f"{data['category']}.html")
311311

@@ -348,7 +348,7 @@ def get_handler(
348348
setup_commands: list[str] | None = None,
349349
config_file_path: str | None = None,
350350
paths: list[str] | None = None,
351-
**config: Any,
351+
**config: Any, # noqa: ARG001
352352
) -> PythonHandler:
353353
"""Simply return an instance of `PythonHandler`.
354354

tests/conftest.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
"""Configuration for the pytest test suite."""
22

3+
from __future__ import annotations
4+
35
from collections import ChainMap
6+
from typing import TYPE_CHECKING, Iterator
47

58
import pytest
69
from markdown.core import Markdown
710
from mkdocs.config.defaults import MkDocsConfig
811

12+
if TYPE_CHECKING:
13+
from pathlib import Path
14+
15+
from mkdocstrings.extension import MkdocstringsExtension
16+
from mkdocstrings.plugin import MkdocstringsPlugin
17+
918

1019
@pytest.fixture(name="mkdocs_conf")
11-
def fixture_mkdocs_conf(request, tmp_path):
20+
def fixture_mkdocs_conf(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[MkDocsConfig]:
1221
"""Yield a MkDocs configuration object.
1322
1423
Parameters:
@@ -45,7 +54,7 @@ def fixture_mkdocs_conf(request, tmp_path):
4554

4655

4756
@pytest.fixture(name="plugin")
48-
def fixture_plugin(mkdocs_conf):
57+
def fixture_plugin(mkdocs_conf: MkDocsConfig) -> MkdocstringsPlugin:
4958
"""Return a plugin instance.
5059
5160
Parameters:
@@ -60,7 +69,7 @@ def fixture_plugin(mkdocs_conf):
6069

6170

6271
@pytest.fixture(name="ext_markdown")
63-
def fixture_ext_markdown(plugin):
72+
def fixture_ext_markdown(plugin: MkdocstringsPlugin) -> MkdocstringsExtension:
6473
"""Return a Markdown instance with MkdocstringsExtension.
6574
6675
Parameters:
@@ -69,4 +78,4 @@ def fixture_ext_markdown(plugin):
6978
Returns:
7079
The plugin Markdown instance.
7180
"""
72-
return plugin.md
81+
return plugin.md # type: ignore[attr-defined]

tests/test_collector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
({"error": "", "traceback": "hello"}, "\nhello"),
1616
],
1717
)
18-
def test_collect_result_error(retval, exp_res):
18+
def test_collect_result_error(retval: dict, exp_res: str) -> None:
1919
"""Test handling of errors when collecting an object.
2020
2121
Args:
2222
retval: Return value to mock `json.loads` with.
2323
exp_res: Expected result.
2424
"""
25-
with mock.patch("mkdocstrings_handlers.python.handler.json.loads") as m_loads:
25+
with mock.patch("mkdocstrings_handlers.python.handler.json.loads") as m_loads: # noqa: SIM117
2626
with pytest.raises(CollectionError) as excinfo: # noqa: PT012
2727
m_loads.return_value = retval
2828
handler = get_handler("material")

tests/test_renderer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tests for the handlers.python module."""
22

3+
from __future__ import annotations
4+
35
from copy import deepcopy
46

57
from mkdocstrings_handlers.python.rendering import (
@@ -10,9 +12,9 @@
1012
)
1113

1214

13-
def test_members_order():
15+
def test_members_order() -> None:
1416
"""Assert that members sorting functions work correctly."""
15-
subcategories = {key: [] for key in ("attributes", "classes", "functions", "methods", "modules")}
17+
subcategories: dict[str, list] = {key: [] for key in ("attributes", "classes", "functions", "methods", "modules")}
1618
categories = {"children": {}, **subcategories}
1719
collected = {
1820
"name": "root",

tests/test_themes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
"""Tests for the different themes we claim to support."""
22

3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
36

47
import pytest
58

9+
if TYPE_CHECKING:
10+
from mkdocstrings.plugin import MkdocstringsPlugin
11+
612

713
@pytest.mark.parametrize(
814
"plugin",
@@ -25,14 +31,14 @@
2531
"mkdocstrings_handlers.python.rendering",
2632
],
2733
)
28-
def test_render_themes_templates(module, plugin):
34+
def test_render_themes_templates(module: str, plugin: MkdocstringsPlugin) -> None:
2935
"""Test rendering of a given theme's templates.
3036
3137
Parameters:
3238
module: The module to load and render (parametrized).
3339
plugin: The plugin instance (parametrized fixture).
3440
"""
3541
handler = plugin.handlers.get_handler("python")
36-
handler._update_env(plugin.md, plugin.handlers._config)
42+
handler._update_env(plugin.md, plugin.handlers._config) # type: ignore[attr-defined]
3743
data = handler.collect(module, {})
3844
handler.render(data, {})

0 commit comments

Comments
 (0)