|
| 1 | +"""Deterministic import-footprint regression gate. |
| 2 | +
|
| 3 | +Each target module is imported in a clean subprocess; a per-target denylist of |
| 4 | +heavy modules must be absent from sys.modules afterward. This is the CI |
| 5 | +performance guard: it fails the moment an eager heavy import is reintroduced. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import os |
| 10 | +import subprocess |
| 11 | +import sys |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +# (import target, modules that must be ABSENT from sys.modules afterward) |
| 16 | +FOOTPRINT_CASES = [ |
| 17 | + ('python_utils', ('typing_extensions', 'asyncio')), |
| 18 | + ('python_utils.time', ('typing_extensions', 'asyncio')), |
| 19 | + ('python_utils.logger', ('typing_extensions',)), |
| 20 | + ('python_utils.converters', ('typing_extensions', 'asyncio')), |
| 21 | + ('python_utils.formatters', ('typing_extensions', 'asyncio')), |
| 22 | + ('python_utils.import_', ('typing_extensions', 'asyncio')), |
| 23 | + ('python_utils.terminal', ('typing_extensions',)), |
| 24 | + ('python_utils.containers', ('typing_extensions', 'asyncio')), |
| 25 | + ('python_utils.decorators', ('typing_extensions', 'asyncio')), |
| 26 | + ('python_utils.exceptions', ('typing_extensions', 'asyncio')), |
| 27 | + # aio, generators legitimately use asyncio; only typing_extensions denied. |
| 28 | + ('python_utils.aio', ('typing_extensions',)), |
| 29 | + ('python_utils.generators', ('typing_extensions',)), |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +def _modules_after_import(target: str) -> set[str]: |
| 34 | + result = subprocess.run( |
| 35 | + [ |
| 36 | + sys.executable, |
| 37 | + '-c', |
| 38 | + f'import sys, {target}\n' |
| 39 | + 'import json\n' |
| 40 | + 'print(json.dumps(sorted(sys.modules)))\n', |
| 41 | + ], |
| 42 | + capture_output=True, |
| 43 | + text=True, |
| 44 | + env={**os.environ, 'PYTHONPATH': os.pathsep.join(sys.path)}, |
| 45 | + ) |
| 46 | + assert result.returncode == 0, result.stderr |
| 47 | + |
| 48 | + return set(json.loads(result.stdout)) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize(('target', 'denied'), FOOTPRINT_CASES) |
| 52 | +def test_import_footprint(target: str, denied: tuple[str, ...]) -> None: |
| 53 | + present = _modules_after_import(target) |
| 54 | + leaked = [m for m in denied if m in present] |
| 55 | + assert not leaked, f'{target} eagerly imported {leaked}' |
| 56 | + |
| 57 | + |
| 58 | +def test_bare_import_module_count_under_budget() -> None: |
| 59 | + # Coarse bloat tripwire (denylist above is the real guard). Cap tightened |
| 60 | + # now that __version__ is lazy (importlib.metadata no longer pulled on bare |
| 61 | + # import). Bump only if a new Python version legitimately adds startup |
| 62 | + # modules. Measures modules ADDED by importing python_utils. |
| 63 | + added = len(_modules_after_import('python_utils')) - len( |
| 64 | + _modules_after_import('sys') |
| 65 | + ) |
| 66 | + assert added < 40, f'python_utils added {added} modules to sys.modules' |
| 67 | + |
| 68 | + |
| 69 | +def test_bare_import_does_not_pull_importlib_metadata() -> None: |
| 70 | + # __version__ is resolved lazily; bare import must not call |
| 71 | + # importlib.metadata.version() (which drags in email/zipfile/json/...). |
| 72 | + present = _modules_after_import('python_utils') |
| 73 | + assert 'importlib.metadata' not in present |
| 74 | + |
| 75 | + |
| 76 | +def test_version_resolves_correctly() -> None: |
| 77 | + import python_utils |
| 78 | + |
| 79 | + assert isinstance(python_utils.__version__, str) |
| 80 | + assert python_utils.__version__ # non-empty |
| 81 | + |
| 82 | + |
| 83 | +PUBLIC_CALLABLES_TO_INTROSPECT = [ |
| 84 | + ('python_utils.time', 'timeout_generator'), |
| 85 | + ('python_utils.time', 'aio_timeout_generator'), |
| 86 | + ('python_utils.time', 'format_time'), |
| 87 | + ('python_utils.converters', 'remap'), |
| 88 | + ('python_utils.converters', 'to_int'), |
| 89 | + ('python_utils.formatters', 'timesince'), |
| 90 | + ('python_utils.import_', 'import_global'), |
| 91 | +] |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize(('module', 'name'), PUBLIC_CALLABLES_TO_INTROSPECT) |
| 95 | +def test_get_type_hints_still_resolves(module: str, name: str) -> None: |
| 96 | + import importlib |
| 97 | + import typing |
| 98 | + |
| 99 | + obj = getattr(importlib.import_module(module), name) |
| 100 | + # Must not raise NameError now that type imports moved/changed. |
| 101 | + typing.get_type_hints(obj) |
0 commit comments