Remove pytest_funcarg__ prefix support for defining fixtures

Fix #4543
This commit is contained in:
Bruno Oliveira 2018-12-13 21:14:41 -02:00
parent 26d202a7bd
commit 1e80a9cb34
7 changed files with 30 additions and 96 deletions

View File

@ -0,0 +1,3 @@
Remove support to define fixtures using the ``pytest_funcarg__`` prefix. Use the ``@pytest.fixture`` decorator instead.
See our `docs <https://docs.pytest.org/en/latest/deprecations.html#pytest-funcarg-prefix>`__ on information on how to update your code.

View File

@ -237,26 +237,6 @@ By passing a string, users expect that pytest will interpret that command-line u
on (for example ``bash`` or ``Powershell``), but this is very hard/impossible to do in a portable way. on (for example ``bash`` or ``Powershell``), but this is very hard/impossible to do in a portable way.
``pytest_funcarg__`` prefix
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. deprecated:: 3.0
In very early pytest versions fixtures could be defined using the ``pytest_funcarg__`` prefix:
.. code-block:: python
def pytest_funcarg__data():
return SomeData()
Switch over to the ``@pytest.fixture`` decorator:
.. code-block:: python
@pytest.fixture
def data():
return SomeData()
[pytest] section in setup.cfg files [pytest] section in setup.cfg files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -295,6 +275,27 @@ collection.
This issue should affect only advanced plugins who create new collection types, so if you see this warning This issue should affect only advanced plugins who create new collection types, so if you see this warning
message please contact the authors so they can change the code. message please contact the authors so they can change the code.
``pytest_funcarg__`` prefix
~~~~~~~~~~~~~~~~~~~~~~~~~~~
*Removed in version 4.0.*
In very early pytest versions fixtures could be defined using the ``pytest_funcarg__`` prefix:
.. code-block:: python
def pytest_funcarg__data():
return SomeData()
Switch over to the ``@pytest.fixture`` decorator:
.. code-block:: python
@pytest.fixture
def data():
return SomeData()
Metafunc.addcall Metafunc.addcall
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~

View File

@ -261,8 +261,8 @@ class PytestPluginManager(PluginManager):
# (see issue #1073) # (see issue #1073)
if not name.startswith("pytest_"): if not name.startswith("pytest_"):
return return
# ignore some historic special names which can not be hooks anyway # ignore names which can not be hooks
if name == "pytest_plugins" or name.startswith("pytest_funcarg__"): if name == "pytest_plugins":
return return
method = getattr(plugin, name) method = getattr(plugin, name)

View File

