Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove config.warn, Node.warn and pytest_logwarning #4542

Merged
merged 2 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/3078.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Remove legacy internal warnings system: ``config.warn``, ``Node.warn``. The ``pytest_logwarning`` now issues a warning when implemented.

See our `docs <https://docs.pytest.org/en/latest/deprecations.html#config-warn-and-node-warn>`__ on information on how to update your code.
56 changes: 27 additions & 29 deletions doc/en/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,6 @@ Becomes:



``Config.warn`` and ``Node.warn``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. deprecated:: 3.8

Those methods were part of the internal pytest warnings system, but since ``3.8`` pytest is using the builtin warning
system for its own warnings, so those two functions are now deprecated.

``Config.warn`` should be replaced by calls to the standard ``warnings.warn``, example:

.. code-block:: python

config.warn("C1", "some warning")

Becomes:

.. code-block:: python

warnings.warn(pytest.PytestWarning("some warning"))

``Node.warn`` now supports two signatures:

* ``node.warn(PytestWarning("some message"))``: is now the **recommended** way to call this function.
The warning instance must be a PytestWarning or subclass.

* ``node.warn("CI", "some message")``: this code/message form is now **deprecated** and should be converted to the warning instance form above.


Calling fixtures directly
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -308,7 +280,33 @@ This should be updated to make use of standard fixture mechanisms:
You can consult `funcarg comparison section in the docs <https://docs.pytest.org/en/latest/funcarg_compare.html>`_ for
more information.

This has been documented as deprecated for years, but only now we are actually emitting deprecation warnings.

``Config.warn`` and ``Node.warn``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*Removed in version 4.0.*

Those methods were part of the internal pytest warnings system, but since ``3.8`` pytest is using the builtin warning
system for its own warnings, so those two functions are now deprecated.

``Config.warn`` should be replaced by calls to the standard ``warnings.warn``, example:

.. code-block:: python

config.warn("C1", "some warning")

Becomes:

.. code-block:: python

warnings.warn(pytest.PytestWarning("some warning"))

``Node.warn`` now supports two signatures:

* ``node.warn(PytestWarning("some message"))``: is now the **recommended** way to call this function.
The warning instance must be a PytestWarning or subclass.

* ``node.warn("CI", "some message")``: this code/message form has been **removed** and should be converted to the warning instance form above.

record_xml_property
~~~~~~~~~~~~~~~~~~~
Expand Down
1 change: 0 additions & 1 deletion doc/en/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,6 @@ Session related reporting hooks:
.. autofunction:: pytest_terminal_summary
.. autofunction:: pytest_fixture_setup
.. autofunction:: pytest_fixture_post_finalizer
.. autofunction:: pytest_logwarning
.. autofunction:: pytest_warning_captured

And here is the central hook for reporting about
Expand Down
6 changes: 3 additions & 3 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ def mark_rewrite(self, *names):

def _warn_already_imported(self, name):
from _pytest.warning_types import PytestWarning
from _pytest.warnings import _issue_config_warning
from _pytest.warnings import _issue_warning_captured

_issue_config_warning(
_issue_warning_captured(
PytestWarning("Module already imported so cannot be rewritten: %s" % name),
self.config,
self.config.hook,
stacklevel=5,
)

Expand Down
6 changes: 3 additions & 3 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def cache_dir_from_config(config):
return resolve_from_str(config.getini("cache_dir"), config.rootdir)

def warn(self, fmt, **args):
from _pytest.warnings import _issue_config_warning
from _pytest.warnings import _issue_warning_captured
from _pytest.warning_types import PytestWarning

_issue_config_warning(
_issue_warning_captured(
PytestWarning(fmt.format(**args) if args else fmt),
self._config,
self._config.hook,
stacklevel=3,
)

Expand Down
82 changes: 27 additions & 55 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
from .exceptions import UsageError
from .findpaths import determine_setup
from .findpaths import exists
from _pytest import deprecated
from _pytest._code import ExceptionInfo
from _pytest._code import filter_traceback
from _pytest.compat import lru_cache
from _pytest.compat import safe_str
from _pytest.outcomes import Skipped
from _pytest.warning_types import PytestWarning

hookimpl = HookimplMarker("pytest")
hookspec = HookspecMarker("pytest")
Expand Down Expand Up @@ -186,9 +188,9 @@ def _prepareconfig(args=None, plugins=None):
else:
pluginmanager.register(plugin)
if warning:
from _pytest.warnings import _issue_config_warning
from _pytest.warnings import _issue_warning_captured

_issue_config_warning(warning, config=config, stacklevel=4)
_issue_warning_captured(warning, hook=config.hook, stacklevel=4)
return pluginmanager.hook.pytest_cmdline_parse(
pluginmanager=pluginmanager, args=args
)
Expand Down Expand Up @@ -242,14 +244,7 @@ def addhooks(self, module_or_class):
Use :py:meth:`pluggy.PluginManager.add_hookspecs <PluginManager.add_hookspecs>`
instead.
"""
warning = dict(
code="I2",
fslocation=_pytest._code.getfslineno(sys._getframe(1)),
nodeid=None,
message="use pluginmanager.add_hookspecs instead of "
"deprecated addhooks() method.",
)
self._warn(warning)
warnings.warn(deprecated.PLUGIN_MANAGER_ADDHOOKS, stacklevel=2)
return self.add_hookspecs(module_or_class)

def parse_hookimpl_opts(self, plugin, name):
Expand Down Expand Up @@ -293,10 +288,12 @@ def parse_hookspec_opts(self, module_or_class, name):

def register(self, plugin, name=None):
if name in ["pytest_catchlog", "pytest_capturelog"]:
self._warn(
"{} plugin has been merged into the core, "
"please remove it from your requirements.".format(
name.replace("_", "-")
warnings.warn(
PytestWarning(
"{} plugin has been merged into the core, "
"please remove it from your requirements.".format(
name.replace("_", "-")
)
)
)
return
Expand Down Expand Up @@ -333,14 +330,6 @@ def pytest_configure(self, config):
)
self._configured = True

def _warn(self, message):
kwargs = (
message
if isinstance(message, dict)
else {"code": "I1", "message": message, "fslocation": None, "nodeid": None}
)
self.hook.pytest_logwarning.call_historic(kwargs=kwargs)

#
# internal API for local conftest plugin handling
#
Expand Down Expand Up @@ -539,7 +528,13 @@ def import_plugin(self, modname):
six.reraise(new_exc_type, new_exc, sys.exc_info()[2])

except Skipped as e:
self._warn("skipped plugin %r: %s" % ((modname, e.msg)))
from _pytest.warnings import _issue_warning_captured

_issue_warning_captured(
PytestWarning("skipped plugin %r: %s" % (modname, e.msg)),
self.hook,
stacklevel=1,
)
else:
mod = sys.modules[importspec]
self.register(mod, modname)
Expand Down Expand Up @@ -614,7 +609,6 @@ def __init__(self, pluginmanager):
self._override_ini = ()
self._opt2dest = {}
self._cleanup = []
self._warn = self.pluginmanager._warn
self.pluginmanager.register(self, "pytestconfig")
self._configured = False
self.invocation_dir = py.path.local()
Expand All @@ -639,36 +633,6 @@ def _ensure_unconfigure(self):
fin = self._cleanup.pop()
fin()

def warn(self, code, message, fslocation=None, nodeid=None):
"""
.. deprecated:: 3.8

Use :py:func:`warnings.warn` or :py:func:`warnings.warn_explicit` directly instead.

Generate a warning for this test session.
"""
from _pytest.warning_types import RemovedInPytest4Warning

if isinstance(fslocation, (tuple, list)) and len(fslocation) > 2:
filename, lineno = fslocation[:2]
else:
filename = "unknown file"
lineno = 0
msg = "config.warn has been deprecated, use warnings.warn instead"
if nodeid:
msg = "{}: {}".format(nodeid, msg)
warnings.warn_explicit(
RemovedInPytest4Warning(msg),
category=None,
filename=filename,
lineno=lineno,
)
self.hook.pytest_logwarning.call_historic(
kwargs=dict(
code=code, message=message, fslocation=fslocation, nodeid=nodeid
)
)

def get_terminal_writer(self):
return self.pluginmanager.get_plugin("terminalreporter")._tw

Expand Down Expand Up @@ -823,7 +787,15 @@ def _preparse(self, args, addopts=True):
if ns.help or ns.version:
# we don't want to prevent --help/--version to work
# so just let is pass and print a warning at the end
self._warn("could not load initial conftests (%s)\n" % e.path)
from _pytest.warnings import _issue_warning_captured

_issue_warning_captured(
PytestWarning(
"could not load initial conftests: {}".format(e.path)
),
self.hook,
stacklevel=2,
)
else:
raise

Expand Down
12 changes: 6 additions & 6 deletions src/_pytest/config/findpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def getcfg(args, config=None):
iniconfig = py.iniconfig.IniConfig(p)
if "pytest" in iniconfig.sections:
if inibasename == "setup.cfg" and config is not None:
from _pytest.warnings import _issue_config_warning
from _pytest.warnings import _issue_warning_captured
from _pytest.warning_types import RemovedInPytest4Warning

_issue_config_warning(
_issue_warning_captured(
RemovedInPytest4Warning(
CFG_PYTEST_SECTION.format(filename=inibasename)
),
config=config,
hook=config.hook,
stacklevel=2,
)
return base, p, iniconfig["pytest"]
Expand Down Expand Up @@ -112,13 +112,13 @@ def determine_setup(inifile, args, rootdir_cmd_arg=None, config=None):
inicfg = iniconfig[section]
if is_cfg_file and section == "pytest" and config is not None:
from _pytest.deprecated import CFG_PYTEST_SECTION
from _pytest.warnings import _issue_config_warning
from _pytest.warnings import _issue_warning_captured

# TODO: [pytest] section in *.cfg files is deprecated. Need refactoring once
# the deprecation expires.
_issue_config_warning(
_issue_warning_captured(
CFG_PYTEST_SECTION.format(filename=str(inifile)),
config,
config.hook,
stacklevel=2,
)
break
Expand Down
10 changes: 5 additions & 5 deletions src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@
"For more details, see: https://docs.pytest.org/en/latest/parametrize.html"
)

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"
Expand All @@ -77,7 +73,6 @@
"See https://docs.pytest.org/en/latest/deprecations.html#raises-warns-exec"
)


PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST = RemovedInPytest4Warning(
"Defining pytest_plugins in a non-top-level conftest is deprecated, "
"because it affects the entire directory tree in a non-explicit way.\n"
Expand All @@ -93,3 +88,8 @@
"pytest/tmpdir_factory.ensuretemp is deprecated, \n"
"please use the tmp_path fixture or tmp_path_factory.mktemp"
)

PYTEST_LOGWARNING = PytestDeprecationWarning(
"pytest_logwarning is deprecated, no longer being called, and will be removed soon\n"
"please use pytest_warning_captured instead"
)
4 changes: 3 additions & 1 deletion src/_pytest/hookspec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
from pluggy import HookspecMarker

from _pytest.deprecated import PYTEST_LOGWARNING


hookspec = HookspecMarker("pytest")

Expand Down Expand Up @@ -496,7 +498,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus):
"""


@hookspec(historic=True)
@hookspec(historic=True, warn_on_impl=PYTEST_LOGWARNING)
def pytest_logwarning(message, code, nodeid, fslocation):
"""
.. deprecated:: 3.8
Expand Down
Loading