[Python-ideas] Optional Static Typing -- the Python Way
Brett Cannon
brett at python.org
Thu Aug 21 16:30:03 CEST 2014
On Wed Aug 20 2014 at 5:58:31 PM Antoine Pitrou <antoine at python.org> wrote:
>
> Le 19/08/2014 20:12, Guido van Rossum a écrit :
> > Hmm, I've been saying this already, but my intuition is that it's a
> > bad idea to conflate *type descriptions* (what this proposal is
> > about) and actual *runtime types* (what ABCs are).
> >
> >
> > But are they? I think the registration mechanism makes it clear that
> > they aren't (necessarily) runtime types -- by linking a concrete type
> > with an ABC through registration you are pretty clearly stating that the
> > ABC *describes* (an aspect of) the concrete class without automatically
> > adding any behavior from the ABC to it.
>
> Hmm... well, they are usable at runtime (if only for isinstance calls
> :-)). I admit my wording was a bit vague here. But our type descriptions
> should be able to express more information than ABCs currently do. For
> example, I don't how you'd express the idea of a "mapping from str to
> int" using the current Mapping ABC, while retaining the runtime
> properties of the Mapping class.
>
Isn't that the magic of __instancecheck__/__subclasscheck__? If typing.py
does the right thing here then it shouldn't matter. You could use __init__
or __getitem__ to construct an object who upon introspection provides the
details you want and do what you expect, and then override the appropriate
methods so that isinstance() checks check against the ABC instead of the
type class itself (heck we could make the base type class require that you
inherit from an ABC to promote working with ABCs instead of concrete
classes).
I have to also say that I like using ABCs because `from collections import
abc as a` leads to reading parameters like "x is a.Mapping" which is
linguistically quaint. =)
P.S.: Off-topic for this email but something I don't think has been
mentioned more than once, type hinting like we are proposing is exactly
what Dart does and it is nice. Built-in API documentation of interfaces
along with IDE support at the API makes all of this worth it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140821/c4d5628d/attachment.html>
More information about the Python-ideas
mailing list