Merge pull request #11794 from bluetech/fixturedef-ref-cycle

A few cleanups
This commit is contained in:
Ronny Pfannschmidt 2024-01-10 06:44:14 +01:00 committed by GitHub
commit b1c430820f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 23 deletions

View File

@ -638,9 +638,16 @@ class PytestPluginManager(PluginManager):
if existing is not None:
return cast(types.ModuleType, existing)
# conftest.py files there are not in a Python package all have module
# name "conftest", and thus conflict with each other. Clear the existing
# before loading the new one, otherwise the existing one will be
# returned from the module cache.
pkgpath = resolve_package_path(conftestpath)
if pkgpath is None:
_ensure_removed_sysmodule(conftestpath.stem)
try:
del sys.modules[conftestpath.stem]
except KeyError:
pass
try:
mod = import_path(conftestpath, mode=importmode, root=rootpath)
@ -818,13 +825,6 @@ def _get_plugin_specs_as_list(
)
def _ensure_removed_sysmodule(modname: str) -> None:
try:
del sys.modules[modname]
except KeyError:
pass
class Notset:
def __repr__(self):
return "<NOTSET>"

View File

@ -525,7 +525,7 @@ class FixtureRequest(abc.ABC):
:param msg:
An optional custom error message.
"""
raise self._fixturemanager.FixtureLookupError(None, self, msg)
raise FixtureLookupError(None, self, msg)
def getfixturevalue(self, argname: str) -> Any:
"""Dynamically run a named fixture function.
@ -970,7 +970,7 @@ class FixtureDef(Generic[FixtureValue]):
def __init__(
self,
fixturemanager: "FixtureManager",
config: Config,
baseid: Optional[str],
argname: str,
func: "_FixtureFunc[FixtureValue]",
@ -984,7 +984,6 @@ class FixtureDef(Generic[FixtureValue]):
_ispytest: bool = False,
) -> None:
check_ispytest(_ispytest)
self._fixturemanager = fixturemanager
# The "base" node ID for the fixture.
#
# This is a node ID prefix. A fixture is only available to a node (e.g.
@ -1010,7 +1009,7 @@ class FixtureDef(Generic[FixtureValue]):
if scope is None:
scope = Scope.Function
elif callable(scope):
scope = _eval_scope_callable(scope, argname, fixturemanager.config)
scope = _eval_scope_callable(scope, argname, config)
if isinstance(scope, str):
scope = Scope.from_user(
scope, descr=f"Fixture '{func.__name__}'", where=baseid
@ -1439,9 +1438,6 @@ class FixtureManager:
by a lookup of their FuncFixtureInfo.
"""
FixtureLookupError = FixtureLookupError
FixtureLookupErrorRepr = FixtureLookupErrorRepr
def __init__(self, session: "Session") -> None:
self.session = session
self.config: Config = session.config
@ -1657,7 +1653,7 @@ class FixtureManager:
Set this if this is a unittest fixture.
"""
fixture_def = FixtureDef(
fixturemanager=self,
config=self.config,
baseid=nodeid,
argname=name,
func=func,

View File

@ -1323,7 +1323,7 @@ class Metafunc:
fixturedef = name2pseudofixturedef[argname]
else:
fixturedef = FixtureDef(
fixturemanager=self.definition.session._fixturemanager,
config=self.config,
baseid="",
argname=argname,
func=get_direct_param_fixture_func,

View File

@ -47,20 +47,23 @@ def pytest_fixture_setup(
else:
param = request.param
fixturedef.cached_param = param # type: ignore[attr-defined]
_show_fixture_action(fixturedef, "SETUP")
_show_fixture_action(fixturedef, request.config, "SETUP")
def pytest_fixture_post_finalizer(fixturedef: FixtureDef[object]) -> None:
def pytest_fixture_post_finalizer(
fixturedef: FixtureDef[object], request: SubRequest
) -> None:
if fixturedef.cached_result is not None:
config = fixturedef._fixturemanager.config
config = request.config
if config.option.setupshow:
_show_fixture_action(fixturedef, "TEARDOWN")
_show_fixture_action(fixturedef, request.config, "TEARDOWN")
if hasattr(fixturedef, "cached_param"):
del fixturedef.cached_param # type: ignore[attr-defined]
def _show_fixture_action(fixturedef: FixtureDef[object], msg: str) -> None:
config = fixturedef._fixturemanager.config
def _show_fixture_action(
fixturedef: FixtureDef[object], config: Config, msg: str
) -> None:
capman = config.pluginmanager.getplugin("capturemanager")
if capman:
capman.suspend_global_capture()