Just raise deprecation warning
This commit is contained in:
parent
e700e667e6
commit
3f10d53b8c
|
@ -819,7 +819,7 @@ case we just write some information out to a ``failures`` file:
|
||||||
with open("failures", mode, encoding="utf-8") as f:
|
with open("failures", mode, encoding="utf-8") as f:
|
||||||
# let's also access a fixture for the fun of it
|
# let's also access a fixture for the fun of it
|
||||||
if "tmp_path" in item.fixturenames:
|
if "tmp_path" in item.fixturenames:
|
||||||
extra = " ({})".format(item.funcargs["tmp_path"])
|
extra = " ({})".format(item._request.getfixturevalue("tmp_path"))
|
||||||
else:
|
else:
|
||||||
extra = ""
|
extra = ""
|
||||||
|
|
||||||
|
|
|
@ -683,11 +683,9 @@ class TopRequest(FixtureRequest):
|
||||||
def _fillfixtures(self) -> None:
|
def _fillfixtures(self) -> None:
|
||||||
item = self._pyfuncitem
|
item = self._pyfuncitem
|
||||||
fixturenames = getattr(item, "fixturenames", self.fixturenames)
|
fixturenames = getattr(item, "fixturenames", self.fixturenames)
|
||||||
initialnames = item._fixtureinfo.initialnames
|
|
||||||
for argname in fixturenames:
|
for argname in fixturenames:
|
||||||
value = self.getfixturevalue(argname)
|
if argname not in item.funcargs:
|
||||||
if argname not in item.funcargs and argname in initialnames:
|
item.funcargs[argname] = self.getfixturevalue(argname)
|
||||||
item.funcargs[argname] = value
|
|
||||||
|
|
||||||
def addfinalizer(self, finalizer: Callable[[], object]) -> None:
|
def addfinalizer(self, finalizer: Callable[[], object]) -> None:
|
||||||
self.node.addfinalizer(finalizer)
|
self.node.addfinalizer(finalizer)
|
||||||
|
|
|
@ -1659,6 +1659,23 @@ def write_docstring(tw: TerminalWriter, doc: str, indent: str = " ") -> None:
|
||||||
tw.line(indent + line)
|
tw.line(indent + line)
|
||||||
|
|
||||||
|
|
||||||
|
class DeprecatingFuncArgs(Dict[str, object]):
|
||||||
|
def __init__(self, initialnames):
|
||||||
|
self.initialnames = initialnames
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def __getitem__(self, key: str) -> object:
|
||||||
|
if key not in self.initialnames:
|
||||||
|
warnings.warn(
|
||||||
|
"Accessing to names other than initialnames i.e., direct args,"
|
||||||
|
" the ones with `usefixture` or the ones with `autouse` through "
|
||||||
|
"`item.funcargs` is deprecated and will raise `KeyError` from "
|
||||||
|
"pytest 9. Please use `request.getfixturevalue` instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
)
|
||||||
|
return super().__getitem__(key)
|
||||||
|
|
||||||
|
|
||||||
class Function(PyobjMixin, nodes.Item):
|
class Function(PyobjMixin, nodes.Item):
|
||||||
"""Item responsible for setting up and executing a Python test function.
|
"""Item responsible for setting up and executing a Python test function.
|
||||||
|
|
||||||
|
@ -1747,7 +1764,9 @@ class Function(PyobjMixin, nodes.Item):
|
||||||
return super().from_parent(parent=parent, **kw)
|
return super().from_parent(parent=parent, **kw)
|
||||||
|
|
||||||
def _initrequest(self) -> None:
|
def _initrequest(self) -> None:
|
||||||
self.funcargs: Dict[str, object] = {}
|
self.funcargs: Dict[str, object] = DeprecatingFuncArgs(
|
||||||
|
self._fixtureinfo.initialnames
|
||||||
|
)
|
||||||
self._request = fixtures.TopRequest(self, _ispytest=True)
|
self._request = fixtures.TopRequest(self, _ispytest=True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -9,6 +9,7 @@ from _pytest.config import ExitCode
|
||||||
from _pytest.fixtures import deduplicate_names
|
from _pytest.fixtures import deduplicate_names
|
||||||
from _pytest.fixtures import TopRequest
|
from _pytest.fixtures import TopRequest
|
||||||
from _pytest.monkeypatch import MonkeyPatch
|
from _pytest.monkeypatch import MonkeyPatch
|
||||||
|
from _pytest.pytester import get_public_names
|
||||||
from _pytest.pytester import Pytester
|
from _pytest.pytester import Pytester
|
||||||
from _pytest.python import Function
|
from _pytest.python import Function
|
||||||
|
|
||||||
|
@ -121,15 +122,20 @@ class TestFillFixtures:
|
||||||
["*recursive dependency involving fixture 'fix1' detected*"]
|
["*recursive dependency involving fixture 'fix1' detected*"]
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_funcarg_basic(self, pytester: Pytester) -> None:
|
def test_funcarg_basic(self, recwarn, pytester: Pytester) -> None:
|
||||||
pytester.copy_example()
|
pytester.copy_example()
|
||||||
item = pytester.getitem(Path("test_funcarg_basic.py"))
|
item = pytester.getitem(Path("test_funcarg_basic.py"))
|
||||||
assert isinstance(item, Function)
|
assert isinstance(item, Function)
|
||||||
# Execute's item's setup, which fills fixtures.
|
# Execute's item's setup, which fills fixtures.
|
||||||
item.session._setupstate.setup(item)
|
item.session._setupstate.setup(item)
|
||||||
assert len(item.funcargs) == 2
|
assert len(recwarn) == 0
|
||||||
|
item.funcargs["request"]
|
||||||
|
assert len(recwarn) == 1 and recwarn[0].category is DeprecationWarning
|
||||||
|
del item.funcargs["request"]
|
||||||
|
assert len(get_public_names(item.funcargs)) == 2
|
||||||
assert item.funcargs["some"] == "test_func"
|
assert item.funcargs["some"] == "test_func"
|
||||||
assert item.funcargs["other"] == 42
|
assert item.funcargs["other"] == 42
|
||||||
|
assert len(recwarn) == 1
|
||||||
|
|
||||||
def test_funcarg_lookup_modulelevel(self, pytester: Pytester) -> None:
|
def test_funcarg_lookup_modulelevel(self, pytester: Pytester) -> None:
|
||||||
pytester.copy_example()
|
pytester.copy_example()
|
||||||
|
@ -839,7 +845,8 @@ class TestRequestBasic:
|
||||||
val2 = req.getfixturevalue("other") # see about caching
|
val2 = req.getfixturevalue("other") # see about caching
|
||||||
assert val2 == 2
|
assert val2 == 2
|
||||||
assert item.funcargs["something"] == 1
|
assert item.funcargs["something"] == 1
|
||||||
assert len(item.funcargs) == 1
|
assert len(get_public_names(item.funcargs)) == 2
|
||||||
|
assert "request" in item.funcargs
|
||||||
|
|
||||||
def test_request_addfinalizer(self, pytester: Pytester) -> None:
|
def test_request_addfinalizer(self, pytester: Pytester) -> None:
|
||||||
item = pytester.getitem(
|
item = pytester.getitem(
|
||||||
|
|
Loading…
Reference in New Issue