Reach the whole library through one namespace - #239
Open
henrikjacobsenfys wants to merge 1 commit into
Open
Conversation
Review feedback: the import style was inconsistent enough that a reader
had to scroll back to the imports cell to find out where a name came
from. Surveying it, the tutorials used four styles, and the last two
existed only because there was no other way to reach those names:
import easydynamics as edyn 32 uses
import easydynamics.sample_model as sm 151 uses
from easydynamics.convolution import Convolution forced
from easydynamics.utils.utils import hbar forced
easydynamics.__all__ held six names, so Analysis1d, Convolution,
detailed_balance_factor and hbar could only be had by importing the
module that defines them. The inconsistency was structural rather than
careless, and no amount of tidying the notebooks alone would have fixed
it.
Everything public is now re-exported from easydynamics, 37 names, so
`import easydynamics as edyn` reaches all of it. The sub-packages stay
importable and the internal layout is untouched: only the front door is
flat. Flat is comfortable at this size, there were no name collisions,
and the sample_model grouping was already imprecise, holding
InstrumentModel, ResolutionModel and BackgroundModel.
The tutorials and the docstring examples that render into the API
reference now use that one style throughout. A test keeps the front door
in step with the sub-packages and the notebooks in step with the
convention, which is also written down in CONTRIBUTING.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## bayesian-analysis #239 +/- ##
==================================================
Coverage 99.10% 99.11%
==================================================
Files 56 56
Lines 4825 4856 +31
Branches 831 831
==================================================
+ Hits 4782 4813 +31
Misses 18 18
Partials 25 25
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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.
Addresses the review comment that the import style was inconsistent enough that a reader had to scroll back to the imports cell to find out where a name came from.
Stacked on #238 (which is stacked on #237). This targets
bayesian-analysis; I will retarget it as the stack lands.The inconsistency was structural, not careless
Surveying the tutorials turned up four styles, and the last two existed only because there was no other way to reach those names:
import easydynamics as edynimport easydynamics.sample_model as smfrom easydynamics.convolution import Convolutionfrom easydynamics.utils.utils import hbareasydynamics.__all__held six names. SoAnalysis1d,Convolution,detailed_balance_factorandhbarcould only be had by importing the module that defines them — the last of those reaching into an implementation module that is not even inutils.__all__:Tidying the notebooks alone could not have fixed this. The same four styles appeared in the docstring examples that render into the API reference.
What changed
One namespace. All 37 public names are re-exported from
easydynamics, soimport easydynamics as edynreaches everything. Sub-packages stay importable and the internal layout is untouched — only the front door is flat, the way numpy and scipp do it.Why flat rather than two namespaces: the whole public API is 32 names with zero collisions when flattened, which is small; two namespaces would still leave the reader asking "is it
edynorsm?"; and thesample_modelgrouping was already imprecise, holdingInstrumentModel,ResolutionModelandBackgroundModel, which are instrument things. A namespace that does not hold a clean rule is worse than none, because people trust it and get it wrong.Tutorials and docstring examples now use that one style throughout — 16 notebooks, ~183 call sites, 37 example blocks.
A test keeps it honest.
tests/unit/easydynamics/test_public_api.pychecks that every sub-package export is re-exported, that the re-exports are the same objects rather than copies, and that no notebook imports EasyDynamics another way. I verified both guards actually fail when the regression they exist for is introduced, rather than passing vacuously.The convention is written down in
CONTRIBUTING.md, including the note that inside the library you still import from the specific module — flat is only the public front door.Backwards compatibility
Purely additive. Nothing was removed or renamed, and
import easydynamics.sample_model as smkeeps working, so no user code breaks.Testing
pixi run fixandpixi run checkfully clean — includingnonpy-format-check, which was failing before this branch on a pre-existingCONTRIBUTING.mdformatting drift that editing the file has now cleared.Not included
Enforcement via a ruff
banned-apirule, which we agreed was overkill for now. The test covers the notebooks, which is where the drift actually happened.🤖 Generated with Claude Code