3

Is there a way to 'detect' what exceptions function/method raises? Examplifying:

def foo():
    print 'inside foo, next calling bar()'
    _bar()
    _baz()
    # lots of other methods calls which raise other legitimate exceptions

def _bar():
    raise my_exceptions.NotFound

def _baz():
    raise my_exceptions.BadRequest

so, supposing that foo is part of my API and I need to document it, is there a way to get all exceptions that can be raised from it?

Just to be clear I don't want to handle those exceptions, they are supposed to happen (when a resource is not found or the request is malformed for instance).

I'm thinking to create some tool that transform that sequence of code in something 'inline' like:

def foo():
    print 'inside foo, next calling bar()'
    # what _bar() does
    raise my_exceptions.NotFound
    # what _baz() does
    raise my_exceptions.BadRequest
    # lots of other methods calls which raise other legitimate exceptions

Is there anything that can help me detect that instead of navigate through each method call? (Which goes deep into several files.)

2
  • What about instead of this particular exact question you ask instead, if by inspection / discovery you can list all the exceptions that a method or function explicitly raises using a raise statement, and without attempting to descend / recurse into other methods or calls? Not exactly what you wished for but you would at least have a mapping from method names to possible exceptions, and that may be the main ones you're looking for? Not sure if that would be good enough without the call stack, but I imagine that might be an easier question to answer. Commented May 22, 2024 at 13:20

2 Answers 2

4

You can't reasonably do this with Python, for a few reasons:

1) The Python primitives don't document precisely what exceptions they can throw. The Python ethos is that anything can throw any exception at any time.

2) Python's dynamic nature makes it very difficult to statically analyze code at all, it's pretty much impossible to know what code "might" do.

3) All sorts of uninteresting exceptions would have to be in the list, for example, if you have self.foo, then it could raise AttributeError. It would take a very sophisticated analyzer to figure out that foo must exist.

Sign up to request clarification or add additional context in comments.

3 Comments

Got it... thanks! About your 3rd point: I don't want this kind of 'detection', what I want is just get all explicit raised exceptions, not the ones that the interpreter can raise like the AttributeError.
wow, just realized that you're Ned Batchelder, the guy behind coverage module right? What I was thinking to do was pretty much inspired by your module, but not that complex hehe... I agree that python dynamic nature makes impossible to do a analyzer that is 100% of times right, but it would be just to have general idea of what is raised instead of read method by method what is being raised.
:) glad to have you on the team! :)
3

No, because of the dynamic nature of Python. How would your tool work if a function took another function chosen at runtime (very common), or if the code is later monkeypatched?

There's simply no way to know ahead of time (in enough situations for it to be useful), what the interpreter is going to do through static analysis. You effectively have to run the interpreter and see what happens, which of course could change between runs...

1 Comment

"You effectively have to run the interpreter and see what happens, which of course could change between runs…" But that's insane! I can't know in advance what might happen at some time given certain circumstances!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.