[Python-ideas] Optional static typing -- the crossroads

Andrew Barnert abarnert at yahoo.com
Sun Aug 17 00:30:03 CEST 2014


On Aug 16, 2014, at 13:48, Ryan Gonzalez <rymg19 at gmail.com> wrote:

> Maybe that point of interest could be solved by using some kind of type class/interface(in the Obj C/Java sense). That way, external types that the user has no control of can be added to the interface.

We already have that. ABCs are enough like Java interfaces, ObjC mandatory protocols, C++ (non-auto) concepts, etc. to do everything we need here (except genericity, which it seems like everyone agrees with adding) if you want to do it nominatively. You just need to write a Date ABC. Or argue that there should be a datetime.abc library in the stdlib that does it for you. And that's simple.

And if you want to do it structurally, like Go protocols, C++ auto concepts, ObjC optional protocols, etc., ABCs can also do that. It's not _quite_ as simple today, but it's not hard, and there are a half dozen libraries that make it easy (I wrote one in a couple hours, and didn't bother publishing it because a quick search turned up so many pre-existing alternatives, and at least three classes in the stdlib that just did it manually without help...). Of course the existing implementations don't give you a way to statically declare the types of method arguments, attribute/properties, etc., but MyPy.Protocol does, and that can easily be adopted into the stdlib as part of this proposal. (In fact, I think it's already on the list.)

So, for Ethan's case, the last argument is just "payment_date: datetime.abc.Date", except that nobody has added that to the stdlib yet, so instead he has to write it himself and use it.


More information about the Python-ideas mailing list