question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Making clearer what namespaces are public by use of underscores

See original GitHub issue

When we add new .py files, we are careful to add underscores to the file names to ensure everyone sees at a glance that this is not a new public namespace. However, we have lots of files that are missing that underscore. This is only because of historical reasons; in the early days of SciPy no one paid attention to this. We had a discussion about it once, probably about 9-10 years ago, and decided to not clean things up back then because the concrete benefits were unclear.

The public namespaces are documented in http://scipy.github.io/devdocs/reference/index.html#api-definition. No one reads the docs though. This has become much more of a problem recently, because authors of other libraries have started to reimplement parts of the SciPy API, for example:

Those libraries can get it wrong by accident, or they just mirror what our actual filenames are even though we tell them it’s not meant to be a public namespace. An example of each:

Long story short: we should clean this up. There’s two ways we can go about this:

  1. don’t deprecate anything. Just move somefile.py to _somefile.py, and then add a new somefile.py which reimports the public functions from _somefile.py and adds a big comment with warnings about this not being a public namespace at the top of the file.
  2. do (1), and then deprecate the functions in somefile

I’d suggest to do (2) by default, and only make exceptions in case something is quite heavily used and adding deprecations would be too disruptive.

We should prioritize modules that are being duplicated in CuPy, JAX, PyTorch et al.

EDIT: a test should also be added that no new private-but-public-looking namespaces are added. The approach can be taken from https://github.com/numpy/numpy/blob/main/numpy/tests/test_public_api.py

EDIT 2: a basic API design principle is: a public object should only be available from one namespace. Having any function in two or more places is just extra technical debt, and with things like dispatching on an API or another library implementing a mirror API, the cost goes up.

>>> from scipy import ndimage
>>> ndimage.filters.gaussian_filter is ndimage.gaussian_filter  # :(
True

EDIT 3: now that it’s decided we’re going ahead with this, here is a tracker of which modules are done:

  • cluster
  • constants
  • fft
  • fftpack
  • integrate
  • interpolate
  • io
  • linalg
  • misc
  • ndimage
  • odr
  • optimize
  • signal
  • sparse
  • sparse.csgraph
  • sparse.linalg
  • spatial
  • special
  • stats

Also, we should take over the public API test from NumPy:

EDIT 4:

  • right before closing this issue, we should compare master with 1.7.0 and use some version of sorted([x for x in dir(fitpack) if not (x.startswith('_') or x in ('np', 'warnings'))] to compare for each file/namespace we gave an underscore that everything that looks like a regular SciPy function/object is present in the listings from which we raise deprecation warnings. That way, even if people used clearly private things that were not in __all__ in that file, they will still get the warnings rather than a hard break.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:39 (39 by maintainers)

github_iconTop GitHub Comments

9reactions
rgommerscommented, Nov 30, 2021

The last fixes went in in gh-15105, this is all done now so I’ll close the issue. Thanks @Smit-create, @czgdp1807, and everyone else who helped with this!

2reactions
rgommerscommented, Oct 12, 2021

I have updated the issue description with a status tracker. @Smit-create will start moving this forward, starting with odr, linalg, special and stats.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Name of namespaces (using _ underscore in names) - MSDN
When you create a namespace in C# and start the name with number, the VS IDE rename the namespace by putting underscore (_)...
Read more >
What are the rules about using an underscore in a C++ ...
Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace. 165. 165)...
Read more >
Python, namespace vs module with underscores
It is more beautiful to use module and namespace, but on pip, independent packages are saved in different location.
Read more >
The Meaning of Underscores in Python - dbader.org
Single underscores are a Python naming convention indicating a name is meant for internal use. It is generally not enforced by the Python...
Read more >
Use something other than a single underscore for hook ...
namespace Drupal\hooks\node { function menu() { /* body of node_menu ... @#19: The purpose of the change should be to make clearer when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found