Skip to content

Commit

Permalink
config/argparsing: use proper deprecations instead of ad-hoc Deprecat…
Browse files Browse the repository at this point in the history
…ionWarning

Proper for removing this in the next major pytest release.
  • Loading branch information
bluetech committed Feb 4, 2021
1 parent 3907856 commit e490823
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/_pytest/config/argparsing.py
Expand Up @@ -18,6 +18,9 @@
import _pytest._io
from _pytest.compat import final
from _pytest.config.exceptions import UsageError
from _pytest.deprecated import ARGUMENT_PERCENT_DEFAULT
from _pytest.deprecated import ARGUMENT_TYPE_STR
from _pytest.deprecated import ARGUMENT_TYPE_STR_CHOICE

if TYPE_CHECKING:
from typing import NoReturn
Expand Down Expand Up @@ -212,12 +215,7 @@ def __init__(self, *names: str, **attrs: Any) -> None:
self._short_opts: List[str] = []
self._long_opts: List[str] = []
if "%default" in (attrs.get("help") or ""):
warnings.warn(
'pytest now uses argparse. "%default" should be'
' changed to "%(default)s" ',
DeprecationWarning,
stacklevel=3,
)
warnings.warn(ARGUMENT_PERCENT_DEFAULT, stacklevel=3)
try:
typ = attrs["type"]
except KeyError:
Expand All @@ -227,23 +225,15 @@ def __init__(self, *names: str, **attrs: Any) -> None:
if isinstance(typ, str):
if typ == "choice":
warnings.warn(
"`type` argument to addoption() is the string %r."
" For choices this is optional and can be omitted, "
" but when supplied should be a type (for example `str` or `int`)."
" (options: %s)" % (typ, names),
DeprecationWarning,
ARGUMENT_TYPE_STR_CHOICE.format(typ=typ, names=names),
stacklevel=4,
)
# argparse expects a type here take it from
# the type of the first element
attrs["type"] = type(attrs["choices"][0])
else:
warnings.warn(
"`type` argument to addoption() is the string %r, "
" but when supplied should be a type (for example `str` or `int`)."
" (options: %s)" % (typ, names),
DeprecationWarning,
stacklevel=4,
ARGUMENT_TYPE_STR.format(typ=typ, names=names), stacklevel=4
)
attrs["type"] = Argument._typ_map[typ]
# Used in test_parseopt -> test_parse_defaultgetter.
Expand Down
19 changes: 19 additions & 0 deletions src/_pytest/deprecated.py
Expand Up @@ -69,6 +69,25 @@
"Use pytest.skip() instead."
)

ARGUMENT_PERCENT_DEFAULT = PytestDeprecationWarning(
'pytest now uses argparse. "%default" should be changed to "%(default)s"',
)

ARGUMENT_TYPE_STR_CHOICE = UnformattedWarning(
PytestDeprecationWarning,
"`type` argument to addoption() is the string {typ!r}."
" For choices this is optional and can be omitted, "
" but when supplied should be a type (for example `str` or `int`)."
" (options: {names})",
)

ARGUMENT_TYPE_STR = UnformattedWarning(
PytestDeprecationWarning,
"`type` argument to addoption() is the string {typ!r}, "
" but when supplied should be a type (for example `str` or `int`)."
" (options: {names})",
)


# You want to make some `__init__` or function "private".
#
Expand Down

0 comments on commit e490823

Please sign in to comment.