7
7
import logging
8
8
from typing import Any
9
9
from typing import Callable
10
- from typing import Mapping
11
10
from typing import Sequence
12
11
13
12
from flake8 import utils
20
19
_ARG = enum .Enum ("_ARG" , "NO" )
21
20
22
21
23
- _optparse_callable_map : dict [str , type [Any ] | _ARG ] = {
24
- "int" : int ,
25
- "long" : int ,
26
- "string" : str ,
27
- "float" : float ,
28
- "complex" : complex ,
29
- "choice" : _ARG .NO ,
30
- # optparse allows this but does not document it
31
- "str" : str ,
32
- }
33
-
34
-
35
- class _CallbackAction (argparse .Action ):
36
- """Shim for optparse-style callback actions."""
37
-
38
- def __init__ (
39
- self ,
40
- * args : Any ,
41
- callback : Callable [..., Any ],
42
- callback_args : Sequence [Any ] = (),
43
- callback_kwargs : dict [str , Any ] | None = None ,
44
- ** kwargs : Any ,
45
- ) -> None :
46
- self ._callback = callback
47
- self ._callback_args = callback_args
48
- self ._callback_kwargs = callback_kwargs or {}
49
- super ().__init__ (* args , ** kwargs )
50
-
51
- def __call__ (
52
- self ,
53
- parser : argparse .ArgumentParser ,
54
- namespace : argparse .Namespace ,
55
- values : Sequence [str ] | str | None ,
56
- option_string : str | None = None ,
57
- ) -> None :
58
- if not values :
59
- values = None
60
- elif isinstance (values , list ) and len (values ) > 1 :
61
- values = tuple (values )
62
- self ._callback (
63
- self ,
64
- option_string ,
65
- values ,
66
- parser ,
67
- * self ._callback_args ,
68
- ** self ._callback_kwargs ,
69
- )
70
-
71
-
72
22
def _flake8_normalize (
73
23
value : str ,
74
24
* args : str ,
@@ -95,21 +45,16 @@ def __init__(
95
45
self ,
96
46
short_option_name : str | _ARG = _ARG .NO ,
97
47
long_option_name : str | _ARG = _ARG .NO ,
98
- # Options below here are taken from the optparse.Option class
48
+ # Options below are taken from argparse.ArgumentParser.add_argument
99
49
action : str | type [argparse .Action ] | _ARG = _ARG .NO ,
100
50
default : Any | _ARG = _ARG .NO ,
101
- type : str | Callable [..., Any ] | _ARG = _ARG .NO ,
51
+ type : Callable [..., Any ] | _ARG = _ARG .NO ,
102
52
dest : str | _ARG = _ARG .NO ,
103
53
nargs : int | str | _ARG = _ARG .NO ,
104
54
const : Any | _ARG = _ARG .NO ,
105
55
choices : Sequence [Any ] | _ARG = _ARG .NO ,
106
56
help : str | _ARG = _ARG .NO ,
107
57
metavar : str | _ARG = _ARG .NO ,
108
- # deprecated optparse-only options
109
- callback : Callable [..., Any ] | _ARG = _ARG .NO ,
110
- callback_args : Sequence [Any ] | _ARG = _ARG .NO ,
111
- callback_kwargs : Mapping [str , Any ] | _ARG = _ARG .NO ,
112
- # Options below are taken from argparse.ArgumentParser.add_argument
113
58
required : bool | _ARG = _ARG .NO ,
114
59
# Options below here are specific to Flake8
115
60
parse_from_config : bool = False ,
@@ -150,21 +95,9 @@ def __init__(
150
95
151
96
:param type:
152
97
A callable to normalize the type (as is the case in
153
- :mod:`argparse`). Deprecated: you can also pass through type
154
- strings such as ``'int'`` which are handled by :mod:`optparse`.
98
+ :mod:`argparse`).
155
99
:param action:
156
- Any action allowed by :mod:`argparse`. Deprecated: this also
157
- understands the ``action='callback'`` action from :mod:`optparse`.
158
- :param callback:
159
- Callback used if the action is ``"callback"``. Deprecated: please
160
- use ``action=`` instead.
161
- :param callback_args:
162
- Additional positional arguments to the callback callable.
163
- Deprecated: please use ``action=`` instead (probably with
164
- ``functools.partial``).
165
- :param callback_kwargs:
166
- Keyword arguments to the callback callable. Deprecated: please
167
- use ``action=`` instead (probably with ``functools.partial``).
100
+ Any action allowed by :mod:`argparse`.
168
101
169
102
The following parameters are for Flake8's option handling alone.
170
103
@@ -184,37 +117,6 @@ def __init__(
184
117
):
185
118
short_option_name , long_option_name = _ARG .NO , short_option_name
186
119
187
- # optparse -> argparse `%default` => `%(default)s`
188
- if help is not _ARG .NO and "%default" in help :
189
- LOG .warning (
190
- "option %s: please update `help=` text to use %%(default)s "
191
- "instead of %%default -- this will be an error in the future" ,
192
- long_option_name ,
193
- )
194
- help = help .replace ("%default" , "%(default)s" )
195
-
196
- # optparse -> argparse for `callback`
197
- if action == "callback" :
198
- LOG .warning (
199
- "option %s: please update from optparse `action='callback'` "
200
- "to argparse action classes -- this will be an error in the "
201
- "future" ,
202
- long_option_name ,
203
- )
204
- action = _CallbackAction
205
- if type is _ARG .NO :
206
- nargs = 0
207
-
208
- # optparse -> argparse for `type`
209
- if isinstance (type , str ):
210
- LOG .warning (
211
- "option %s: please update from optparse string `type=` to "
212
- "argparse callable `type=` -- this will be an error in the "
213
- "future" ,
214
- long_option_name ,
215
- )
216
- type = _optparse_callable_map [type ]
217
-
218
120
# flake8 special type normalization
219
121
if comma_separated_list or normalize_paths :
220
122
type = functools .partial (
@@ -237,9 +139,6 @@ def __init__(
237
139
self .nargs = nargs
238
140
self .const = const
239
141
self .choices = choices
240
- self .callback = callback
241
- self .callback_args = callback_args
242
- self .callback_kwargs = callback_kwargs
243
142
self .help = help
244
143
self .metavar = metavar
245
144
self .required = required
@@ -251,9 +150,6 @@ def __init__(
251
150
"nargs" : self .nargs ,
252
151
"const" : self .const ,
253
152
"choices" : self .choices ,
254
- "callback" : self .callback ,
255
- "callback_args" : self .callback_args ,
256
- "callback_kwargs" : self .callback_kwargs ,
257
153
"help" : self .help ,
258
154
"metavar" : self .metavar ,
259
155
"required" : self .required ,
0 commit comments