@@ -137,7 +137,7 @@ def get_scope_package(
137
137
def get_scope_node (node : nodes .Node , scope : Scope ) -> nodes .Node | None :
138
138
import _pytest .python
139
139
140
- if scope is Scope .Function :
140
+ if scope is Scope .Function or scope is Scope . Invocation :
141
141
# Type ignored because this is actually safe, see:
142
142
# https://github.com/python/mypy/issues/4717
143
143
return node .getparent (nodes .Item ) # type: ignore[type-abstract]
@@ -185,7 +185,7 @@ def get_parametrized_fixture_argkeys(
185
185
) -> Iterator [FixtureArgKey ]:
186
186
"""Return list of keys for all parametrized arguments which match
187
187
the specified scope."""
188
- assert scope is not Scope . Function
188
+ assert scope in HIGH_SCOPES
189
189
190
190
try :
191
191
callspec : CallSpec2 = item .callspec # type: ignore[attr-defined]
@@ -537,7 +537,7 @@ def getfixturevalue(self, argname: str) -> Any:
537
537
538
538
if (isinstance (fixturedef , FixtureDef )
539
539
and fixturedef is not None
540
- and fixturedef .use_cache is False ):
540
+ and fixturedef .scope == Scope . Invocation . value ):
541
541
self ._fixture_defs .pop (argname )
542
542
543
543
return fixturedef .cached_result [0 ]
@@ -626,7 +626,7 @@ def _get_active_fixturedef(
626
626
finally :
627
627
for arg_name in fixturedef .argnames :
628
628
arg_fixture = self ._fixture_defs .get (arg_name )
629
- if arg_fixture is not None and arg_fixture .use_cache is not True :
629
+ if arg_fixture is not None and arg_fixture .scope == Scope . Invocation . value :
630
630
self ._fixture_defs .pop (arg_name )
631
631
632
632
return fixturedef
@@ -769,7 +769,7 @@ def _check_scope(
769
769
requested_fixturedef : FixtureDef [object ] | PseudoFixtureDef [object ],
770
770
requested_scope : Scope ,
771
771
) -> None :
772
- if isinstance (requested_fixturedef , PseudoFixtureDef ):
772
+ if isinstance (requested_fixturedef , PseudoFixtureDef ) or requested_scope == Scope . Invocation :
773
773
return
774
774
if self ._scope > requested_scope :
775
775
# Try to report something helpful.
@@ -969,7 +969,6 @@ def __init__(
969
969
scope : Scope | _ScopeName | Callable [[str , Config ], _ScopeName ] | None ,
970
970
params : Sequence [object ] | None ,
971
971
ids : tuple [object | None , ...] | Callable [[Any ], object | None ] | None = None ,
972
- use_cache : bool = True ,
973
972
* ,
974
973
_ispytest : bool = False ,
975
974
) -> None :
@@ -1017,7 +1016,6 @@ def __init__(
1017
1016
# Can change if the fixture is executed with different parameters.
1018
1017
self .cached_result : _FixtureCachedResult [FixtureValue ] | None = None
1019
1018
self ._finalizers : Final [list [Callable [[], object ]]] = []
1020
- self .use_cache = use_cache
1021
1019
1022
1020
@property
1023
1021
def scope (self ) -> _ScopeName :
@@ -1068,7 +1066,7 @@ def execute(self, request: SubRequest) -> FixtureValue:
1068
1066
requested_fixtures_that_should_finalize_us .append (fixturedef )
1069
1067
1070
1068
# Check for (and return) cached value/exception.
1071
- if self .cached_result is not None and self .use_cache :
1069
+ if self .cached_result is not None and self .scope != Scope . Invocation . value :
1072
1070
request_cache_key = self .cache_key (request )
1073
1071
cache_key = self .cached_result [1 ]
1074
1072
try :
@@ -1197,7 +1195,6 @@ class FixtureFunctionMarker:
1197
1195
autouse : bool = False
1198
1196
ids : tuple [object | None , ...] | Callable [[Any ], object | None ] | None = None
1199
1197
name : str | None = None
1200
- cache_result : bool = True
1201
1198
1202
1199
_ispytest : dataclasses .InitVar [bool ] = False
1203
1200
@@ -1240,7 +1237,6 @@ def fixture(
1240
1237
autouse : bool = ...,
1241
1238
ids : Sequence [object | None ] | Callable [[Any ], object | None ] | None = ...,
1242
1239
name : str | None = ...,
1243
- cache_result : bool = True ,
1244
1240
) -> FixtureFunction : ...
1245
1241
1246
1242
@@ -1253,7 +1249,6 @@ def fixture(
1253
1249
autouse : bool = ...,
1254
1250
ids : Sequence [object | None ] | Callable [[Any ], object | None ] | None = ...,
1255
1251
name : str | None = None ,
1256
- cache_result : bool = True ,
1257
1252
) -> FixtureFunctionMarker : ...
1258
1253
1259
1254
@@ -1265,7 +1260,6 @@ def fixture(
1265
1260
autouse : bool = False ,
1266
1261
ids : Sequence [object | None ] | Callable [[Any ], object | None ] | None = None ,
1267
1262
name : str | None = None ,
1268
- cache_result : bool = True ,
1269
1263
) -> FixtureFunctionMarker | FixtureFunction :
1270
1264
"""Decorator to mark a fixture factory function.
1271
1265
@@ -1316,11 +1310,6 @@ def fixture(
1316
1310
function arg that requests the fixture; one way to resolve this is to
1317
1311
name the decorated function ``fixture_<fixturename>`` and then use
1318
1312
``@pytest.fixture(name='<fixturename>')``.
1319
-
1320
- :param cache_result:
1321
- If True (the default), the fixture result is cached and the fixture
1322
- only runs once per scope.
1323
- If False, the fixture will run each time it is requested
1324
1313
"""
1325
1314
fixture_marker = FixtureFunctionMarker (
1326
1315
scope = scope ,
@@ -1329,7 +1318,6 @@ def fixture(
1329
1318
ids = None if ids is None else ids if callable (ids ) else tuple (ids ),
1330
1319
name = name ,
1331
1320
_ispytest = True ,
1332
- cache_result = cache_result
1333
1321
)
1334
1322
1335
1323
# Direct decoration.
@@ -1660,7 +1648,6 @@ def _register_fixture(
1660
1648
params : Sequence [object ] | None = None ,
1661
1649
ids : tuple [object | None , ...] | Callable [[Any ], object | None ] | None = None ,
1662
1650
autouse : bool = False ,
1663
- cache_result : bool = True ,
1664
1651
) -> None :
1665
1652
"""Register a fixture
1666
1653
@@ -1691,7 +1678,6 @@ def _register_fixture(
1691
1678
params = params ,
1692
1679
ids = ids ,
1693
1680
_ispytest = True ,
1694
- use_cache = cache_result ,
1695
1681
)
1696
1682
1697
1683
faclist = self ._arg2fixturedefs .setdefault (name , [])
@@ -1788,7 +1774,6 @@ def parsefactories(
1788
1774
params = marker .params ,
1789
1775
ids = marker .ids ,
1790
1776
autouse = marker .autouse ,
1791
- cache_result = marker .cache_result
1792
1777
)
1793
1778
1794
1779
def getfixturedefs (
0 commit comments