Hi,

I'd like to suggest an idea, that builds on PEPs 3134 and 409.

This idea came up when discussing a problem on the django-developers
mailing list:
https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/django-developers/ibEOt3A9c2M/EP4gbQyTFwAJ

I'll first explain my idea, and then the original problem.

The idea is to add `raise as` syntax, that raises an exception while
setting the currently caught exception to be the cause. It'll look like
this:

    try:
        1/0
    except ZeroDivisionError:
        raise as ValueError('Whatever')

What it does is a shorter version of this:

    try:
        1/0
    except ZeroDivisionError as error:
        raise ValueError('Whatever') from error

Of course, this syntax could only be used inside an except clause,
similarly to a blank `raise`.

The `raise as` syntax was one of the rejected syntaxes in PEP 409, for a
different use-case, of suppressing context:
https://www.python.org/dev/peps/pep-0409/#alternatives

I think that this syntax might be a good fit for this use-case.

The reason I propose this, is that I had a hard time convincing the Django
maintainers to use the current `raise foo from bar` syntax in cases where
it's appropriate, i.e. when they wrap an exception with another exception.
This is important because then the users see the correct message between
the two tracebacks, which is "The above exception was the direct cause of
the following exception:" rather than "During handling of the above
exception, another exception occurred" which is for other cases.

This is my first PR in this topic that was merged:
https://github.com/django/django/pull/12263

Even though that first PR was merged, after we discussed adding this all
over Django, and recommending that `raise foo from bar` be used in the
style guide, Carlton Gibson changed his mind. He gave his arguments in the
Django-developers thread I linked to at the top.

That's sad for me, because if Django doesn't accept the new syntax, and is
okay with the inaccurate "During handling of" message between exceptions,
chances are low that there will be widespread adoption of the current
`raise foo from bar` syntax.

It's possible that introducing the simpler `raise as` would increase
adoption and make users pay attention to the message between exception
tracebacks.

What do you think?


Thanks,
Ram.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KM7NRNFZHALOBKJUXVYQL2SLDP3MAANW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to