5

I wrote a library that sometimes raises exceptions. There is an exception that I want to deprecate, and I would like to advise people to stop catching them, and provide advises in the warning message. But how to make an exception emit a DeprecationWarning when catched?

library code

import warnings

class MyException(ValueError):
    ...
    warnings.warn(
        "MyException is deprecated and will soon be replaced by `ValueError`.",
        DeprecationWarning,
        stacklevel=2,
    )
    ...

def something():
    raise MyException()

user code

try:
    mylib.something()
except MyException: # <-- raise a DeprecationWarning here
    pass

How can I modify MyException to achieve this?

user2357112
  • 260,549
  • 28
  • 431
  • 505
azmeuk
  • 4,026
  • 3
  • 37
  • 64

2 Answers2

10

You can't. None of the logic that occurs in except MyException is customizable. Particularly, it completely ignores things like __instancecheck__ or __subclasscheck__, so you can't hook into the process of determining whether an exception matches an exception class.

The closest you can get is having the warning happen when a user tries to access your exception class with from yourmodule import MyException or yourmodule.MyException. You can do that with a module __getattr__:

class MyException(ValueError):
    ...

# access _MyException instead of MyException to avoid warning
# useful if other submodules of a package need to use this exception
# also use _MyException within this file - module __getattr__ won't apply.
_MyException = MyException
del MyException

def __getattr__(name):
    if name == 'MyException':
        # issue warning
        return _MyException
    raise AttributeError
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Won't work as written. `__getattr__` is only invoked if the name is not found by the normal means. – wim Sep 14 '20 at 22:05
  • @wim: Oh, whoops - I meant to have a `del MyException` there, but I forgot to write it. Fixed now. – user2357112 Sep 14 '20 at 22:06
  • 2
    +1 This is the correct way, and deprecating names is actually the first [rationale given in the PEP](https://www.python.org/dev/peps/pep-0562/#rationale) for the module `__getattr__` feature. – wim Sep 14 '20 at 22:13
-1

Try using this:

import warnings


class MyOtherException(Exception):
    pass


class MyException(MyOtherException):
    def __init__(self):
        warnings.warn(
            "MyException is deprecated and will soon be replaced by `MyOtherException`.",
            DeprecationWarning,
            stacklevel=2,
        )


if __name__ == "__main__":
    try:
        mylib.something()
    except Exception:
        raise MyException()


theNishant
  • 663
  • 5
  • 15
  • I can only edit the `library code` and not the `user code`, so I cannot assume anything going in the except block. No guarantee that the user will raise a `MyException` – azmeuk Sep 11 '20 at 12:44
  • This is missing the point. User code might catch `MyException` class without ever instantiating one. – wim Sep 14 '20 at 22:08