"""Import the package under test, installed or not — and get the same one every time. Two facts collide here. `probe.py` and half of this suite must run with NOTHING installed, which is why the package is registered as a namespace pointing at the source directory and its leaf modules are imported under it: `__init__` never executes, so `verifiers` is never required. And `test_taskset.py` needs the opposite — `verifiers` present and `grand_exchange_live` resolving to the REAL package, because `import_taskset` reads `__all__` off whatever is in `sys.modules` and a namespace shim has none. A shim installed by whichever test module imported first would decide that for the whole process. So the decision is made once, here, and every test module imports this before it imports anything else: the real package if it is importable, the shim if it is not. """ from __future__ import annotations import sys import types from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def shim(package: Path) -> None: """Register `package` as a namespace so its leaf modules import without its `__init__`.""" module = types.ModuleType(package.name) module.__path__ = [str(package)] sys.modules[package.name] = module try: import grand_exchange_live INSTALLED = getattr(grand_exchange_live, "__all__", None) is not None except ImportError: # The package is there and `verifiers` is not, which is the no-dependency path. INSTALLED = False if not INSTALLED: shim(ROOT / ROOT.name)