This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Type union for except
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, Henry Schreiner, JelleZijlstra, gvanrossum, iritkatriel, steven.daprano
Priority: normal Keywords:

Created on 2022-03-09 17:49 by Henry Schreiner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg414807 - (view) Author: Henry Schreiner (Henry Schreiner) * Date: 2022-03-09 17:49
In 3.10 via PEP 604, there was an attempt to use the new union of types where runtime types were previously expected to be a tuple. `isinstance(x, (A, B))` can be written `isinstance(x, A | B)`. Unfortunately, there still is a case were a tuple of types is required: `except (A, B) as err:` cannot be written `except A | B as err:`. I think this should be allowed; it is consistent with isinstance and pattern matching's use of |, and nicely avoids confusion with `except A, B:` which is disallowed for Python 2 reasons; `except A | B`: would be valid.
msg414811 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-03-09 19:50
This would be nice but I'm not sure it's worth the hassle in terms of documentation, tooling support, etc.

There is an existing issue (that I can't find right now) that makes the `except` machinery use `__instancecheck__`, instead of looking only at real base classes like it does now. That change would automatically also make `|` work.
msg414818 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-03-09 22:43
I don't think that `except A|B` looks better than `except (A, B)`, so I am against this proposal. Exception matching is its own special thing (e.g. it doesn't honor virtual subclasses) and we shouldn't hyper-generalize.
msg415083 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-03-13 19:12
> I don't think that `except A|B` looks better than `except (A, B)`

I agree.
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91123
2022-03-13 19:12:42iritkatrielsetstatus: open -> closed
resolution: rejected
messages: + msg415083

stage: resolved
2022-03-09 22:43:09gvanrossumsetmessages: + msg414818
2022-03-09 20:35:36steven.dapranosetnosy: + steven.daprano
2022-03-09 19:50:28JelleZijlstrasetmessages: + msg414811
2022-03-09 19:13:00AlexWaygoodsetnosy: + gvanrossum, JelleZijlstra, iritkatriel, AlexWaygood
2022-03-09 17:49:02Henry Schreinercreate