-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Description
Describe your issue.
Introduced during the work to clarify private/public API #14360
As seen in issues like #17572 or #17771, the deprecation mechanism defines __all__
and use it to advise on imports.
e.g. MemoizeJac
was defined in scipy.optimize.optimize
, it was moved to scipy.optimize._optimize
so the warning is telling users that they are using a private import and should go over scipy.optimize
:
from scipy.optimize.optimize import MemoizeJac
# DeprecationWarning: Please use `MemoizeJac` from the `scipy.optimize` namespace, the `scipy.optimize.optimize` namespace is deprecated.
But the message is misleading as MemoizeJac
is not part of scipy.optimize
. There are a many occurrence like these.
I would suggest to adjust the message. As a first step we can use the same formulation as the other message Try looking in scipy.optimize instead
.
There is also another problem with the __all__
list. In this module for instance, they're unreachable elements: is_array_scalar,main
. This would cause a warning telling to look elsewhere, but users won't be able to find these even in the private module directly.
Some code to check all that:
import scipy
for module in scipy.optimize.optimize.__all__:
res = getattr(scipy.optimize._optimize, module, None)
if res is None:
print(f'Cannot import module: {module}')
res = getattr(scipy.optimize, module, None)
if res is None:
print(f"Module is not present in 'scipy.optimize':{module}")
Some bits of that could be used to have a more dynamic/correct message. I would not necessarily do that though.
In the end we've had some bug reports, but not much. I think it could be ok to move one step further with the whole deprecation process by adding a clear version number in the warning.
Right now it says SciPy 2.0 in the docstrings, not in the warning and this is either way uncertain as we have no concrete timeline for SciPy 2.0.
Reproducing Code Example
from scipy.optimize import sys
from scipy.optimize._optimize import is_array_scalar
Error message
ImportError: cannot import name 'sys' from 'scipy.optimize' (.../scipy/optimize/__init__.py)
ImportError: cannot import name 'is_array_scalar' from 'scipy.optimize._optimize' (.../scipy/optimize/_optimize.py)
SciPy/NumPy/Python version and system information
main