Skip to content

Commit bcbfbaa

Browse files
authored
Merge pull request #9672 from jdufresne/mypy-docs
Complete typing of docs directory
2 parents af60df5 + 96615e9 commit bcbfbaa

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ repos:
6666
rev: v0.800
6767
hooks:
6868
- id: mypy
69-
exclude: docs|tests
69+
exclude: tests
7070
args: ["--pretty"]
7171
additional_dependencies: ['nox==2020.12.31']
7272

docs/html/conf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pathlib
66
import re
77
import sys
8+
from typing import List, Tuple
89

910
# Add the docs/ directory to sys.path, because pip_sphinxext.py is there.
1011
docs_dir = os.path.dirname(os.path.dirname(__file__))
@@ -93,10 +94,10 @@
9394

9495

9596
# List of manual pages generated
96-
def determine_man_pages():
97+
def determine_man_pages() -> List[Tuple[str, str, str, str, int]]:
9798
"""Determine which man pages need to be generated."""
9899

99-
def to_document_name(path, base_dir):
100+
def to_document_name(path: str, base_dir: str) -> str:
100101
"""Convert a provided path to a Sphinx "document name"."""
101102
relative_path = os.path.relpath(path, base_dir)
102103
root, _ = os.path.splitext(relative_path)

docs/pip_sphinxext.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import re
55
import sys
66
from textwrap import dedent
7+
from typing import Iterable, List, Optional
78

89
from docutils import nodes
910
from docutils.parsers import rst
1011
from docutils.statemachine import StringList, ViewList
12+
from sphinx.application import Sphinx
1113

1214
from pip._internal.cli import cmdoptions
1315
from pip._internal.commands import commands_dict, create_command
@@ -18,7 +20,7 @@ class PipCommandUsage(rst.Directive):
1820
required_arguments = 1
1921
optional_arguments = 3
2022

21-
def run(self):
23+
def run(self) -> List[nodes.Node]:
2224
cmd = create_command(self.arguments[0])
2325
cmd_prefix = "python -m pip"
2426
if len(self.arguments) > 1:
@@ -33,11 +35,12 @@ def run(self):
3335
class PipCommandDescription(rst.Directive):
3436
required_arguments = 1
3537

36-
def run(self):
38+
def run(self) -> List[nodes.Node]:
3739
node = nodes.paragraph()
3840
node.document = self.state.document
3941
desc = ViewList()
4042
cmd = create_command(self.arguments[0])
43+
assert cmd.__doc__ is not None
4144
description = dedent(cmd.__doc__)
4245
for line in description.split("\n"):
4346
desc.append(line, "")
@@ -46,7 +49,9 @@ def run(self):
4649

4750

4851
class PipOptions(rst.Directive):
49-
def _format_option(self, option, cmd_name=None):
52+
def _format_option(
53+
self, option: optparse.Option, cmd_name: Optional[str] = None
54+
) -> List[str]:
5055
bookmark_line = (
5156
f".. _`{cmd_name}_{option._long_opts[0]}`:"
5257
if cmd_name
@@ -60,22 +65,27 @@ def _format_option(self, option, cmd_name=None):
6065
elif option._long_opts:
6166
line += option._long_opts[0]
6267
if option.takes_value():
63-
metavar = option.metavar or option.dest.lower()
68+
metavar = option.metavar or option.dest
69+
assert metavar is not None
6470
line += f" <{metavar.lower()}>"
6571
# fix defaults
66-
opt_help = option.help.replace("%default", str(option.default))
72+
assert option.help is not None
73+
# https://github.com/python/typeshed/pull/5080
74+
opt_help = option.help.replace("%default", str(option.default)) # type: ignore
6775
# fix paths with sys.prefix
6876
opt_help = opt_help.replace(sys.prefix, "<sys.prefix>")
6977
return [bookmark_line, "", line, "", " " + opt_help, ""]
7078

71-
def _format_options(self, options, cmd_name=None):
79+
def _format_options(
80+
self, options: Iterable[optparse.Option], cmd_name: Optional[str] = None
81+
) -> None:
7282
for option in options:
7383
if option.help == optparse.SUPPRESS_HELP:
7484
continue
7585
for line in self._format_option(option, cmd_name):
7686
self.view_list.append(line, "")
7787

78-
def run(self):
88+
def run(self) -> List[nodes.Node]:
7989
node = nodes.paragraph()
8090
node.document = self.state.document
8191
self.view_list = ViewList()
@@ -85,14 +95,14 @@ def run(self):
8595

8696

8797
class PipGeneralOptions(PipOptions):
88-
def process_options(self):
98+
def process_options(self) -> None:
8999
self._format_options([o() for o in cmdoptions.general_group["options"]])
90100

91101

92102
class PipIndexOptions(PipOptions):
93103
required_arguments = 1
94104

95-
def process_options(self):
105+
def process_options(self) -> None:
96106
cmd_name = self.arguments[0]
97107
self._format_options(
98108
[o() for o in cmdoptions.index_group["options"]],
@@ -103,7 +113,7 @@ def process_options(self):
103113
class PipCommandOptions(PipOptions):
104114
required_arguments = 1
105115

106-
def process_options(self):
116+
def process_options(self) -> None:
107117
cmd = create_command(self.arguments[0])
108118
self._format_options(
109119
cmd.parser.option_groups[0].option_list,
@@ -112,15 +122,15 @@ def process_options(self):
112122

113123

114124
class PipReqFileOptionsReference(PipOptions):
115-
def determine_opt_prefix(self, opt_name):
125+
def determine_opt_prefix(self, opt_name: str) -> str:
116126
for command in commands_dict:
117127
cmd = create_command(command)
118128
if cmd.cmd_opts.has_option(opt_name):
119129
return command
120130

121131
raise KeyError(f"Could not identify prefix of opt {opt_name}")
122132

123-
def process_options(self):
133+
def process_options(self) -> None:
124134
for option in SUPPORTED_OPTIONS:
125135
if getattr(option, "deprecated", False):
126136
continue
@@ -157,7 +167,7 @@ class PipCLIDirective(rst.Directive):
157167
has_content = True
158168
optional_arguments = 1
159169

160-
def run(self):
170+
def run(self) -> List[nodes.Node]:
161171
node = nodes.paragraph()
162172
node.document = self.state.document
163173

@@ -226,7 +236,7 @@ def run(self):
226236
return [node]
227237

228238

229-
def setup(app):
239+
def setup(app: Sphinx) -> None:
230240
app.add_directive("pip-command-usage", PipCommandUsage)
231241
app.add_directive("pip-command-description", PipCommandDescription)
232242
app.add_directive("pip-command-options", PipCommandOptions)

0 commit comments

Comments
 (0)