Skip to content
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

MAINT: a nicer way to detect a module is deprecated in DTModule #135

Merged
merged 1 commit into from
Mar 1, 2024

Conversation

ev-br
Copy link
Member

@ev-br ev-br commented Feb 29, 2024

The try-except-pass stanza was bothering me every time I saw it.

Turns out it was to filter out modules which are deprecated, meaning they emit a DeprecationWarning on an attribute access. Consider

In [1]: from scipy.constants import constants      # deprecated submodule constants.constants

In [2]: constants.zetta      # DeprecationWarning
<ipython-input-5-835b60441caf>:1: DeprecationWarning: Please import `zetta` from the `scipy.constants` namespace; the `scipy.constants.constants` namespace is deprecated and will be removed in SciPy 2.0.0.
  constants.zetta
Out[5]: 1e+21

Note however that non-existent attributes still raise an AttributeError, only with a helpful error message:

In [3]: constants.tapas                 # non-existent attr: AttributeError w/ special error message
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], line 1
----> 1 constants.tapas

File ~/repos/scipy/scipy/build-install/lib/python3.10/site-packages/scipy/constants/constants.py:51, in __getattr__(name)
     50 def __getattr__(name):
---> 51     return _sub_module_deprecation(sub_package="constants", module="constants",
     52                                    private_modules=["_constants"], all=__all__,
     53                                    attribute=name)

File ~/repos/scipy/scipy/build-install/lib/python3.10/site-packages/scipy/_lib/deprecation.py:42, in _sub_module_deprecation(sub_package, module, private_modules, all, attribute, correct_module)
     39     correct_import = f"scipy.{sub_package}"
     41 if attribute not in all:
---> 42     raise AttributeError(
     43         f"`scipy.{sub_package}.{module}` has no attribute `{attribute}`; "
     44         f"furthermore, `scipy.{sub_package}.{module}` is deprecated "
     45         f"and will be removed in SciPy 2.0.0."
     46     )
     48 attr = getattr(import_module(correct_import), attribute, None)
     50 if attr is not None:

AttributeError: `scipy.constants.constants` has no attribute `tapas`; furthermore, `scipy.constants.constants` is deprecated and will be removed in SciPy 2.0.0.

@ev-br ev-br merged commit 59a0649 into main Mar 1, 2024
4 checks passed
@ev-br ev-br deleted the detect_deprecated_modules branch March 1, 2024 12:07
@ev-br
Copy link
Member Author

ev-br commented Mar 1, 2024

Merged to test scipy/scipy#20127 on CI. A post-merge review would be valuable @Sheila-nk

@Sheila-nk
Copy link
Collaborator

LGTM! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants