Skip to content

Commit d358a06

Browse files
committedFeb 4, 2021
config/argparsing: use proper deprecations instead of ad-hoc DeprecationWarning
Proper for removing this in the next major pytest release.
1 parent 3907856 commit d358a06

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed
 

‎changelog/8315.deprecation.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Several behaviors of :meth:`Parser.addoption <_pytest.config.argparsing.Parser.addoption>` are now
2+
scheduled for removal in pytest 7 (deprecated since pytest 2.4.0):
3+
4+
- ``parser.addoption(..., help=".. %default ..")`` - use ``%(default)s`` instead.
5+
- ``parser.addoption(..., type="int/string/float/complex")`` - use ``type=int`` etc. instead.

‎doc/en/deprecations.rst

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ Deprecated Features
1818
Below is a complete list of all pytest features which are considered deprecated. Using those features will issue
1919
:class:`PytestWarning` or subclasses, which can be filtered using :ref:`standard warning filters <warnings>`.
2020

21+
22+
Backward compatibilities in ``Parser.addoption``
23+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24+
25+
.. deprecated:: 2.4
26+
27+
Several behaviors of :meth:`Parser.addoption <_pytest.config.argparsing.Parser.addoption>` are now
28+
scheduled for removal in pytest 7 (deprecated since pytest 2.4.0):
29+
30+
- ``parser.addoption(..., help=".. %default ..")`` - use ``%(default)s`` instead.
31+
- ``parser.addoption(..., type="int/string/float/complex")`` - use ``type=int`` etc. instead.
32+
33+
2134
Raising ``unittest.SkipTest`` during collection
2235
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2336

‎src/_pytest/config/argparsing.py

+6-16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import _pytest._io
1919
from _pytest.compat import final
2020
from _pytest.config.exceptions import UsageError
21+
from _pytest.deprecated import ARGUMENT_PERCENT_DEFAULT
22+
from _pytest.deprecated import ARGUMENT_TYPE_STR
23+
from _pytest.deprecated import ARGUMENT_TYPE_STR_CHOICE
2124

2225
if TYPE_CHECKING:
2326
from typing import NoReturn
@@ -212,12 +215,7 @@ def __init__(self, *names: str, **attrs: Any) -> None:
212215
self._short_opts: List[str] = []
213216
self._long_opts: List[str] = []
214217
if "%default" in (attrs.get("help") or ""):
215-
warnings.warn(
216-
'pytest now uses argparse. "%default" should be'
217-
' changed to "%(default)s" ',
218-
DeprecationWarning,
219-
stacklevel=3,
220-
)
218+
warnings.warn(ARGUMENT_PERCENT_DEFAULT, stacklevel=3)
221219
try:
222220
typ = attrs["type"]
223221
except KeyError:
@@ -227,23 +225,15 @@ def __init__(self, *names: str, **attrs: Any) -> None:
227225
if isinstance(typ, str):
228226
if typ == "choice":
229227
warnings.warn(
230-
"`type` argument to addoption() is the string %r."
231-
" For choices this is optional and can be omitted, "
232-
" but when supplied should be a type (for example `str` or `int`)."
233-
" (options: %s)" % (typ, names),
234-
DeprecationWarning,
228+
ARGUMENT_TYPE_STR_CHOICE.format(typ=typ, names=names),
235229
stacklevel=4,
236230
)
237231
# argparse expects a type here take it from
238232
# the type of the first element
239233
attrs["type"] = type(attrs["choices"][0])
240234
else:
241235
warnings.warn(
242-
"`type` argument to addoption() is the string %r, "
243-
" but when supplied should be a type (for example `str` or `int`)."
244-
" (options: %s)" % (typ, names),
245-
DeprecationWarning,
246-
stacklevel=4,
236+
ARGUMENT_TYPE_STR.format(typ=typ, names=names), stacklevel=4
247237
)
248238
attrs["type"] = Argument._typ_map[typ]
249239
# Used in test_parseopt -> test_parse_defaultgetter.

‎src/_pytest/deprecated.py

+19
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@
6969
"Use pytest.skip() instead."
7070
)
7171

72+
ARGUMENT_PERCENT_DEFAULT = PytestDeprecationWarning(
73+
'pytest now uses argparse. "%default" should be changed to "%(default)s"',
74+
)
75+
76+
ARGUMENT_TYPE_STR_CHOICE = UnformattedWarning(
77+
PytestDeprecationWarning,
78+
"`type` argument to addoption() is the string {typ!r}."
79+
" For choices this is optional and can be omitted, "
80+
" but when supplied should be a type (for example `str` or `int`)."
81+
" (options: {names})",
82+
)
83+
84+
ARGUMENT_TYPE_STR = UnformattedWarning(
85+
PytestDeprecationWarning,
86+
"`type` argument to addoption() is the string {typ!r}, "
87+
" but when supplied should be a type (for example `str` or `int`)."
88+
" (options: {names})",
89+
)
90+
7291

7392
# You want to make some `__init__` or function "private".
7493
#

0 commit comments

Comments
 (0)
Please sign in to comment.