77import logging
88from typing import Any
99from typing import Callable
10- from typing import Mapping
1110from typing import Sequence
1211
1312from flake8 import utils
2019_ARG = enum .Enum ("_ARG" , "NO" )
2120
2221
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-
7222def _flake8_normalize (
7323 value : str ,
7424 * args : str ,
@@ -95,21 +45,16 @@ def __init__(
9545 self ,
9646 short_option_name : str | _ARG = _ARG .NO ,
9747 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
9949 action : str | type [argparse .Action ] | _ARG = _ARG .NO ,
10050 default : Any | _ARG = _ARG .NO ,
101- type : str | Callable [..., Any ] | _ARG = _ARG .NO ,
51+ type : Callable [..., Any ] | _ARG = _ARG .NO ,
10252 dest : str | _ARG = _ARG .NO ,
10353 nargs : int | str | _ARG = _ARG .NO ,
10454 const : Any | _ARG = _ARG .NO ,
10555 choices : Sequence [Any ] | _ARG = _ARG .NO ,
10656 help : str | _ARG = _ARG .NO ,
10757 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
11358 required : bool | _ARG = _ARG .NO ,
11459 # Options below here are specific to Flake8
11560 parse_from_config : bool = False ,
@@ -150,21 +95,9 @@ def __init__(
15095
15196 :param type:
15297 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`).
15599 :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`.
168101
169102 The following parameters are for Flake8's option handling alone.
170103
@@ -184,37 +117,6 @@ def __init__(
184117 ):
185118 short_option_name , long_option_name = _ARG .NO , short_option_name
186119
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-
218120 # flake8 special type normalization
219121 if comma_separated_list or normalize_paths :
220122 type = functools .partial (
@@ -237,9 +139,6 @@ def __init__(
237139 self .nargs = nargs
238140 self .const = const
239141 self .choices = choices
240- self .callback = callback
241- self .callback_args = callback_args
242- self .callback_kwargs = callback_kwargs
243142 self .help = help
244143 self .metavar = metavar
245144 self .required = required
@@ -251,9 +150,6 @@ def __init__(
251150 "nargs" : self .nargs ,
252151 "const" : self .const ,
253152 "choices" : self .choices ,
254- "callback" : self .callback ,
255- "callback_args" : self .callback_args ,
256- "callback_kwargs" : self .callback_kwargs ,
257153 "help" : self .help ,
258154 "metavar" : self .metavar ,
259155 "required" : self .required ,
0 commit comments