Skip to content

Commit 458ecae

Browse files
committed
Replace all usages of "pytest_funcarg__" for @pytest.fixture
1 parent ad4125d commit 458ecae

21 files changed

+237
-101
lines changed

CHANGELOG.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
2])`` only ran once. Now a failure is raised. Fixes `#460`_. Thanks to
4141
`@nikratio`_ for bug report, `@RedBeardCode`_ and `@tomviner`_ for PR.
4242

43-
*
43+
* ``_pytest.monkeypatch.monkeypatch`` class has been renamed to ``_pytest.monkeypatch.MonkeyPatch``
44+
so it doesn't conflict with the ``monkeypatch`` fixture.
4445

4546
*
4647

_pytest/assertion/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77

88
from _pytest.config import hookimpl
9-
from _pytest.monkeypatch import monkeypatch
9+
from _pytest.monkeypatch import MonkeyPatch
1010
from _pytest.assertion import util
1111

1212

@@ -56,7 +56,7 @@ def pytest_load_initial_conftests(early_config, parser, args):
5656

5757
if mode != "plain":
5858
_load_modules(mode)
59-
m = monkeypatch()
59+
m = MonkeyPatch()
6060
early_config._cleanup.append(m.undo)
6161
m.setattr(py.builtin.builtins, 'AssertionError',
6262
reinterpret.AssertionError) # noqa

_pytest/fixtures.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=N
865865
return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)
866866

867867
defaultfuncargprefixmarker = fixture()
868-
funcarg_prefix_warning = 'declaring fixtures using "pytest_funcarg__" prefix is deprecated ' \
868+
funcarg_prefix_warning = '{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated ' \
869869
'and scheduled to be removed in pytest 4.0.\n' \
870870
'remove the prefix and use the @pytest.fixture decorator instead'
871871

@@ -1046,16 +1046,18 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
10461046
if not callable(obj):
10471047
continue
10481048
marker = defaultfuncargprefixmarker
1049+
self.config.warn('C1', funcarg_prefix_warning.format(name=name))
10491050
name = name[len(self._argprefix):]
1050-
self.config.warn('C1', funcarg_prefix_warning)
10511051
elif not isinstance(marker, FixtureFunctionMarker):
10521052
# magic globals with __getattr__ might have got us a wrong
10531053
# fixture attribute
10541054
continue
10551055
else:
10561056
if marker.name:
10571057
name = marker.name
1058-
assert not name.startswith(self._argprefix), name
1058+
msg = 'fixtures cannot have "pytest_funcarg__" prefix ' \
1059+
'and be decorated with @pytest.fixture:\n%s' % name
1060+
assert not name.startswith(self._argprefix), msg
10591061
fixturedef = FixtureDef(self, nodeid, name, obj,
10601062
marker.scope, marker.params,
10611063
unittest=unittest, ids=marker.ids)

_pytest/monkeypatch.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
from py.builtin import _basestring
77

8+
import pytest
9+
810
RE_IMPORT_ERROR_NAME = re.compile("^No module named (.*)$")
911

1012

11-
def pytest_funcarg__monkeypatch(request):
12-
"""The returned ``monkeypatch`` funcarg provides these
13+
@pytest.fixture
14+
def monkeypatch(request):
15+
"""The returned ``monkeypatch`` fixture provides these
1316
helper methods to modify objects, dictionaries or os.environ::
1417
1518
monkeypatch.setattr(obj, name, value, raising=True)
@@ -26,7 +29,7 @@ def pytest_funcarg__monkeypatch(request):
2629
parameter determines if a KeyError or AttributeError
2730
will be raised if the set/deletion operation has no target.
2831
"""
29-
mpatch = monkeypatch()
32+
mpatch = MonkeyPatch()
3033
request.addfinalizer(mpatch.undo)
3134
return mpatch
3235

@@ -93,7 +96,7 @@ def __repr__(self):
9396
notset = Notset()
9497

9598

96-
class monkeypatch:
99+
class MonkeyPatch:
97100
""" Object keeping a record of setattr/item/env/syspath changes. """
98101

99102
def __init__(self):

_pytest/pytester.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ def linecomp(request):
321321
return LineComp()
322322

323323

324-
def pytest_funcarg__LineMatcher(request):
324+
@pytest.fixture(name='LineMatcher')
325+
def LineMatcher_fixture(request):
325326
return LineMatcher
326327

327328

_pytest/tmpdir.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55
import py
6-
from _pytest.monkeypatch import monkeypatch
6+
from _pytest.monkeypatch import MonkeyPatch
77

88

99
class TempdirFactory:
@@ -92,7 +92,7 @@ def pytest_configure(config):
9292
available at pytest_configure time, but ideally should be moved entirely
9393
to the tmpdir_factory session fixture.
9494
"""
95-
mp = monkeypatch()
95+
mp = MonkeyPatch()
9696
t = TempdirFactory(config)
9797
config._cleanup.extend([mp.undo, t.finish])
9898
mp.setattr(config, '_tmpdirhandler', t, raising=False)

testing/code/test_excinfo.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ def test_division_zero():
381381
])
382382

383383
class TestFormattedExcinfo:
384-
def pytest_funcarg__importasmod(self, request):
384+
385+
@pytest.fixture
386+
def importasmod(self, request):
385387
def importasmod(source):
386388
source = _pytest._code.Source(source)
387389
tmpdir = request.getfixturevalue("tmpdir")

testing/python/collect.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -795,21 +795,24 @@ def test_skip_simple(self):
795795

796796
def test_traceback_argsetup(self, testdir):
797797
testdir.makeconftest("""
798-
def pytest_funcarg__hello(request):
798+
import pytest
799+
800+
@pytest.fixture
801+
def hello(request):
799802
raise ValueError("xyz")
800803
""")
801804
p = testdir.makepyfile("def test(hello): pass")
802805
result = testdir.runpytest(p)
803806
assert result.ret != 0
804807
out = result.stdout.str()
805-
assert out.find("xyz") != -1
806-
assert out.find("conftest.py:2: ValueError") != -1
808+
assert "xyz" in out
809+
assert "conftest.py:5: ValueError" in out
807810
numentries = out.count("_ _ _") # separator for traceback entries
808811
assert numentries == 0
809812

810813
result = testdir.runpytest("--fulltrace", p)
811814
out = result.stdout.str()
812-
assert out.find("conftest.py:2: ValueError") != -1
815+
assert "conftest.py:5: ValueError" in out
813816
numentries = out.count("_ _ _ _") # separator for traceback entries
814817
assert numentries > 3
815818

0 commit comments

Comments
 (0)