From 7a469d4871e19c1f0841e54a8e9820ee77e984fd Mon Sep 17 00:00:00 2001 From: dodoshell <326978213+dodoshell@users.noreply.github.com> Date: Mon, 2 Nov 2026 19:27:21 +0100 Subject: [PATCH 1/2] imports_test keeps btclib free of bip32_descriptors imports (closes #2887) `tests/imports_test.py` checked that btclib imports nothing of `btclib_wallet`, but not of `bip32_descriptors`, which depends on btclib as well. `_is_btclib_or_wallet` now matches `bip32_descriptors` names too, and the slab check counts the package among its roots, so a planted `import bip32_descriptors.bip32` in `src/btclib` fails `test_the_application_slab_has_no_inbound_edge`. The scan test plants imports of both packages and is renamed `reads_every_name`, since it reads more than two names now. Signed-off-by: dodoshell <326978213+dodoshell@users.noreply.github.com> --- tests/imports_test.py | 48 ++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/tests/imports_test.py b/tests/imports_test.py index 09c8826fd..bb1ae2e1b 100644 --- a/tests/imports_test.py +++ b/tests/imports_test.py @@ -399,9 +399,10 @@ def test_hashes_stays_stdlib_light() -> None: # Row 5 of issue #2129, `btclib-wallet`, in that table's order, with # `mnemonic/`, which went with `bip85`: the units that left this package -# for `btclib_wallet`. That package imports btclib rather than being -# imported by it, so the check on it is not what it reaches but that -# nothing here reaches back -- under its old name or its new one. +# for `btclib_wallet`. `bip32/` and `descriptors/` have since moved on to +# `bip32_descriptors`. Those packages import btclib rather than being +# imported by it, so the check on them is not what they reach but that +# nothing here reaches back -- under the old names or the new ones. _APPLICATION_SLAB = ( "bip32", "bip44", @@ -431,6 +432,10 @@ def test_hashes_stays_stdlib_light() -> None: # the import package row 5 is published as _WALLET_PACKAGE = "btclib_wallet" +# the import package `bip32/` and `descriptors/` moved to +# (btclib-org/.github#1811) +_DESCRIPTORS_PACKAGE = "bip32_descriptors" + def _is_btclib(name: str) -> bool: """Answer whether a dotted name is this package's, the bindings excluded. @@ -444,16 +449,16 @@ def _is_btclib(name: str) -> bool: def _is_btclib_or_wallet(name: str) -> bool: - """Answer whether a dotted name is this package's or `btclib_wallet`'s.""" + """Answer whether a dotted name is this package's or the slab's.""" return ( _is_btclib(name) - or name == _WALLET_PACKAGE - or name.startswith(f"{_WALLET_PACKAGE}.") + or name in (_WALLET_PACKAGE, _DESCRIPTORS_PACKAGE) + or name.startswith((f"{_WALLET_PACKAGE}.", f"{_DESCRIPTORS_PACKAGE}.")) ) def _btclib_names_imported_by(path: Path) -> set[str]: - """Return every dotted btclib or btclib_wallet name one file's imports name. + """Return every dotted btclib or slab name one file's imports name. `from btclib import wallet` and `from btclib.wallet import Wallet` name the same package two different ways; joining the `from` module with @@ -511,17 +516,18 @@ def test_the_application_slab_check_finds_a_planted_edge() -> None: assert edges == {"outside.py": ["btclib.wallet.wallet"]} -def test_the_application_slab_scan_reads_both_names(tmp_path: Path) -> None: - """`_btclib_names_imported_by` collects the slab under either name. +def test_the_application_slab_scan_reads_every_name(tmp_path: Path) -> None: + """`_btclib_names_imported_by` collects the slab under any of its names. - The tree has no import of `btclib_wallet`, so without a planted file - nothing would show that the scan below reads one at all: a collector - keyed on `btclib` alone would answer the same empty set for a module - importing the package above it. + The tree has no import of `btclib_wallet` or `bip32_descriptors`, so + without a planted file nothing would show that the scan below reads + one at all: a collector keyed on `btclib` alone would answer the same + empty set for a module importing a package above it. """ planted = tmp_path / "planted.py" planted.write_text( "import btclib_wallet.bip32\nfrom btclib_wallet import psbt\n" + "import bip32_descriptors.bip32\nfrom bip32_descriptors import descriptors\n" "from btclib_secp256k1 import dsa\n", encoding="utf-8", ) @@ -529,6 +535,9 @@ def test_the_application_slab_scan_reads_both_names(tmp_path: Path) -> None: "btclib_wallet.bip32", "btclib_wallet", "btclib_wallet.psbt", + "bip32_descriptors.bip32", + "bip32_descriptors", + "bip32_descriptors.descriptors", } @@ -543,14 +552,14 @@ def test_the_application_slab_has_no_inbound_edge() -> None: installed package the same way `module_names` does, from `btclib.__path__`, so this checks the tree the suite runs against rather than a copy on disk that might be stale. Every file is scanned, - for both spellings of the slab: `btclib.`, which no longer - resolves, and `btclib_wallet`, which depends on this package and so - cannot be depended on by it. + for every spelling of the slab: `btclib.`, which no longer + resolves, and `btclib_wallet` and `bip32_descriptors`, which depend on + this package and so cannot be depended on by it. The slab's units are asserted absent from the tree first. A unit that - came back, as a copy or as a module re-exporting `btclib_wallet`'s, is - what rule 3 refuses, and a scan that walked it would be reading the - slab from inside rather than the protocol side. + came back, as a copy or as a module re-exporting `btclib_wallet`'s or + `bip32_descriptors`'s, is what rule 3 refuses, and a scan that walked + it would be reading the slab from inside rather than the protocol side. """ root = Path(btclib.__path__[0]) for name in _APPLICATION_SLAB: @@ -558,6 +567,7 @@ def test_the_application_slab_has_no_inbound_edge() -> None: assert not (root / name).exists(), name slab_roots = ( _WALLET_PACKAGE, + _DESCRIPTORS_PACKAGE, *(f"btclib.{name}" for name in _APPLICATION_SLAB), ) imports_by_file = { From 7c6002b5a30b4cd397edfa09ce85edae84574c1b Mon Sep 17 00:00:00 2001 From: dodoshell <326978213+dodoshell@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:44:32 +0100 Subject: [PATCH 2/2] ARCHITECTURE, ASSURANCE_CASE and imports_test name bip32_descriptors beside btclib_wallet (issue #2887) Signed-off-by: dodoshell <326978213+dodoshell@users.noreply.github.com> --- ARCHITECTURE.md | 10 ++++++---- ASSURANCE_CASE.md | 3 ++- tests/imports_test.py | 14 ++++++-------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 45fed3f79..157a1e2a3 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -50,9 +50,10 @@ btclib calls the bindings only through ecc. Roughly bottom-up. The directions that hold without exception are the substrate, `btclib.ecc` without `btclib.ecc.bms`, the pairs in the next -section and the edge to `btclib_wallet` below, each held by a test; -elsewhere a module reaches up where its subject asks for it, as -`btclib.ecc.bms` does for the address a message signature names. +section and the edges to `bip32_descriptors` and `btclib_wallet` below, +each held by a test; elsewhere a module reaches up where its subject +asks for it, as `btclib.ecc.bms` does for the address a message +signature names. - **the substrate**: `alias`, `exceptions`, `utils` - **the curve and what is built on it**: ecc's `curves`, @@ -134,7 +135,8 @@ sends nothing itself. - **BIP32 key derivation and output descriptors** are the `bip32-descriptors` distribution, imported as `bip32_descriptors`. It - imports btclib. + imports btclib, and nothing in btclib imports it: + `tests/imports_test.py` holds that edge to one direction. - **The rest of the wallet side** (mnemonics, PSBTs, signers, chain backends and the Electrum protocol's codec) is the `btclib-wallet` distribution, imported as `btclib_wallet`. It imports btclib, and diff --git a/ASSURANCE_CASE.md b/ASSURANCE_CASE.md index d6c07ecc3..3804ca495 100644 --- a/ASSURANCE_CASE.md +++ b/ASSURANCE_CASE.md @@ -202,7 +202,8 @@ describes beside them. - **Layering.** The curve arithmetic does not import the schemes built on it, which ecc's `tests/imports_test.py` holds. The codecs do not import the bitcoin semantics on top of them, and nothing in btclib - imports `btclib_wallet`: btclib's `tests/imports_test.py` holds both. + imports `bip32_descriptors` or `btclib_wallet`: btclib's + `tests/imports_test.py` holds both. **Constant time where it is claimed.** It is claimed only of libsecp256k1: CONTRIBUTING.md's `_var` section sets out the tiers of diff --git a/tests/imports_test.py b/tests/imports_test.py index bb1ae2e1b..1bf9eb231 100644 --- a/tests/imports_test.py +++ b/tests/imports_test.py @@ -399,8 +399,8 @@ def test_hashes_stays_stdlib_light() -> None: # Row 5 of issue #2129, `btclib-wallet`, in that table's order, with # `mnemonic/`, which went with `bip85`: the units that left this package -# for `btclib_wallet`. `bip32/` and `descriptors/` have since moved on to -# `bip32_descriptors`. Those packages import btclib rather than being +# for `btclib_wallet`. `bip32_descriptors` publishes `bip32/` and +# `descriptors/`. Those packages import btclib rather than being # imported by it, so the check on them is not what they reach but that # nothing here reaches back -- under the old names or the new ones. _APPLICATION_SLAB = ( @@ -432,7 +432,7 @@ def test_hashes_stays_stdlib_light() -> None: # the import package row 5 is published as _WALLET_PACKAGE = "btclib_wallet" -# the import package `bip32/` and `descriptors/` moved to +# the import package that publishes `bip32/` and `descriptors/` # (btclib-org/.github#1811) _DESCRIPTORS_PACKAGE = "bip32_descriptors" @@ -448,7 +448,7 @@ def _is_btclib(name: str) -> bool: return name == "btclib" or name.startswith("btclib.") -def _is_btclib_or_wallet(name: str) -> bool: +def _is_btclib_or_slab(name: str) -> bool: """Answer whether a dotted name is this package's or the slab's.""" return ( _is_btclib(name) @@ -470,11 +470,9 @@ def _btclib_names_imported_by(path: Path) -> set[str]: for node in ast.walk(tree): if isinstance(node, ast.Import): names.update( - alias.name for alias in node.names if _is_btclib_or_wallet(alias.name) + alias.name for alias in node.names if _is_btclib_or_slab(alias.name) ) - elif isinstance(node, ast.ImportFrom) and _is_btclib_or_wallet( - node.module or "" - ): + elif isinstance(node, ast.ImportFrom) and _is_btclib_or_slab(node.module or ""): module = node.module or "" names.add(module) names.update(f"{module}.{alias.name}" for alias in node.names)