Author ncoghlan
Recipients SilentGhost, barry, mb_, ncoghlan, rhettinger, yselivanov
Date 2016-08-20.17:00:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Michael, first of all, thanks for taking the time to propose this idea, and put together a patch for it.

Unfortunately, I'm going to have to decline the proposal for API design reasons (which I'll explain below), and instead point you towards which aims to fix a limitation in the normal exception handling that prevents custom __subclasscheck__ hooks from being usable in normal except clauses.

This is a more significant proposed change to the language than it first appears, as Python has never really had a concise fully standardised "catch these exceptions, but not these ones" syntax, with your options being limited to a preceding "except <excluded types>: raise" clause, or doing an isinstance() check on an already caught exception.

I'm also not sure it's an idiom we really want to encourage, as it tends to age poorly as new exception subclasses are added, as well as confusing exception flow logic like the example given in the documentation, which would be clearer with a single inline exception statement and a well-named flag variable:

    run_optional_code = True
    except PermissionError:
        run_optional_code = False
    except OSError:
    if run_optional_code:
        # Executed on success and on OSError
        # other than PermissionError
        # In a real example, "run_optional_code" would
        # be replaced by a name that actually conveyed
        # useful information to future readers

For folks that do need this capability, building it themselves is pretty straightforward, and if they go so far as to define a custom __instancecheck__ and __subclasscheck__ implementation, that will already work with contextlib.suppress(), but would need to land to work with ordinary except clauses.
Date User Action Args
2016-08-20 17:00:22ncoghlansetrecipients: + ncoghlan, barry, rhettinger, SilentGhost, yselivanov, mb_
2016-08-20 17:00:22ncoghlansetmessageid: <>
2016-08-20 17:00:22ncoghlanlinkissue27814 messages
2016-08-20 17:00:21ncoghlancreate