0

Given Python's dynamic nature, it's historically been really hard to work out what exception types could get raised from a given code path

Nowadays, language servers (e.g., VSCode’s python) can statically work out a ton of typing and other information from your code which was really hard in the past.

Is there a thing that will enumerate exceptions?

Even partial/best guess enumerations of potential exception types would be welcome.

zcutlip
  • 34
  • 3
  • 1
    Possibly, but unless you use an interpreter that enforces type hints, there's no guarantee that any errors you catch will be one of the "potential exception types". Also your question falls foul of the "don't seek recommendations for libraries etc" guideline: [ask] / [what's on topic](/help/on-topic) – Pranav Hosangadi Nov 07 '22 at 04:47
  • 2
    Probably not, given that the design of type hinting in Python specifically omitted this, see [issue comment](https://github.com/python/typing/issues/71) and [thread](https://stackoverflow.com/questions/44282268/python-type-hinting-with-exceptions) and [subsequent thread](https://stackoverflow.com/questions/57118995/python-typing-with-exception-handling) as a consequence of the decision. – metatoaster Nov 07 '22 at 04:50
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Nov 07 '22 at 06:39
  • Thanks for the input. I suspect this is possible, but I think the answer to my question is that no one knows of a thing that currently does this. Sorry, I don't know how to phrase the question differently; this is my literal question – zcutlip Nov 08 '22 at 06:27
  • @metatoaster I suspect it's possible, and the code comprehension that language servers are able to support go way beyond types, to things like "show all function references" and "show me the method declaration for arg_2.foo()". And they can do so without any type hinting at all (though they do benefit from type hints in the code) – zcutlip Nov 08 '22 at 06:33
  • Yes, that process will "simply" require analysis of _all_ of the code that could conceivably be called from that function to scan for whether or not that function has a `raise Exception` of some kind, except Python do support C based extensions or even better, Python modules written in some other language (e.g. Rust) that got compiled to native code that may not have the `.pyi` file available to make this process easy. At best this only paints a false and incomplete picture; designers of language servers tend to desire comprehensiveness and this makes it undesirable to be implemented. – metatoaster Nov 08 '22 at 22:48
  • Not to mention, language servers were originally designed for code that could be statically analyzed, hence why language servers like the one VSCode has require the type hinting system in place in order to report back what types the function arguments can support, and the lack of the same affordances for exceptions in Python makes it impossible to know if an exception that could be raised in some function down a stack will _actually_ be raised as the code at hand is actually safe from causing that exception? e.g. `[0][0]` could be marked to `raise IndexError` but it is actually safe. – metatoaster Nov 08 '22 at 23:25

0 Answers0