Situation: I have some custom Exception classes in my library, which is used by users in their code. For some reason, I want to rename one of the exceptions and deprecate the old one.
Keeping the old exception as an alias is not difficult:
class MyNewError(ValueError):
pass
MyOldError = MyNewError
But eventually I would like to remove the old error name for my library, and therefore I want users who use this custom exception in downstream code to be notified with a DeprecationWarning that this error will be removed.
But I want to raise the DeprecationWarning in the following usecase (say that my library containing the custom exceptions is called mypackage
):
# downstream user code
import mypackage
...
try:
....
except mypackage.MyOldError:
....
So I want to raise the warning when the user tries to catch the error, not only when the user would raise the error.
Is it possible to do this in some way? (as the user is not calling a function here in which I can raise a deprecation warning)