Skip to content

Commit 16c371d

Browse files
authored
Merge pull request #1739 from PyCQA/remove-optparse
remove optparse support
2 parents 1a92561 + 88457a0 commit 16c371d

File tree

2 files changed

+4
-199
lines changed

2 files changed

+4
-199
lines changed

src/flake8/options/manager.py

Lines changed: 4 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import logging
88
from typing import Any
99
from typing import Callable
10-
from typing import Mapping
1110
from typing import Sequence
1211

1312
from flake8 import utils
@@ -20,55 +19,6 @@
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-
7222
def _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,

tests/unit/test_option_manager.py

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import argparse
55
import os
6-
from unittest import mock
76

87
import pytest
98

@@ -170,96 +169,6 @@ def test_extend_default_ignore(optmanager):
170169
assert optmanager.extended_default_ignore == ["T100", "T101", "T102"]
171170

172171

173-
def test_optparse_normalize_callback_option_legacy(optmanager):
174-
"""Test the optparse shim for `callback=`."""
175-
callback_foo = mock.Mock()
176-
optmanager.add_option(
177-
"--foo",
178-
action="callback",
179-
callback=callback_foo,
180-
callback_args=(1, 2),
181-
callback_kwargs={"a": "b"},
182-
)
183-
callback_bar = mock.Mock()
184-
optmanager.add_option(
185-
"--bar",
186-
action="callback",
187-
type="string",
188-
callback=callback_bar,
189-
)
190-
callback_baz = mock.Mock()
191-
optmanager.add_option(
192-
"--baz",
193-
action="callback",
194-
type="string",
195-
nargs=2,
196-
callback=callback_baz,
197-
)
198-
199-
optmanager.parse_args(["--foo", "--bar", "bararg", "--baz", "1", "2"])
200-
201-
callback_foo.assert_called_once_with(
202-
mock.ANY, # the option / action instance
203-
"--foo",
204-
None,
205-
mock.ANY, # the OptionParser / ArgumentParser
206-
1,
207-
2,
208-
a="b",
209-
)
210-
callback_bar.assert_called_once_with(
211-
mock.ANY, # the option / action instance
212-
"--bar",
213-
"bararg",
214-
mock.ANY, # the OptionParser / ArgumentParser
215-
)
216-
callback_baz.assert_called_once_with(
217-
mock.ANY, # the option / action instance
218-
"--baz",
219-
("1", "2"),
220-
mock.ANY, # the OptionParser / ArgumentParser
221-
)
222-
223-
224-
@pytest.mark.parametrize(
225-
("type_s", "input_val", "expected"),
226-
(
227-
("int", "5", 5),
228-
("long", "6", 6),
229-
("string", "foo", "foo"),
230-
("float", "1.5", 1.5),
231-
("complex", "1+5j", 1 + 5j),
232-
# optparse allows this but does not document it
233-
("str", "foo", "foo"),
234-
),
235-
)
236-
def test_optparse_normalize_types(optmanager, type_s, input_val, expected):
237-
"""Test the optparse shim for type="typename"."""
238-
optmanager.add_option("--foo", type=type_s)
239-
opts = optmanager.parse_args(["--foo", input_val])
240-
assert opts.foo == expected
241-
242-
243-
def test_optparse_normalize_choice_type(optmanager):
244-
"""Test the optparse shim for type="choice"."""
245-
optmanager.add_option("--foo", type="choice", choices=("1", "2", "3"))
246-
opts = optmanager.parse_args(["--foo", "1"])
247-
assert opts.foo == "1"
248-
# fails to parse
249-
with pytest.raises(SystemExit):
250-
optmanager.parse_args(["--foo", "4"])
251-
252-
253-
def test_optparse_normalize_help(optmanager, capsys):
254-
"""Test the optparse shim for %default in help text."""
255-
optmanager.add_option("--foo", default="bar", help="default: %default")
256-
with pytest.raises(SystemExit):
257-
optmanager.parse_args(["--help"])
258-
out, err = capsys.readouterr()
259-
output = out + err
260-
assert "default: bar" in output
261-
262-
263172
@pytest.mark.parametrize(
264173
("s", "is_auto", "n_jobs"),
265174
(

0 commit comments

Comments
 (0)