I think it’s the function (or at least, the current usage of the function) that you want to deprecate, not necessarily the exception itself. You can modify the function to emit a warning when it gets called (not just when it raises, because it’s possible it will never actually raise before your deprecation period is over) to let users know that whatever exception they are expecting now may not be raised later.
def foo():
warnings.warn("Future versions will raise New, not Old", DeprecationWarning)
raise Old()
Code that current tries to catch Old can try to catch OldorNew in response to the deprecation warning:
try:
foo()
except (Old, New): # Expect Old now, New later
...
Once the deprecation period is over, code that calls foo and expected Old or New to be raised can simply stop trying to catch Old.
Thanks @chepner, I guess that’s the most sensible thing to do We could argue that a function (whether it’s completely or partially deprecated) might never be called either, and therefore could never emit any deprecation warnings, even though it’s there in a user code base. And therefore we should go all the way up and use static analysis to find and report deprecated use of a library. I think I’ll try that route in the tool I’m developing (Griffe)
I’d still love being able to emit deprecation warnings from an __exceptcheck__ method I’d love that Python better equips itself for dealing with APIs, public APIs, deprecated APIs.