Files
computer-playbook/tests/unit/python/utils/test_reexport.py
T
kevinveenbirkenbachandClaude Opus 5 a6861da0ed fix(cli): re-export only what a package shim's module defines
Nine package shims listed dir(__main__) and lifted every public name into
the package namespace, so each of them re-exported its imports. pathlib.Path
became a member of nine packages, and sphinx found five targets for a bare
Path annotation: 320 ambiguous cross-references in the documentation build,
down to 10 with this change.

public_names() honours a declared __all__ verbatim and otherwise keeps the
names whose __module__ is the module itself. Nine call sites shared the same
expression, so it lives in one place now.

This narrows a package's public surface: __all__ shrinks and the shim no
longer carries its imports. Every importer in the repository pulls a locally
defined symbol - EXIT_NO_SUCH_APPLICATION_ROLE, find_application_ids,
build_mappings, output_graph - and none pulls Path or Any.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-25 18:40:07 +02:00

50 lines
1.4 KiB
Python

from __future__ import annotations
import unittest
from pathlib import Path
from types import ModuleType
from utils.reexport import public_names
def _module(name: str = "pkg.__main__") -> ModuleType:
"""Return a module that defines one name and imported another."""
module = ModuleType(name)
module.Path = Path
def run() -> None:
"""A name the module defines itself."""
run.__module__ = name
module.run = run
module._private = object()
return module
class TestPublicNames(unittest.TestCase):
def test_an_imported_name_is_not_re_exported(self) -> None:
self.assertEqual(public_names(_module()), ["run"])
def test_an_imported_name_used_to_reach_the_package_namespace(self) -> None:
module = _module()
self.assertIn(
"Path",
[name for name in dir(module) if not name.startswith("_")],
"listing dir() is what nine shims did, and it made Path a member of "
"each of them, so sphinx found the same python target nine times",
)
def test_a_declared_all_is_taken_verbatim(self) -> None:
module = _module()
module.__all__ = ["Path", "run"]
self.assertEqual(public_names(module), ["Path", "run"])
def test_a_private_name_stays_private(self) -> None:
self.assertNotIn("_private", public_names(_module()))
if __name__ == "__main__":
unittest.main()