fix(brc105): lazy PaymentMiddleware import so core primitives work without Starlette#1
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
src/bsv_brc/brc105/__init__.pyimportedPaymentMiddlewareunconditionally. SincePaymentMiddlewarepulls in Starlette -- an optional extra (pip install "bsv-brc[starlette]") -- importing anything frombsv_brc.brc105, including the core 402 primitives (NonceManager,StaticPricing,BSVPayment,create_challenge,parse_challenge_headers,PaymentClient), raisedModuleNotFoundError: No module named 'starlette'when the extra wasn't installed.Fix
Wrap the middleware import in
try/except ModuleNotFoundErrorand bind a small placeholder that raises a clear, actionableImportErrorif someone usesPaymentMiddlewarewithout the extra. Core primitives now import fine without Starlette.Test result (local, Python 3.13)
Without Starlette installed:
from bsv_brc.brc105 import NonceManager->ModuleNotFoundError. With this fix it succeeds, andPaymentMiddleware()raises a clearImportErrorpointing to the extra.pytest:tests/test_brc105.py(imports via the package) now collects and passes.tests/test_brc105_middleware.pystill errors at collection because it importsfrom starlette.applications import Starlettedirectly and lacks animportorskipguard -- see note below.With Starlette installed (CI's
.[starlette,dev]):pytest-> 136 passed (unchanged).🤖 Generated with Claude Code