Skip to content

fix(brc105): lazy PaymentMiddleware import so core primitives work without Starlette#1

Merged
kryp2 merged 1 commit into
masterfrom
agent/brc105-lazy-starlette-import
May 30, 2026
Merged

fix(brc105): lazy PaymentMiddleware import so core primitives work without Starlette#1
kryp2 merged 1 commit into
masterfrom
agent/brc105-lazy-starlette-import

Conversation

@kryp2

@kryp2 kryp2 commented May 30, 2026

Copy link
Copy Markdown
Contributor

What

src/bsv_brc/brc105/__init__.py imported PaymentMiddleware unconditionally. Since PaymentMiddleware pulls in Starlette -- an optional extra (pip install "bsv-brc[starlette]") -- importing anything from bsv_brc.brc105, including the core 402 primitives (NonceManager, StaticPricing, BSVPayment, create_challenge, parse_challenge_headers, PaymentClient), raised ModuleNotFoundError: No module named 'starlette' when the extra wasn't installed.

Fix

Wrap the middleware import in try/except ModuleNotFoundError and bind a small placeholder that raises a clear, actionable ImportError if someone uses PaymentMiddleware without the extra. Core primitives now import fine without Starlette.

  • One file, no dependency changes, no pin bumps.
  • Behaviour with Starlette installed is unchanged.

Test result (local, Python 3.13)

Without Starlette installed:

  • Before: from bsv_brc.brc105 import NonceManager -> ModuleNotFoundError. With this fix it succeeds, and PaymentMiddleware() raises a clear ImportError pointing to the extra.
  • pytest: tests/test_brc105.py (imports via the package) now collects and passes. tests/test_brc105_middleware.py still errors at collection because it imports from starlette.applications import Starlette directly and lacks an importorskip guard -- see note below.

With Starlette installed (CI's .[starlette,dev]):

  • pytest -> 136 passed (unchanged).

Suggested follow-up (kept out of this PR to scope it to the single library fix): add pytest.importorskip("starlette") at the top of tests/test_brc105_middleware.py (or add starlette to the dev extra) so pip install -e '.[dev]' && pytest is fully green. Happy to fold that in.

🤖 Generated with Claude Code

…rk without Starlette

Starlette is an optional extra (pip install "bsv-brc[starlette]"), but the
brc105 package __init__ imported PaymentMiddleware unconditionally. That made
ANY import from bsv_brc.brc105 -- including core 402 primitives like
NonceManager, StaticPricing and BSVPayment -- raise
ModuleNotFoundError: No module named 'starlette' when the extra was not
installed.

This also broke the documented local dev flow: `pip install -e '.[dev]'`
(the dev extra does not include starlette) followed by `pytest` failed at
collection (exit 2) because the brc105 test module imports from the package.

Wrap the middleware import in try/except ModuleNotFoundError and substitute a
placeholder that raises a clear, actionable ImportError on use. No dependency
changes; behaviour with Starlette installed is unchanged.

Verified locally (Python 3.13):
- without starlette: `from bsv_brc.brc105 import NonceManager` now succeeds and
  `PaymentMiddleware()` raises a clear ImportError; pytest -> 123 passed,
  6 skipped (was: collection error / exit 2)
- with starlette installed: pytest -> 136 passed (unchanged)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@kryp2 kryp2 marked this pull request as ready for review May 30, 2026 22:37
@kryp2 kryp2 merged commit bb580ce into master May 30, 2026
4 checks passed
@kryp2 kryp2 deleted the agent/brc105-lazy-starlette-import branch May 30, 2026 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant