feat(api): enforce public brainpy.state namespace; block direct brainpy_state import#86
Merged
Merged
Conversation
…py_state import Users must reach the API through `brainpy.state`. A direct `import brainpy_state` (or `from brainpy_state import ...`) from user code now raises ImportError pointing at the public namespace. The guard lives in `brainpy_state/_namespace.py::enforce_namespace_access()`, runs once at package-init top, and exempts the blessed `brainpy.state` shim, `pytest`, `sphinx`, and the `BRAINPY_STATE_ALLOW_DIRECT_IMPORT=1` override — so internal source, tests, and doc builds keep importing `brainpy_state` directly. - Expose `brainpy.state.__version__` / `__version_info__` via `__all__`. - Migrate every example, doc, and public-API docstring to the public namespace (`from brainpy.state import ...` / `from brainpy import state as ...`). No example imports private `brainpy_state._*` submodules anymore: the Clopath and gap-junction examples inline the public-API builders/constants they previously borrowed from the internal validation harness, preserving exact numerical parity. - conf.py reads the version via importlib.metadata instead of importing the package. - Drive-by: fix a stale `_plot` import in `_nest_spatial/plot_test.py` left by the `_plot.py` -> `plot.py` rename.
There was a problem hiding this comment.
We failed to fetch pull request #86.
You can try again by commenting this pull request with @sourcery-ai review, or contact us for help.
|
🎉 Congratulations on opening your first pull request in brainpy.state! Thank you for your contribution! A maintainer will review your changes soon. Please make sure:
We appreciate your effort in making brainpy.state better. If you have any questions, feel free to ask in the comments. Welcome to the brainpy.state community! 🌟 |
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.
Summary
Make the public
brainpy.statenamespace the only supported entry point. A directimport brainpy_state(orfrom brainpy_state import ...) from user code now raises a clearImportErrorpointing atbrainpy.state. Internal source, tests, and doc builds are unaffected.What changed
brainpy_state/_namespace.py::enforce_namespace_access()runs once at package-init top. It allows the import only when reached via the blessedbrainpy.stateshim (the normal user path, whichimport brainpyauto-loads), underpytest/sphinx, or withBRAINPY_STATE_ALLOW_DIRECT_IMPORT=1. Otherwise it raisesImportErrorwith the fix.brainpy.state.__version__/__version_info__are now exposed through__all__.from brainpy.state import .../from brainpy import state as ...). No example imports privatebrainpy_state._*submodules: the Clopath and gap-junction examples now inline the public-API builders/constants they previously borrowed from the internal validation harness (verified to reproduce the prior results to the bit).importlib.metadatarather than importing the package._plotimport in_nest_spatial/plot_test.pyleft over from the_plot.py→plot.pyrename.Verification
brainpy_state/_namespace_test.py(16 tests): pure allow/deny matrix, enforcer raise + message, and real-subprocess E2E (external import blocked; public path + version + env override allowed).pytest brainpy_state/ -m "not requires_nest" -n auto→ all pass (the one timing-invariant test that flakes under parallel load passes in isolation).python -c "import brainpy_state"→ exits 1 with the guidance message;python -c "import brainpy.state"→ exits 0.