@ -24,12 +24,6 @@ MAIN_STR_ARGS = RemovedInPytest4Warning(
YIELD_TESTS = "yield tests were removed in pytest 4.0 - {name} will be ignored" YIELD_TESTS = "yield tests were removed in pytest 4.0 - {name} will be ignored"
FUNCARG_PREFIX = UnformattedWarning(
RemovedInPytest4Warning,
'{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated '
"and scheduled to be removed in pytest 4.0. "
"Please remove the prefix and use the @pytest.fixture decorator instead.",
)
FIXTURE_FUNCTION_CALL = UnformattedWarning( FIXTURE_FUNCTION_CALL = UnformattedWarning(
RemovedInPytest4Warning, RemovedInPytest4Warning,

View File

@ -38,8 +38,6 @@ from _pytest.deprecated import FIXTURE_NAMED_REQUEST
from _pytest.outcomes import fail from _pytest.outcomes import fail
from _pytest.outcomes import TEST_OUTCOME from _pytest.outcomes import TEST_OUTCOME
FIXTURE_MSG = 'fixtures cannot have "pytest_funcarg__" prefix and be decorated with @pytest.fixture:\n{}'
@attr.s(frozen=True) @attr.s(frozen=True)
class PseudoFixtureDef(object): class PseudoFixtureDef(object):
@ -1117,7 +1115,6 @@ class FixtureManager(object):
by a lookup of their FuncFixtureInfo. by a lookup of their FuncFixtureInfo.
""" """
_argprefix = "pytest_funcarg__"
FixtureLookupError = FixtureLookupError FixtureLookupError = FixtureLookupError
FixtureLookupErrorRepr = FixtureLookupErrorRepr FixtureLookupErrorRepr = FixtureLookupErrorRepr
@ -1255,8 +1252,6 @@ class FixtureManager(object):
items[:] = reorder_items(items) items[:] = reorder_items(items)
def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False): def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
from _pytest import deprecated
if nodeid is not NOTSET: if nodeid is not NOTSET:
holderobj = node_or_obj holderobj = node_or_obj
else: else:
@ -1272,31 +1267,13 @@ class FixtureManager(object):
# access below can raise. safe_getatt() ignores such exceptions. # access below can raise. safe_getatt() ignores such exceptions.
obj = safe_getattr(holderobj, name, None) obj = safe_getattr(holderobj, name, None)
marker = getfixturemarker(obj) marker = getfixturemarker(obj)
# fixture functions have a pytest_funcarg__ prefix (pre-2.3 style) if not isinstance(marker, FixtureFunctionMarker):
# or are "@pytest.fixture" marked
if marker is None:
if not name.startswith(self._argprefix):
continue
if not callable(obj):
continue
marker = defaultfuncargprefixmarker
filename, lineno = getfslineno(obj)
warnings.warn_explicit(
deprecated.FUNCARG_PREFIX.format(name=name),
category=None,
filename=str(filename),
lineno=lineno + 1,
)
name = name[len(self._argprefix) :]
elif not isinstance(marker, FixtureFunctionMarker):
# magic globals with __getattr__ might have got us a wrong # magic globals with __getattr__ might have got us a wrong
# fixture attribute # fixture attribute
continue continue
else:
if marker.name: if marker.name:
name = marker.name name = marker.name
assert not name.startswith(self._argprefix), FIXTURE_MSG.format(name)
# during fixture definition we wrap the original fixture function # during fixture definition we wrap the original fixture function
# to issue a warning if called directly, so here we unwrap it in order to not emit the warning # to issue a warning if called directly, so here we unwrap it in order to not emit the warning

View File

@ -10,28 +10,6 @@ from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG
pytestmark = pytest.mark.pytester_example_path("deprecated") pytestmark = pytest.mark.pytester_example_path("deprecated")
def test_funcarg_prefix_deprecation(testdir):
testdir.makepyfile(
"""
def pytest_funcarg__value():
return 10
def test_funcarg_prefix(value):
assert value == 10
"""
)
result = testdir.runpytest("-ra", SHOW_PYTEST_WARNINGS_ARG)
result.stdout.fnmatch_lines(
[
(
"*test_funcarg_prefix_deprecation.py:1: *pytest_funcarg__value: "
'declaring fixtures using "pytest_funcarg__" prefix is deprecated*'
),
"*1 passed*",
]
)
@pytest.mark.filterwarnings("default") @pytest.mark.filterwarnings("default")
def test_pytest_setup_cfg_deprecated(testdir): def test_pytest_setup_cfg_deprecated(testdir):
testdir.makefile( testdir.makefile(

View File

@ -627,25 +627,6 @@ class TestRequestBasic(object):
print(ss.stack) print(ss.stack)
assert teardownlist == [1] assert teardownlist == [1]
def test_mark_as_fixture_with_prefix_and_decorator_fails(self, testdir):
testdir.makeconftest(
"""
import pytest
@pytest.fixture
def pytest_funcarg__marked_with_prefix_and_decorator():
pass
"""
)
result = testdir.runpytest_subprocess()
assert result.ret != 0
result.stdout.fnmatch_lines(
[
"*AssertionError: fixtures cannot have*@pytest.fixture*",
"*pytest_funcarg__marked_with_prefix_and_decorator*",
]
)
def test_request_addfinalizer_failing_setup(self, testdir): def test_request_addfinalizer_failing_setup(self, testdir):
testdir.makepyfile( testdir.makepyfile(
""" """