Skip to content

feat(api): enforce public brainpy.state namespace; block direct brainpy_state import#86

Merged
chaoming0625 merged 1 commit into
mainfrom
feat/enforce-brainpy-state-namespace
Jun 16, 2026
Merged

feat(api): enforce public brainpy.state namespace; block direct brainpy_state import#86
chaoming0625 merged 1 commit into
mainfrom
feat/enforce-brainpy-state-namespace

Conversation

@chaoming0625

Copy link
Copy Markdown
Contributor

Summary

Make the public brainpy.state namespace the only supported entry point. A direct import brainpy_state (or from brainpy_state import ...) from user code now raises a clear ImportError pointing at brainpy.state. Internal source, tests, and doc builds are unaffected.

What changed

  • Guardbrainpy_state/_namespace.py::enforce_namespace_access() runs once at package-init top. It allows the import only when reached via the blessed brainpy.state shim (the normal user path, which import brainpy auto-loads), under pytest/sphinx, or with BRAINPY_STATE_ALLOW_DIRECT_IMPORT=1. Otherwise it raises ImportError with the fix.
  • Versionbrainpy.state.__version__ / __version_info__ are now exposed through __all__.
  • Examples / docs / docstrings — migrated to the public namespace (from brainpy.state import ... / from brainpy import state as ...). No example imports private brainpy_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).
  • conf.py reads the version via importlib.metadata rather than importing the package.
  • Drive-by: fixed a stale _plot import in _nest_spatial/plot_test.py left over from the _plot.pyplot.py rename.

Verification

  • New 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).
  • Full suite: 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.

…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.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chaoming0625 chaoming0625 merged commit 1c57937 into main Jun 16, 2026
5 checks passed
@github-actions

Copy link
Copy Markdown

🎉 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:

  • ✅ Your code follows our style guidelines
  • ✅ All tests pass
  • ✅ Documentation is updated (if applicable)
  • ✅ You've filled out the PR template completely

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! 🌟

@github-actions github-actions Bot added documentation Improvements or additions to documentation tests examples labels Jun 16, 2026
@chaoming0625 chaoming0625 deleted the feat/enforce-brainpy-state-namespace branch June 16, 2026 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation examples tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant