-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Separate private from public API: Underscore convention or __all__
#290
Comments
Thank you for thinking about this issue. To my naive eye, both solutions look sensible. Which of the two solutions would @stefanradev93 and @LarsKue prefer? |
@stefanradev93 @LarsKue Bumping this, as we are nearing a release and this is relevant for having good public API documentation. I think option a) would be cleanest, but would be a larger one-off effort to adjust all filenames and imports. After that, it would be cheap to maintain. I believe if we would want to make that change, now would be the best time to do so. I would offer to put some time in to make this happen. If we opt for a), it probably would be good to join forces to get it done in a day or two. What do you think? |
I will have to have a closer look at how we generate docs and which modules or classes might be considered clutter (and thus should be private). I will prioritize this. However, as a counter-argument, why should your example |
Thanks for taking a look. I did not mean that |
I don't think having the modules public is necessarily an issue, unless it clutters the docs. Users are still responsible for what they import, and it is unlikely that there will be significant confusion if we keep following naming conventions like CamelCase for classes. |
In my opinion it clutters the docs (you can take a look by checking out the |
This is related to the documentation issue #280, where we only want to render the public API documentation, but goes a bit further.
In many cases, our current setup indicates a desired import structure (e.g. importing
bayesflow.transforms.Transform
instead ofbayesflow.transforms.transform.Transform
. This is intuitive, but it is implicit and we cannot automatically (for generating the API docs) tell that the latter is not intended usage and should be private.As far as I can tell, there are two usual approaches:
a) prepending private modules and files with an underscore (e.g.,
bayesflow.transforms._transform.Transform
)b) adding public functions, classes and modules to
__all__
, while excluding all private onesI think for Sphinx to work efficiently, we probably need b), either everywhere where we import functions that we want to make publicly availiable (when
autosummary_imported_members = False
), or everywhere where we use external modules (keras, scikit-learn, ...) withautosummary_imported_members = True
.One problem I encountered when trying this out is Python's peculiar behavior with relative imports. When doing
we would expect that one variable is added to the namespace,
Transform
. But it turns out two objects are added:transform
andTransform
. The reason for this is explained in this thread.For us, this means that without explicitly specifying
__all__
or renamingtransform
to_transform
, both are detected as public, even though our intention was to only makeTransform
publicly available.We can do b) semi-automatically, by populating
__all__
by a function like the following:This would allow us to exclude unwanted relative imports (or, by modifying it a bit, only to add functions and classes, while manually specifying modules we want to add). If we also have a), we can exclude them automatically, as they start with an underscore.
Both solutions require a larger one-off effort, but have a comparatively low maintenance cost afterwards. As a) requires larger changes to the codebase, it would be beneficial to time the changes well to avoid large merge conflicts.
This issue is a bit technical, as both Python and Sphinx show somewhat peculiar behavior. If you have any questions, please send them so I can clarify if necessary. Also, if you have ideas for easier solutions, or think the effort is not worth the gains, please also share those considerations.
The text was updated successfully, but these errors were encountered: