diff --git a/docs/adr/0001-expose-installs-into-modules-only.md b/docs/adr/0001-expose-installs-into-modules-only.md index 657d70b..cf255df 100644 --- a/docs/adr/0001-expose-installs-into-modules-only.md +++ b/docs/adr/0001-expose-installs-into-modules-only.md @@ -5,7 +5,7 @@ rejecting an empty call and raising on a name two groups both claim, and install module. The decision half already lives in the private, pure `_collect_fixtures`; the recurring proposal is to extract the install half too, wrapping the `name -> provider` mapping in a `FixtureSet` exposing `.install(into=...)`. We will not, because a seam needs two adapters and the -only install targets are the caller's module, located via `inspect.stack()`, and an explicit +only install targets are the caller's module, located through the calling frame, and an explicit `module=`, both `types.ModuleType`, which is one adapter exercised with two instances. Deleting a `FixtureSet` would bring back no complexity, since its `setattr` loop simply inlines, so the install step stays that loop and the public surface stays at `expose` and `modern_di_fixture`. A genuinely diff --git a/modern_di_pytest/factory.py b/modern_di_pytest/factory.py index 1fb3476..3ff5418 100644 --- a/modern_di_pytest/factory.py +++ b/modern_di_pytest/factory.py @@ -1,4 +1,5 @@ import inspect +import sys import types import typing @@ -129,7 +130,7 @@ def expose( A provider with this name raises ``ValueError``. pytest_scope: pytest fixture scope applied to every generated fixture. module: Module to install fixtures onto. Defaults to the caller's module - (located via ``inspect.stack()``). + (located via the calling frame). Example (in ``conftest.py``):: @@ -144,8 +145,7 @@ def expose( """ _require_groups(groups) if module is None: - frame = inspect.stack()[1].frame - module = inspect.getmodule(frame) + module = inspect.getmodule(sys._getframe(1)) # noqa: SLF001 if module is None: msg = "expose() could not determine the caller module; pass module=... explicitly." raise RuntimeError(msg)