Clarifying typing behavior for exception handlers (except/except*)

Welcome, and thanks for the detailed post!

I think the “parenthesized” part here is because except A, B: is a SyntaxError, to avoid confusion with the old Python 2 syntax (where you wrote except Exception, e: instead of except Exception as e:).

Possibly this wording can be improved, but I wouldn’t look to the tutorial for a fully precise specification of the behavior in rarely seen cases. You also link the wording in the language reference, and there the wording appears to be mostly correct.

I agree this is imprecise and should mention that tuples are allowed. Could you open an issue and make a PR to the language reference?

I don’t want the typing spec to duplicate the language reference; in cases like this, the implicit spec is that type checkers should follow the behavior specified in the language reference.

I think ideally type checkers should give an error if the except* clause contains a literal class or tuple of literal classes and one of them is a subclass of ExceptionGroup, yes.

Note your example uses a type alias, not a type variable.

I think disallowing your example would be more trouble than it’s worth. The type system has no way to express difference types.

2 Likes