Skip to content

Commit 4bf2285

Browse files
Just raise deprecation warning
1 parent e003b35 commit 4bf2285

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

doc/en/example/simple.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ case we just write some information out to a ``failures`` file:
819819
with open("failures", mode, encoding="utf-8") as f:
820820
# let's also access a fixture for the fun of it
821821
if "tmp_path" in item.fixturenames:
822-
extra = " ({})".format(item.funcargs["tmp_path"])
822+
extra = " ({})".format(item._request.getfixturevalue("tmp_path"))
823823
else:
824824
extra = ""
825825

src/_pytest/fixtures.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,11 +686,9 @@ def __repr__(self) -> str:
686686
def _fillfixtures(self) -> None:
687687
item = self._pyfuncitem
688688
fixturenames = getattr(item, "fixturenames", self.fixturenames)
689-
initialnames = item._fixtureinfo.initialnames
690689
for argname in fixturenames:
691-
value = self.getfixturevalue(argname)
692-
if argname not in item.funcargs and argname in initialnames:
693-
item.funcargs[argname] = value
690+
if argname not in item.funcargs:
691+
item.funcargs[argname] = self.getfixturevalue(argname)
694692

695693
def addfinalizer(self, finalizer: Callable[[], object]) -> None:
696694
self.node.addfinalizer(finalizer)

src/_pytest/python.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,23 @@ def write_docstring(tw: TerminalWriter, doc: str, indent: str = " ") -> None:
17241724
tw.line(indent + line)
17251725

17261726

1727+
class DeprecatingFuncArgs(Dict[str, object]):
1728+
def __init__(self, initialnames):
1729+
self.initialnames = initialnames
1730+
super().__init__()
1731+
1732+
def __getitem__(self, key: str) -> object:
1733+
if key not in self.initialnames:
1734+
warnings.warn(
1735+
"Accessing to names other than initialnames i.e., direct args,"
1736+
" the ones with `usefixture` or the ones with `autouse` through "
1737+
"`item.funcargs` is deprecated and will raise `KeyError` from "
1738+
"pytest 9. Please use `request.getfixturevalue` instead.",
1739+
DeprecationWarning,
1740+
)
1741+
return super().__getitem__(key)
1742+
1743+
17271744
class Function(PyobjMixin, nodes.Item):
17281745
"""Item responsible for setting up and executing a Python test function.
17291746
@@ -1813,7 +1830,9 @@ def from_parent(cls, parent, **kw): # todo: determine sound type limitations
18131830
return super().from_parent(parent=parent, **kw)
18141831

18151832
def _initrequest(self) -> None:
1816-
self.funcargs: Dict[str, object] = {}
1833+
self.funcargs: Dict[str, object] = DeprecatingFuncArgs(
1834+
self._fixtureinfo.initialnames
1835+
)
18171836
self._request = fixtures.TopRequest(self, _ispytest=True)
18181837

18191838
@property

testing/python/fixtures.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from _pytest.config import ExitCode
99
from _pytest.fixtures import TopRequest
1010
from _pytest.monkeypatch import MonkeyPatch
11+
from _pytest.pytester import get_public_names
1112
from _pytest.pytester import Pytester
1213
from _pytest.python import Function
1314

@@ -120,15 +121,20 @@ def test_detect_recursive_dependency_error(self, pytester: Pytester) -> None:
120121
["*recursive dependency involving fixture 'fix1' detected*"]
121122
)
122123

123-
def test_funcarg_basic(self, pytester: Pytester) -> None:
124+
def test_funcarg_basic(self, recwarn, pytester: Pytester) -> None:
124125
pytester.copy_example()
125126
item = pytester.getitem(Path("test_funcarg_basic.py"))
126127
assert isinstance(item, Function)
127128
# Execute's item's setup, which fills fixtures.
128129
item.session._setupstate.setup(item)
129-
assert len(item.funcargs) == 2
130+
assert len(recwarn) == 0
131+
item.funcargs["request"]
132+
assert len(recwarn) == 1 and recwarn[0].category is DeprecationWarning
133+
del item.funcargs["request"]
134+
assert len(get_public_names(item.funcargs)) == 2
130135
assert item.funcargs["some"] == "test_func"
131136
assert item.funcargs["other"] == 42
137+
assert len(recwarn) == 1
132138

133139
def test_funcarg_lookup_modulelevel(self, pytester: Pytester) -> None:
134140
pytester.copy_example()
@@ -838,7 +844,8 @@ def test_func(something): pass
838844
val2 = req.getfixturevalue("other") # see about caching
839845
assert val2 == 2
840846
assert item.funcargs["something"] == 1
841-
assert len(item.funcargs) == 1
847+
assert len(get_public_names(item.funcargs)) == 2
848+
assert "request" in item.funcargs
842849

843850
def test_request_addfinalizer(self, pytester: Pytester) -> None:
844851
item = pytester.getitem(

0 commit comments

Comments
 (0)