Deprecate raises(..., 'code(as_a_string)') / `warns(..., 'code(as_a_string)')
This commit is contained in:
@@ -92,6 +92,15 @@ NODE_WARN = RemovedInPytest4Warning(
|
||||
"Node.warn(code, message) form has been deprecated, use Node.warn(warning_instance) instead."
|
||||
)
|
||||
|
||||
RAISES_EXEC = PytestDeprecationWarning(
|
||||
"raises(..., 'code(as_a_string)') is deprecated, use the context manager form or use `exec()` directly\n\n"
|
||||
"See https://docs.pytest.org/en/latest/deprecations.html#raises-warns-exec"
|
||||
)
|
||||
WARNS_EXEC = PytestDeprecationWarning(
|
||||
"warns(..., 'code(as_a_string)') is deprecated, use the context manager form or use `exec()` directly.\n\n"
|
||||
"See https://docs.pytest.org/en/latest/deprecations.html#raises-warns-exec"
|
||||
)
|
||||
|
||||
RECORD_XML_PROPERTY = RemovedInPytest4Warning(
|
||||
'Fixture renamed from "record_xml_property" to "record_property" as user '
|
||||
"properties are now available to all reporters.\n"
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import math
|
||||
import pprint
|
||||
import sys
|
||||
import warnings
|
||||
from decimal import Decimal
|
||||
from numbers import Number
|
||||
|
||||
@@ -14,6 +17,7 @@ from _pytest.compat import isclass
|
||||
from _pytest.compat import Mapping
|
||||
from _pytest.compat import Sequence
|
||||
from _pytest.compat import STRING_TYPES
|
||||
from _pytest.deprecated import RAISES_EXEC
|
||||
from _pytest.outcomes import fail
|
||||
|
||||
BASE_TYPE = (type, STRING_TYPES)
|
||||
@@ -604,9 +608,9 @@ def raises(expected_exception, *args, **kwargs):
|
||||
>>> with raises(ValueError, match=r'must be \d+$'):
|
||||
... raise ValueError("value must be 42")
|
||||
|
||||
**Legacy forms**
|
||||
**Legacy form**
|
||||
|
||||
The forms below are fully supported but are discouraged for new code because the
|
||||
The form below is fully supported but discouraged for new code because the
|
||||
context manager form is regarded as more readable and less error-prone.
|
||||
|
||||
It is possible to specify a callable by passing a to-be-called lambda::
|
||||
@@ -623,14 +627,6 @@ def raises(expected_exception, *args, **kwargs):
|
||||
>>> raises(ZeroDivisionError, f, x=0)
|
||||
<ExceptionInfo ...>
|
||||
|
||||
It is also possible to pass a string to be evaluated at runtime::
|
||||
|
||||
>>> raises(ZeroDivisionError, "f(0)")
|
||||
<ExceptionInfo ...>
|
||||
|
||||
The string will be evaluated using the same ``locals()`` and ``globals()``
|
||||
at the moment of the ``raises`` call.
|
||||
|
||||
.. currentmodule:: _pytest._code
|
||||
|
||||
Consult the API of ``excinfo`` objects: :class:`ExceptionInfo`.
|
||||
@@ -672,6 +668,7 @@ def raises(expected_exception, *args, **kwargs):
|
||||
raise TypeError(msg)
|
||||
return RaisesContext(expected_exception, message, match_expr)
|
||||
elif isinstance(args[0], str):
|
||||
warnings.warn(RAISES_EXEC, stacklevel=2)
|
||||
code, = args
|
||||
assert isinstance(code, str)
|
||||
frame = sys._getframe(1)
|
||||
|
||||
@@ -11,6 +11,7 @@ import warnings
|
||||
import six
|
||||
|
||||
import _pytest._code
|
||||
from _pytest.deprecated import WARNS_EXEC
|
||||
from _pytest.fixtures import yield_fixture
|
||||
from _pytest.outcomes import fail
|
||||
|
||||
@@ -89,6 +90,7 @@ def warns(expected_warning, *args, **kwargs):
|
||||
match_expr = kwargs.pop("match")
|
||||
return WarningsChecker(expected_warning, match_expr=match_expr)
|
||||
elif isinstance(args[0], str):
|
||||
warnings.warn(WARNS_EXEC, stacklevel=2)
|
||||
code, = args
|
||||
assert isinstance(code, str)
|
||||
frame = sys._getframe(1)
|
||||
|
||||
Reference in New Issue
Block a user