## 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?

## Comment viewing options

### Sounds a lot like "limited" in Dylan

The Dylan language (an object-oriented dynamic language - not unlike Python) has a built-in function limited that can be used to construct more specific types from existing types. Your Seq(X) example would be something like limited(<sequence>, of: <x>).

In Dylan the values returned by this function are types though not necessarily classes.

### Community of one.

As a static typing person that is now stuck with Python at work, I just wanted to say that I'm really interested in this sort of thing. I've looked around and haven't found anything like it for Python (though there are tools for other dynamic languages). Some related work:

PyLint (different from Logilab's pylint, which doesn't really do much type checking).

PySonar type inference for Python (for enabling good code navigation, I believe).

### ooo thanks for pysonar.

ooo thanks for pysonar. didn't know about that.