[Python-ideas] Type Metadata (and related ideas)

andrew cooke andrew at acooke.org
Thu May 19 14:27:57 CEST 2011


Hi,

I just finished working on a project that plays around with ABCs and
function annotations.  The idea was to allow for more delcarative code
by adding tools to describe Pytohn data in more detail.

While I don't think the result is suitable for adding to Python (it's
way too big a change, and it's not yet proven), the process of making
something consistent involved working through a lot of ideas about
"types in Python" that I recorded at http://www.acooke.org/pytyp.pdf

Part of that paper (pages 8 and 9) describes some issues that caused
particular problems, including:

* The lack of annotations on type generators makes it hard to use
  annotations as a way of completely describing types.

* There seems to be a missing ABC for __getitem__ (which unites lists,
  dicts and tuples).

* As ever, mutability is complicated :o) If we had copy on write lists
  (which already exist), perhaps we could hash instances efficiently?
  (OK, this may be already discussed, but I had to mention it)

* Given duck typing, shouldn't AttributeError be a TypeError (or vice
  versa?)

* For this particular use-case, an __instancehook__ (which would work
  much like __subclasshook__) for ABCMeta would have been useful (as
  described in the paper, polymorphism in Python occurs at the
  instance level, so asking about the types of instances makes a
  surprising amount of sense, if done right).

Anyway, apologies if some or all of this is old news or inapprorpiate.
I just thought people here might find it interesting (you can do
things like type check functions and use dynamic dispatch by type -
all in a fairly pythonic way... (imho))

Cheers,
Andrew

PS The project home and more docs are at http://www.acooke.org/pytyp/
; the code is at http://code.google.com/p/pytyp/ ; pypi page is
http://pypi.python.org/pypi/pytyp



More information about the Python-ideas mailing list