[issue32158] Suppress (and other contextlib context managers) should work as decorators (where appropriate)

Jason R. Coombs report at bugs.python.org
Tue Nov 28 14:58:59 EST 2017


Jason R. Coombs <jaraco at jaraco.com> added the comment:

Fair enough.

For an example, here's the case where I wanted to use the decorator to avoid excess indentation and keep the most meaningful part of the function at the base of the body:

@suppress(KeyError)
def v12_to_13(manager, case):
    case['sample_id'] = case.pop('caseid')


In my opinion, it's nominally nicer and clearer than:

def v12_to_13(manager, case):
    with suppress(KeyError):
        case['sample_id'] = case.pop('caseid')


But I see your points about encouraging overly-broad catching of exceptions... so it's better to have the indentation as something of a wart to dissuade excess wrapping.

----------
resolution:  -> rejected
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32158>
_______________________________________


More information about the Python-bugs-list mailing list