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

Juancarlo Añez apalala at gmail.com
Thu Aug 14 23:03:13 CEST 2014


On Thu, Aug 14, 2014 at 2:22 PM, Steven D'Aprano <steve at pearwood.info>
wrote:

> def foo(x:float)->float:
>     return (x+1)/2
>
> and rely on mypy to check the types at compile time. Fred is happy: he
> has static type checks, Python does it automatically for him (once he
> has set up his build system to call mypy), and he is now convinced that
> foo() is type-safe and an isinstance check at run-time would be a waste
> of cycles.
>

The foo() kind of examples won't cut it.

The standard library and other important Python libraries will take an
argument of any of several "reasonable" but otherwise unrelated types...
and do the right thing. Such is duck-typing. Trying to find the "common
abstract type" to cast it in stone is a waste of time.

For example, see https://docs.python.org/3.3/library/json.html#json.dump,
or take a look at http://pyyaml.org/.

In fact, Why aren't we instead discussing the much more interesting topic
of *type inference*, as it's going on in projects like PyDev, PyCharm,
Rope, and others?

I would be all-in for an approach that helps make code-completion tools
more precise and more available, and that would enable linters to tell me
why a certain call will probably not work. It could be a milder approach,
with "type hinting" for when the tools can't infer the type, instead of one
of "type specification".

def set_color(self, color):
   ...

o.set_color("red")

It could well be that 'color' should actually be one of the constants in a
Color enum, and the call should fail statically... if it was Java. In the
Python way, the method will probably figure out the caller's intentions,
and do the right thing.

Cheers,

p.s. I just had an epiphany: This discussion is not about empowering Python
programmers, but again about finding "magic" to allow bad (read "cheap")
programmers to write good programs. I'm out!

-- 
Juancarlo *Añez*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140814/a9e834cd/attachment.html>


More information about the Python-ideas mailing list