archives

Algebraic ABCs - Extending "types" in Python

I've been experimenting with Python, seeing how best to extend the idea of "types" that it supports. Unfortunately, I suspect that people who work with typed languages will find this rather unimpressive (since it's not a type system) while people who don't, and use Python, won't see the point (and may be right).

Anyway, recent Python supports both Abstract Base Classes (ABCs, which identify collections of attributes and so can be seen as an attempt to reify "duck" types, in a largely unverified way) and function annotations (free-form metadata associated with function arguments).

Building on this, I added parametric ABCs. For example, to the existing Sequence I added Seq(X), which returns a new ABC that represents sequences of values of type X. I call these "type specifications" (calling them "types" would be misleading; "parametric algebraic ABCs" was unwieldy). They are metadata that are associated with data via type specifications, and also by inheritance and/or "registering" with ABCs.

The work also includes iteration over data+"type spec" with backtracking to help manage sum types (there's no type system, so sum types are handled at runtime by assuming the first possibility until something fails, then trying the next...).

You can find a paper describing the above at http://www.acooke.org/pytyp.pdf and an implementation, Pytyp, at http://www.acooke.org/pytyp (the latter includes runtime verification of parameter args, conversion between JSON and Python classes, and dynamic dispatch by type on method arguments other than "self").

I'm posting here in case anyone has any comments (does anything similar exist?). The work was done in isolation and hasn't generated any response from the Python community - my hope is that Python programmers see it as pointless (or impenetrable), but perhaps it's just wrong?