diff --git a/CHANGES b/CHANGES index 51daf804..d2fc1ec8 100644 --- a/CHANGES +++ b/CHANGES @@ -37,6 +37,16 @@ Deleting an attribute on {class}`~libvt.snapshot.Snapshot` now raises ### Documentation +#### Document lazy root imports and module boundaries + +{ref}`topics-architecture` documents PEP 562 lazy root resolution on +{doc}`api/libvt`, showing which root attributes load {mod}`libvt.terminal` +versus grid, snapshot, and screen modules. It also covers cold `pytest11` +discovery for {mod}`libvt.pytest_plugin` and {mod}`libvt.contrib.syrupy`, +`asyncio` and `pty` loading in {mod}`libvt.aio`, and module boundaries across +{mod}`libvt.views`, {mod}`libvt.snapshot`, {mod}`libvt.format`, +{mod}`libvt.recorder`, and {mod}`libvt.contrib`. + #### Document working and committed assertion frames Clarify that {func}`~libvt.testing.assert_screen`, diff --git a/docs/topics/architecture.md b/docs/topics/architecture.md index b0b74919..55f8e164 100644 --- a/docs/topics/architecture.md +++ b/docs/topics/architecture.md @@ -4,21 +4,47 @@ {class}`~libvt.parser.Parser` ({mod}`libvt.parser`) recognizes text and control sequences. {class}`~libvt.screen.Screen` ({mod}`libvt.screen`) stores cells, -cursor position, and scrollback. {class}`~libvt.terminal.Terminal` -({mod}`libvt.terminal`) applies sequences across primary and alternate screens -and prepares replies and events. The adapters in {mod}`libvt.aio` connect -`Terminal` to `asyncio` streams and POSIX child processes. +cursor position, and scrollback, with read-only cell, row, and region views in +{mod}`libvt.views`. {mod}`libvt.snapshot` provides immutable +{class}`~libvt.snapshot.Snapshot` frames and incremental deltas +({class}`~libvt.snapshot.SyncToken` and {class}`~libvt.snapshot.ScreenDelta`). +{class}`~libvt.format.Formatter` ({mod}`libvt.format`) exports plain text, VT, +and HTML. {mod}`libvt.recorder` defines +{class}`~libvt.recorder.TerminalRecorder` and +{class}`~libvt.recorder.FlightRecorder`. {class}`~libvt.terminal.Terminal` +({mod}`libvt.terminal`) applies sequences across primary and alternate screens, +encodes input via {mod}`libvt.input`, and prepares replies and events. The +adapters in {mod}`libvt.aio` connect `Terminal` to `asyncio` streams and POSIX +child processes, and peer integrations live under {mod}`libvt.contrib`. {meth}`Terminal.feed() ` processes supplied bytes. The caller provides incoming data, retrieves queued replies with {meth}`~libvt.terminal.Terminal.drain_replies`, and decides when to read or write the transport (a sans-I/O boundary). -Importing `libvt` loads core emulation, input, and formatting modules while -keeping peer integrations under {mod}`libvt.contrib`. {mod}`libvt.aio`, -{mod}`libvt.testing`, and {mod}`libvt.pytest_plugin` are imported separately -when used, and POSIX `pty` / `termios` modules are loaded inside -{func}`~libvt.aio.open_pty`. +Top-level {doc}`../api/libvt` resolves root exports lazily through +[PEP 562](https://peps.python.org/pep-0562/) (`__getattr__`). Running +`import libvt` loads only package metadata (`__version__`) and leaves +emulation, screen, view, snapshot, formatting, recorder, input, and Unicode +table modules unloaded. First access to a root export loads and caches its +owning submodule. Reading `libvt.Terminal`, +{func}`libvt.render `, or +{func}`libvt.strip ` loads `libvt.terminal` and the +emulation engine. Reading `libvt.Snapshot` or `libvt.Screen` loads Unicode +tables (`libvt._ucd`), grid storage, views, snapshots, and formatting without +loading `libvt.terminal` or `libvt.parser`. `libvt.Snapshot` leaves +`libvt.screen` and `libvt.input` unloaded, while `libvt.Screen` loads both. +See {doc}`../project/public-api` for the root export list. + +Importing {mod}`libvt.pytest_plugin` or loading the `pytest11` entry points +leaves the terminal engine, Unicode tables, {mod}`libvt.testing`, `syrupy`, +and transport modules unloaded. Each fixture imports its modules inside the +fixture body when a test requests it. Import `libvt.aio`, `libvt.testing`, +and peer integrations under `libvt.contrib` separately when needed. Importing +`libvt.aio` loads `asyncio` and `libvt.terminal`, while POSIX `pty` and +`termios` load inside {func}`~libvt.aio.open_pty`. Under `libvt.contrib`, +{mod}`libvt.contrib.tmux` resolves its re-exports lazily, and +{mod}`libvt.contrib.syrupy` defers importing `syrupy` and `libvt.testing`. ## Configuration diff --git a/tests/test_libvt.py b/tests/test_libvt.py index e59a2ce1..db897395 100644 --- a/tests/test_libvt.py +++ b/tests/test_libvt.py @@ -79,42 +79,130 @@ def test_package_metadata_and_pep562_root_exports() -> None: def test_import_libvt_and_pytest_plugin_leave_subsystems_unloaded() -> None: - """Fresh ``import libvt`` and ``import libvt.pytest_plugin`` stay cold.""" + """Fresh ``import libvt`` and ``pytest11`` entry points stay cold.""" root_script = ( "import sys, libvt; " - "assert 'libvt.terminal' not in sys.modules, list(sys.modules); " - "assert 'libvt._ucd' not in sys.modules, list(sys.modules); " - "assert 'libvt.codec' not in sys.modules, list(sys.modules); " - "assert 'asyncio' not in sys.modules, list(sys.modules); " - "assert 'pty' not in sys.modules, list(sys.modules); " - "assert 'termios' not in sys.modules, list(sys.modules); " - "assert 'pytest' not in sys.modules, list(sys.modules)" + "loaded = {m for m in sys.modules if m == 'libvt' or m.startswith('libvt.')}; " + "assert loaded == {'libvt', 'libvt.__about__'}, sorted(loaded); " + "ext = {'asyncio', 'pty', 'termios', 'pytest', 'syrupy'} & sys.modules.keys(); " + "assert not ext, sorted(ext); " + "assert 'Terminal' not in vars(libvt); " + "term_cls = libvt.Terminal; " + "assert 'Terminal' in vars(libvt); " + "assert vars(libvt)['Terminal'] is term_cls; " + "req = {'libvt.terminal', 'libvt.parser', 'libvt.screen', " + "'libvt.format', 'libvt.input', 'libvt._ucd'}; " + "assert req <= sys.modules.keys(), sorted(req - sys.modules.keys()); " + "leaked = {'asyncio', 'pty', 'termios', 'pytest', 'syrupy'} " + "& sys.modules.keys(); " + "assert not leaked, sorted(leaked)" + ) + render_script = ( + "import sys, libvt; " + "assert 'render' not in vars(libvt); " + "assert 'libvt.terminal' not in sys.modules; " + "assert 'libvt.format' not in sys.modules; " + "fn = libvt.render; " + "assert vars(libvt)['render'] is fn; " + "assert {'libvt.terminal', 'libvt.format'} <= sys.modules.keys(); " + "leaked = {'asyncio', 'pty', 'termios', 'pytest', 'syrupy'} " + "& sys.modules.keys(); " + "assert not leaked, sorted(leaked)" + ) + strip_script = ( + "import sys, libvt; " + "assert 'strip' not in vars(libvt); " + "assert 'libvt.terminal' not in sys.modules; " + "assert 'libvt.format' not in sys.modules; " + "fn = libvt.strip; " + "assert vars(libvt)['strip'] is fn; " + "assert {'libvt.terminal', 'libvt.format'} <= sys.modules.keys(); " + "leaked = {'asyncio', 'pty', 'termios', 'pytest', 'syrupy'} " + "& sys.modules.keys(); " + "assert not leaked, sorted(leaked)" + ) + snapshot_script = ( + "import sys, libvt; " + "assert 'Snapshot' not in vars(libvt); " + "cls = libvt.Snapshot; " + "assert vars(libvt)['Snapshot'] is cls; " + "req = {'libvt.snapshot', 'libvt.format', 'libvt.views', " + "'libvt._grid', 'libvt._ucd'}; " + "assert req <= sys.modules.keys(), sorted(req - sys.modules.keys()); " + "forbidden = {'libvt.screen', 'libvt.input', 'libvt.terminal', " + "'libvt.parser', 'asyncio', 'pty', 'termios', 'pytest', 'syrupy'}; " + "leaked = forbidden & sys.modules.keys(); " + "assert not leaked, sorted(leaked)" + ) + screen_script = ( + "import sys, libvt; " + "assert 'Screen' not in vars(libvt); " + "cls = libvt.Screen; " + "assert vars(libvt)['Screen'] is cls; " + "req = {'libvt.screen', 'libvt.views', 'libvt.snapshot', " + "'libvt.format', 'libvt.input', 'libvt._grid', 'libvt._ucd'}; " + "assert req <= sys.modules.keys(), sorted(req - sys.modules.keys()); " + "forbidden = {'libvt.terminal', 'libvt.parser', 'asyncio', " + "'pty', 'termios', 'pytest', 'syrupy'}; " + "leaked = forbidden & sys.modules.keys(); " + "assert not leaked, sorted(leaked)" ) plugin_script = ( "import sys, libvt.pytest_plugin; " - "assert 'libvt.terminal' not in sys.modules, list(sys.modules); " - "assert 'libvt._ucd' not in sys.modules, list(sys.modules); " - "assert 'syrupy' not in sys.modules, list(sys.modules); " - "assert 'libvt.contrib.syrupy' not in sys.modules, list(sys.modules); " - "assert 'asyncio' not in sys.modules, list(sys.modules); " - "assert 'pty' not in sys.modules, list(sys.modules)" + "loaded = {m for m in sys.modules if m == 'libvt' or m.startswith('libvt.')}; " + "assert loaded == {'libvt', 'libvt.__about__', 'libvt.pytest_plugin'}, " + "sorted(loaded); " + "ext = {'asyncio', 'pty', 'termios', 'syrupy'} & sys.modules.keys(); " + "assert not ext, sorted(ext)" ) syrupy_contrib_script = ( "import sys, libvt.contrib.syrupy; " - "assert 'libvt.terminal' not in sys.modules, list(sys.modules); " - "assert 'libvt.testing' not in sys.modules, list(sys.modules); " - "assert 'libvt._ucd' not in sys.modules, list(sys.modules); " - "assert 'syrupy' not in sys.modules, list(sys.modules); " - "assert 'pytest' not in sys.modules, list(sys.modules)" + "loaded = {m for m in sys.modules if m == 'libvt' or m.startswith('libvt.')}; " + "assert loaded == {'libvt', 'libvt.__about__', " + "'libvt.contrib', 'libvt.contrib.syrupy'}, sorted(loaded); " + "ext = {'asyncio', 'pty', 'termios', 'pytest', 'syrupy'} & sys.modules.keys(); " + "assert not ext, sorted(ext)" + ) + pytest11_entry_points_script = ( + "import importlib.metadata, sys; " + "eps = importlib.metadata.distribution('libvt')" + ".entry_points.select(group='pytest11'); " + "assert sorted(ep.name for ep in eps) == ['libvt', 'libvt_syrupy'], " + "[ep.name for ep in eps]; " + "[ep.load() for ep in eps]; " + "loaded = {m for m in sys.modules if m == 'libvt' or m.startswith('libvt.')}; " + "assert loaded == {'libvt', 'libvt.__about__', 'libvt.pytest_plugin', " + "'libvt.contrib', 'libvt.contrib.syrupy'}, sorted(loaded); " + "forbidden = {'libvt.terminal', 'libvt.screen', 'libvt.parser', " + "'libvt._ucd', 'libvt.testing', 'asyncio', 'pty', 'termios', 'syrupy'}; " + "leaked = forbidden & sys.modules.keys(); " + "assert not leaked, sorted(leaked)" ) - for snippet in (root_script, plugin_script, syrupy_contrib_script): + aio_script = ( + "import sys, libvt.aio; " + "assert {'asyncio', 'libvt.terminal'} <= sys.modules.keys(); " + "forbidden = {'pty', 'termios', 'pytest', 'syrupy'}; " + "leaked = forbidden & sys.modules.keys(); " + "assert not leaked, sorted(leaked)" + ) + for label, snippet in ( + ("root", root_script), + ("render", render_script), + ("strip", strip_script), + ("snapshot", snapshot_script), + ("screen", screen_script), + ("pytest_plugin", plugin_script), + ("contrib.syrupy", syrupy_contrib_script), + ("pytest11_entry_points", pytest11_entry_points_script), + ("aio", aio_script), + ): proc = subprocess.run( (sys.executable, "-c", snippet), capture_output=True, text=True, check=False, ) - assert proc.returncode == 0, proc.stderr + assert proc.returncode == 0, f"{label}: {proc.stderr}" def test_top_level_render_strip_and_unicode_helpers() -> None: