Skip to content

Commit ad6ce59

Browse files
committed
Strict typing, py.typed and link issues
1 parent a8b6f12 commit ad6ce59

File tree

6 files changed

+57
-31
lines changed

6 files changed

+57
-31
lines changed

docs/conf.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@
3434
nitpicky = True
3535
nitpick_ignore: list[tuple[str, str]] = []
3636

37-
nitpick_ignore = [
38-
('py:class', '_pytest.nodes.Item'),
39-
('py:class', 'Config'),
40-
('py:class', 'Path'),
41-
('py:class', 'Session'),
42-
]
43-
4437
# Include Python intersphinx mapping to prevent failures
4538
# jaraco/skeleton#51
4639
extensions += ['sphinx.ext.intersphinx']
@@ -65,3 +58,13 @@
6558
intersphinx_mapping.update(
6659
pytest=('https://docs.pytest.org/en/latest/', None),
6760
)
61+
62+
# local
63+
64+
nitpick_ignore += [
65+
('py:class', 'Config'),
66+
('py:class', 'Session'),
67+
# jaraco/pytest-checkdocs#25
68+
('py:class', 'PackageMetadata'),
69+
('py:class', 'Node'),
70+
]

mypy.ini

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[mypy]
22
# Is the project well-typed?
3-
strict = False
3+
strict = True
44

55
# Early opt-in even when strict = False
66
warn_unused_ignores = True
@@ -13,3 +13,7 @@ explicit_package_bases = True
1313
disable_error_code =
1414
# Disable due to many false positives
1515
overload-overlap,
16+
17+
# jaraco/jaraco.packaging#20
18+
[mypy-jaraco.packaging.*]
19+
ignore_missing_imports = True

newsfragments/25.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Complete annotations and add ``py.typed`` marker -- by :user:`Avasam`

pyproject.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ type = [
6565

6666
# local
6767
"types-docutils", # pytest-checkdocs#13
68+
"importlib_metadata; python_version < '3.12'", # Only used for annotations, not at runtime
69+
"pytest>=8.3.3" # Typing fixes not necessary at runtime
6870
]
6971

7072

@@ -73,7 +75,3 @@ pytest11 = {checkdocs = "pytest_checkdocs"}
7375

7476

7577
[tool.setuptools_scm]
76-
77-
78-
[tool.pytest-enabler.mypy]
79-
# Disabled due to jaraco/skeleton#143

pytest_checkdocs/__init__.py

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,72 @@
1+
from __future__ import annotations
2+
13
import contextlib
24
import pathlib
35
import re
6+
import sys
7+
from collections.abc import Iterator
8+
from typing import TYPE_CHECKING, Any
49

5-
import pytest
610
import docutils.core
11+
import docutils.nodes
12+
import docutils.utils
13+
import pytest
714
from jaraco.packaging import metadata
815

16+
if TYPE_CHECKING:
17+
if sys.version_info >= (3, 12):
18+
from importlib.metadata import PackageMetadata
19+
else:
20+
from importlib_metadata import PackageMetadata
21+
22+
from _pytest.nodes import Node
23+
from typing_extensions import Self
924

1025
project_files = 'setup.py', 'setup.cfg', 'pyproject.toml'
1126

1227

13-
def pytest_collect_file(file_path: pathlib.Path, parent):
28+
def pytest_collect_file(file_path: pathlib.Path, parent: Node) -> CheckdocsItem | None:
1429
if file_path.name not in project_files:
15-
return
30+
return None
1631
return CheckdocsItem.from_parent(parent, name='project')
1732

1833

1934
class Description(str):
35+
content_type: str = ""
36+
2037
@classmethod
21-
def from_md(cls, md):
38+
def from_md(cls, md: PackageMetadata) -> Self:
2239
desc = cls(md.get('Description'))
2340
desc.content_type = md.get('Description-Content-Type', 'text/x-rst')
2441
return desc
2542

2643

2744
class CheckdocsItem(pytest.Item):
28-
def runtest(self):
45+
def runtest(self) -> None:
2946
desc = self.get_long_description()
3047
method_name = f"run_{re.sub('[-/]', '_', desc.content_type)}"
3148
getattr(self, method_name)(desc)
3249

33-
def run_text_markdown(self, desc):
50+
def run_text_markdown(self, desc: str) -> None:
3451
"stubbed"
3552

36-
def run_text_x_rst(self, desc):
53+
def run_text_x_rst(self, desc: str) -> None:
3754
with self.monkey_patch_system_message() as reports:
3855
self.rst2html(desc)
3956
assert not reports
4057

4158
@contextlib.contextmanager
42-
def monkey_patch_system_message(self):
43-
reports = []
59+
def monkey_patch_system_message(self) -> Iterator[list[str | Exception]]:
60+
reports: list[str | Exception] = []
4461
orig = docutils.utils.Reporter.system_message
4562

46-
def system_message(reporter, level, message, *children, **kwargs):
63+
def system_message(
64+
reporter: docutils.utils.Reporter,
65+
level: int,
66+
message: str | Exception,
67+
*children: docutils.nodes.Node,
68+
**kwargs: Any,
69+
) -> docutils.nodes.system_message:
4770
result = orig(reporter, level, message, *children, **kwargs)
4871
if level >= reporter.WARNING_LEVEL:
4972
# All reST failures preventing doc publishing go to reports
@@ -52,17 +75,14 @@ def system_message(reporter, level, message, *children, **kwargs):
5275

5376
return result
5477

55-
docutils.utils.Reporter.system_message = system_message
78+
docutils.utils.Reporter.system_message = system_message # type: ignore[assignment] # type-stubs expands the kwargs
5679
yield reports
57-
docutils.utils.Reporter.system_message = orig
80+
docutils.utils.Reporter.system_message = orig # type: ignore[method-assign]
5881

59-
def get_long_description(self):
82+
def get_long_description(self) -> Description:
6083
return Description.from_md(metadata.load('.'))
6184

6285
@staticmethod
63-
def rst2html(value):
64-
docutils_settings = {}
65-
parts = docutils.core.publish_parts(
66-
source=value, writer_name="html4css1", settings_overrides=docutils_settings
67-
)
68-
return parts['whole']
86+
def rst2html(value: str) -> str:
87+
parts = docutils.core.publish_parts(source=value, writer_name="html4css1")
88+
return parts['whole'] # type: ignore[no-any-return] # python/typeshed#12595

pytest_checkdocs/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)