4
4
import re
5
5
import sys
6
6
from textwrap import dedent
7
+ from typing import Iterable , List , Optional
7
8
8
9
from docutils import nodes
9
10
from docutils .parsers import rst
10
11
from docutils .statemachine import StringList , ViewList
12
+ from sphinx .application import Sphinx
11
13
12
14
from pip ._internal .cli import cmdoptions
13
15
from pip ._internal .commands import commands_dict , create_command
@@ -18,7 +20,7 @@ class PipCommandUsage(rst.Directive):
18
20
required_arguments = 1
19
21
optional_arguments = 3
20
22
21
- def run (self ):
23
+ def run (self ) -> List [ nodes . Node ] :
22
24
cmd = create_command (self .arguments [0 ])
23
25
cmd_prefix = "python -m pip"
24
26
if len (self .arguments ) > 1 :
@@ -33,11 +35,12 @@ def run(self):
33
35
class PipCommandDescription (rst .Directive ):
34
36
required_arguments = 1
35
37
36
- def run (self ):
38
+ def run (self ) -> List [ nodes . Node ] :
37
39
node = nodes .paragraph ()
38
40
node .document = self .state .document
39
41
desc = ViewList ()
40
42
cmd = create_command (self .arguments [0 ])
43
+ assert cmd .__doc__ is not None
41
44
description = dedent (cmd .__doc__ )
42
45
for line in description .split ("\n " ):
43
46
desc .append (line , "" )
@@ -46,7 +49,9 @@ def run(self):
46
49
47
50
48
51
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 ]:
50
55
bookmark_line = (
51
56
f".. _`{ cmd_name } _{ option ._long_opts [0 ]} `:"
52
57
if cmd_name
@@ -60,22 +65,27 @@ def _format_option(self, option, cmd_name=None):
60
65
elif option ._long_opts :
61
66
line += option ._long_opts [0 ]
62
67
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
64
70
line += f" <{ metavar .lower ()} >"
65
71
# 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
67
75
# fix paths with sys.prefix
68
76
opt_help = opt_help .replace (sys .prefix , "<sys.prefix>" )
69
77
return [bookmark_line , "" , line , "" , " " + opt_help , "" ]
70
78
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 :
72
82
for option in options :
73
83
if option .help == optparse .SUPPRESS_HELP :
74
84
continue
75
85
for line in self ._format_option (option , cmd_name ):
76
86
self .view_list .append (line , "" )
77
87
78
- def run (self ):
88
+ def run (self ) -> List [ nodes . Node ] :
79
89
node = nodes .paragraph ()
80
90
node .document = self .state .document
81
91
self .view_list = ViewList ()
@@ -85,14 +95,14 @@ def run(self):
85
95
86
96
87
97
class PipGeneralOptions (PipOptions ):
88
- def process_options (self ):
98
+ def process_options (self ) -> None :
89
99
self ._format_options ([o () for o in cmdoptions .general_group ["options" ]])
90
100
91
101
92
102
class PipIndexOptions (PipOptions ):
93
103
required_arguments = 1
94
104
95
- def process_options (self ):
105
+ def process_options (self ) -> None :
96
106
cmd_name = self .arguments [0 ]
97
107
self ._format_options (
98
108
[o () for o in cmdoptions .index_group ["options" ]],
@@ -103,7 +113,7 @@ def process_options(self):
103
113
class PipCommandOptions (PipOptions ):
104
114
required_arguments = 1
105
115
106
- def process_options (self ):
116
+ def process_options (self ) -> None :
107
117
cmd = create_command (self .arguments [0 ])
108
118
self ._format_options (
109
119
cmd .parser .option_groups [0 ].option_list ,
@@ -112,15 +122,15 @@ def process_options(self):
112
122
113
123
114
124
class PipReqFileOptionsReference (PipOptions ):
115
- def determine_opt_prefix (self , opt_name ) :
125
+ def determine_opt_prefix (self , opt_name : str ) -> str :
116
126
for command in commands_dict :
117
127
cmd = create_command (command )
118
128
if cmd .cmd_opts .has_option (opt_name ):
119
129
return command
120
130
121
131
raise KeyError (f"Could not identify prefix of opt { opt_name } " )
122
132
123
- def process_options (self ):
133
+ def process_options (self ) -> None :
124
134
for option in SUPPORTED_OPTIONS :
125
135
if getattr (option , "deprecated" , False ):
126
136
continue
@@ -157,7 +167,7 @@ class PipCLIDirective(rst.Directive):
157
167
has_content = True
158
168
optional_arguments = 1
159
169
160
- def run (self ):
170
+ def run (self ) -> List [ nodes . Node ] :
161
171
node = nodes .paragraph ()
162
172
node .document = self .state .document
163
173
@@ -226,7 +236,7 @@ def run(self):
226
236
return [node ]
227
237
228
238
229
- def setup (app ) :
239
+ def setup (app : Sphinx ) -> None :
230
240
app .add_directive ("pip-command-usage" , PipCommandUsage )
231
241
app .add_directive ("pip-command-description" , PipCommandDescription )
232
242
app .add_directive ("pip-command-options" , PipCommandOptions )
0 commit comments