python-typeclasses icon indicating copy to clipboard operation
python-typeclasses copied to clipboard

Structural type hierarchy ?

Open asmodehn opened this issue 5 years ago • 2 comments

Having a look at #3 it seems the main benefit compared to functools.singledispatch is the ability to work on structural types.

Therefore I wonder if one can define a hierarchy of protocols via python-typeclasses and have them checked with mypy as well ?

These days I find myself wanting to use haskell concepts in my python code (that I want to keep for the prototyping/iterative development feel) and I guess I actually want to replace nominal typing in my code with structural types + strict dynamic typing, but I am not quite sure how to go about it.

I've been wanting for a while to attempt to implement typeclasses (metaclasses were my initial take), so I'm happy to find this, thanks.

asmodehn avatar Jan 18 '20 11:01 asmodehn

You mean a hierarchy of typeclasses, e.g. the way Monad refines Applicative refines Functor in Haskell? If not that, maybe an example can help me understand what you're looking for.

thejohnfreeman avatar Jan 18 '20 13:01 thejohnfreeman

Yes exactly that. I've been wanting to do that in Python for a while to define explicit interfaces/way to use my class instances, without having traditional class inheritance getting in the way. I've been looking for a kind of "de-facto standard" package for it without luck so far.

I believe it's called a trait in other imperative/object languages, but I haven't tried any 'traits' implementation in python just yet to know if it's actually the same or not... I know only of https://github.com/enthought/traits and https://github.com/ipython/traitlets

Thanks for any hints on that topic !

asmodehn avatar Jan 19 '20 17:01 asmodehn