[Python-ideas] Proposal: Use mypy syntax for function annotations

Sunjay Varma varma.sunjay at gmail.com
Thu Aug 14 21:40:26 CEST 2014


See responses inline

On Aug 14, 2014 3:28 PM, "Dennis Brakhane" <brakhane at googlemail.com> wrote:

> 1. Annotations can be used to communicate additional restrictions on
> values that must be checked on run time
>
> Let's assume a simple Web service that is called via HTTP to register a
> user, and the underlying framework decodes
> the request and finally calls a simple controller function, it could
> look like this
>
> (Java code, @ signifies an annotation)
>
>   public Response register(@Range(18,100) int age, @ValidEmail String
> email) { ... }
>
> The framework would check the range of the age parameter and the
> validity of the email and if there are validation errors,
> refusing the request with a suitable error message without ever calling
> our function.
>

This is exactly the kind of syntax I want to avoid. Python should not
attempt to just blindly become like Java or any other language. Though
Dennis was just illustrating his point (this was not a suggestion of an
alternate syntax), I feel like Python is moving further and further towards
code like this. Python programmers should not be forcing as much as
possible into each line. Explicit is better than implicit.

> Even if we assume that mypy's type system will be incredibly complex and
> allowing to specify all kinds of restrictions on a type,
> it won't help because those checks have to be done at run time, and are
> not optional.

This is a great point. Regardless of the nature of the annotation, we can't
let this become a way out for lazy programmers. Having annotations is no
excuse for poor error checking.

> 2. They can give meta information to a framework
>
> An admittedly contrieved example, let's expand our register function:
>
> public Response register( int age, @Inject @Scope("session") UserService
> userService, @Authenticated User user) ....

This is too much to put on one line in Python. We should be aiming to make
code cleaner and promote proper error checking and documentation.

> The flexibility annotations give in Java makes programming much less
> frustrating. It also took quite some time before
> annotations were widly used (they were only available starting in Java
> 5) and people started finding more uses for them.
> I think this will also be true for Python, it will take time before
> people find useful ways for them. Redefining them now to
> be "not much more" than static type information feels wrong to me.

I agree that annotations can be useful, but I don't think they should be
used for type checking at this scale.

Sunjay _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140814/95f043eb/attachment.html>


More information about the Python-ideas mailing list