On Thu, Sep 24, 2020 at 10:47:21AM +0200, Sergio Fenoll wrote:
> Hi,
> 
> I was wondering if the following idea would be a useful addition to the
> Python language and if it could use a new PEP.
> I personally find myself often looking into the documentation &
> implementation of libraries I use to try and figure out what exceptions
> a function may raise.

I think that it's a truism that any function written in Python could 
raise any exception :-)

In practice, though, I think it is reasonable to say that functions will 
*typically* but not necessarily exclusively raise a certain set of 
exceptions.

What you seem to be describing is similar to Java's "Checked 
Exceptions", which are widely agreed to be an failure. Can you explain 
why this would be more successful?

The last thing I want to see is people being encouraged to write code 
like this:

    def demo(arg)->Something:
        # Raises ValueError
        try:
            processing...
        except ValueError:
            raise
        except:
            # Any other unexpected error.
            raise ValueError('something unexpected')


just to ensure that the declared exception is correct.

> In the same vein as adding type annotations to code, I think it'd be
> very useful to have exception "raises" annotations, i.e. a way to
> annotate what exceptions a function raises. Again, like type
> annotations, it shouldn't be mandatory nor actually be enforced at
> runtime. It would purely serve as a feature that IDEs can make use of.

How would an IDE make use of this?

> An example of how it may look:
> 
> def divide(numerator: float, denominator: float) raises [ZeroDivisionError] 
> -> float:
>     return numerator / denominator

If either argument is a subclass of float, this could raise any 
exception.


-- 
Steve
_______________________________________________
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/WKANCUIL2XTQXYRWP7SVINWB5ANT2ZOR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to