Skip to content

Commit 17c1aa7

Browse files
committed
Add type hints to untyped definitions
1 parent 82d78c1 commit 17c1aa7

File tree

13 files changed

+42
-35
lines changed

13 files changed

+42
-35
lines changed

.ruff.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ select = [
3838
"ANN003", # Missing type annotation for `**{name}`
3939
# "ANN101", # Missing type annotation for `{name}` in method
4040
# "ANN102", # Missing type annotation for `{name}` in classmethod
41-
# "ANN201", # Missing return type annotation for public function `{name}`
41+
"ANN201", # Missing return type annotation for public function `{name}`
4242
# "ANN202", # Missing return type annotation for private function `{name}`
4343
# "ANN204", # Missing return type annotation for special method `{name}`
4444
"ANN205", # Missing return type annotation for staticmethod `{name}`
@@ -471,6 +471,9 @@ select = [
471471
]
472472

473473
[lint.per-file-ignores]
474+
"doc/*" = [
475+
"ANN", # documentation doesn't need annotations
476+
]
474477
"doc/conf.py" = ["INP001"]
475478
"doc/development/tutorials/examples/*" = ["INP001"]
476479
# allow print() in the tutorial
@@ -506,8 +509,10 @@ select = [
506509
"tests/test_util_typing.py" = ["UP006", "UP035"]
507510
"tests/typing_test_data.py" = ["FA100", "UP006", "UP035"]
508511

509-
# whitelist ``print`` for stdout messages
510-
"utils/*" = ["T201"]
512+
"utils/*" = [
513+
"T201", # whitelist ``print`` for stdout messages
514+
"ANN", # utilities don't need annotations
515+
]
511516

512517
[lint.flake8-quotes]
513518
inline-quotes = "single"

sphinx/addnodes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class desc_signature(_desc_classes_injector, nodes.Part, nodes.Inline, nodes.Tex
167167
classes = ['sig', 'sig-object']
168168

169169
@property
170-
def child_text_separator(self):
170+
def child_text_separator(self) -> str: # type: ignore[override]
171171
if self.get('is_multiline'):
172172
return ' '
173173
else:
@@ -258,7 +258,7 @@ class desc_parameterlist(nodes.Part, nodes.Inline, nodes.FixedTextElement):
258258
"""
259259
child_text_separator = ', '
260260

261-
def astext(self):
261+
def astext(self) -> str:
262262
return f'({super().astext()})'
263263

264264

@@ -271,7 +271,7 @@ class desc_type_parameter_list(nodes.Part, nodes.Inline, nodes.FixedTextElement)
271271
"""
272272
child_text_separator = ', '
273273

274-
def astext(self):
274+
def astext(self) -> str:
275275
return f'[{super().astext()}]'
276276

277277

@@ -322,7 +322,7 @@ def __init__(self, rawsource: str = '', text: str = '',
322322
super().__init__(rawsource, text, *children, **attributes)
323323
self['classes'].extend(self.classes)
324324

325-
def __init_subclass__(cls, *, _sig_element=False, **kwargs: Any):
325+
def __init_subclass__(cls, *, _sig_element: bool = False, **kwargs: Any):
326326
super().__init_subclass__(**kwargs)
327327
if _sig_element:
328328
# add the class to the SIG_ELEMENTS set if asked

sphinx/application.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from collections.abc import Collection, Sequence # NoQA: TCH003
1414
from io import StringIO
1515
from os import path
16-
from typing import IO, TYPE_CHECKING, Any, Callable
16+
from typing import IO, TYPE_CHECKING, Any, Callable, Literal
1717

1818
from docutils.nodes import TextElement # NoQA: TCH002
1919
from docutils.parsers.rst import Directive, roles
@@ -1307,7 +1307,7 @@ def is_parallel_allowed(self, typ: str) -> bool:
13071307

13081308
return True
13091309

1310-
def set_html_assets_policy(self, policy):
1310+
def set_html_assets_policy(self, policy: Literal['always', 'per_page']) -> None:
13111311
"""Set the policy to include assets in HTML pages.
13121312
13131313
- always: include the assets in all the pages

sphinx/domains/c.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from sphinx.util.nodes import make_refnode
4343

4444
if TYPE_CHECKING:
45-
from collections.abc import Generator, Iterator
45+
from collections.abc import Generator, Iterator, Sequence
4646

4747
from docutils.nodes import Element, Node, TextElement, system_message
4848

@@ -2197,11 +2197,11 @@ def language(self) -> str:
21972197
return 'C'
21982198

21992199
@property
2200-
def id_attributes(self):
2200+
def id_attributes(self) -> Sequence[str]:
22012201
return self.config.c_id_attributes
22022202

22032203
@property
2204-
def paren_attributes(self):
2204+
def paren_attributes(self) -> Sequence[str]:
22052205
return self.config.c_paren_attributes
22062206

22072207
def _parse_string(self) -> str | None:

sphinx/domains/cpp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from sphinx.util.nodes import make_refnode
4444

4545
if TYPE_CHECKING:
46-
from collections.abc import Generator, Iterator
46+
from collections.abc import Generator, Iterator, Sequence
4747

4848
from docutils.nodes import Element, Node, TextElement, system_message
4949

@@ -2880,7 +2880,7 @@ def name(self, name: ASTNestedName) -> None:
28802880
self.next.name = name
28812881

28822882
@property
2883-
def isPack(self):
2883+
def isPack(self) -> bool:
28842884
return self.next.isPack
28852885

28862886
@property
@@ -2978,7 +2978,7 @@ def name(self, name: ASTNestedName) -> None:
29782978
self.inner.name = name
29792979

29802980
@property
2981-
def isPack(self):
2981+
def isPack(self) -> bool:
29822982
return self.inner.isPack or self.next.isPack
29832983

29842984
@property
@@ -5240,11 +5240,11 @@ def language(self) -> str:
52405240
return 'C++'
52415241

52425242
@property
5243-
def id_attributes(self):
5243+
def id_attributes(self) -> Sequence[str]:
52445244
return self.config.cpp_id_attributes
52455245

52465246
@property
5247-
def paren_attributes(self):
5247+
def paren_attributes(self) -> Sequence[str]:
52485248
return self.config.cpp_paren_attributes
52495249

52505250
def _parse_string(self) -> str:

sphinx/locale/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def init_console(
155155
locale_dir = _LOCALE_DIR
156156
try:
157157
# encoding is ignored
158-
language, _ = locale.getlocale(locale.LC_MESSAGES)
158+
language, _ = locale.getlocale(locale.LC_MESSAGES) # type: ignore[attr-defined]
159159
except AttributeError:
160160
# LC_MESSAGES is not always defined. Fallback to the default language
161161
# in case it is not.

sphinx/testing/fixtures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
]
2727

2828

29-
def pytest_configure(config):
29+
def pytest_configure(config: pytest.Config) -> None:
3030
"""Register custom markers"""
3131
for marker in DEFAULT_ENABLED_MARKERS:
3232
config.addinivalue_line('markers', marker)
@@ -175,7 +175,7 @@ def make_app(test_params: dict, monkeypatch: Any) -> Generator[Callable, None, N
175175
apps = []
176176
syspath = sys.path.copy()
177177

178-
def make(*args: Any, **kwargs: Any):
178+
def make(*args: Any, **kwargs: Any) -> SphinxTestApp:
179179
status, warning = StringIO(), StringIO()
180180
kwargs.setdefault('status', status)
181181
kwargs.setdefault('warning', warning)
@@ -226,7 +226,7 @@ def sphinx_test_tempdir(tmp_path_factory: Any) -> Path:
226226

227227

228228
@pytest.fixture()
229-
def rollback_sysmodules(): # NoQA: PT004
229+
def rollback_sysmodules() -> Generator[None, None, None]: # NoQA: PT004
230230
"""
231231
Rollback sys.modules to its value before testing to unload modules
232232
during tests.

sphinx/util/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ def __setstate__(self, state: set[str]) -> None:
104104
self._existing = state
105105

106106

107-
def _md5(data=b'', **_kw: Any):
107+
def _md5(data: bytes = b'', **_kw: Any) -> hashlib._Hash:
108108
"""Deprecated wrapper around hashlib.md5
109109
110110
To be removed in Sphinx 9.0
111111
"""
112112
return hashlib.md5(data, usedforsecurity=False)
113113

114114

115-
def _sha1(data=b'', **_kw: Any):
115+
def _sha1(data: bytes = b'', **_kw: Any) -> hashlib._Hash:
116116
"""Deprecated wrapper around hashlib.sha1
117117
118118
To be removed in Sphinx 9.0

sphinx/util/cfamily.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from sphinx.util import logging
1313

1414
if TYPE_CHECKING:
15+
from collections.abc import Sequence
16+
1517
from docutils.nodes import TextElement
1618

1719
from sphinx.config import Config
@@ -369,11 +371,11 @@ def assert_end(self, *, allowSemicolon: bool = False) -> None:
369371
################################################################################
370372

371373
@property
372-
def id_attributes(self):
374+
def id_attributes(self) -> Sequence[str]:
373375
raise NotImplementedError
374376

375377
@property
376-
def paren_attributes(self):
378+
def paren_attributes(self) -> Sequence[str]:
377379
raise NotImplementedError
378380

379381
def _parse_balanced_token_seq(self, end: list[str]) -> str:

sphinx/writers/html5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ def depart_math_block(self, node: Element, math_env: str = '') -> None:
928928

929929
# See Docutils r9413
930930
# Re-instate the footnote-reference class
931-
def visit_footnote_reference(self, node):
931+
def visit_footnote_reference(self, node: Element) -> None:
932932
href = '#' + node['refid']
933933
classes = ['footnote-reference', self.settings.footnote_references]
934934
self.body.append(self.starttag(node, 'a', suffix='', classes=classes,

0 commit comments

Comments
 (0)