I think Python has actually started to move in a direction similar to Go, as described. Protocols and types which were in typing
have been moved to io
and collections
to namespace and organize them better.
I would like to see intersection types and a protocol per dunder in a dedicated namespace. That will make it, as others have said in other threads, very easy to write aliases for useful intersections.
However, I also think that we need to find a good way to distribute and socialize the idea of writing protocols and canonically naming them in the SupportsX
style. I have taken to doing this in my own code, in imitation of typeshed and useful_types, since opening this thread and it has made a dramatic improvement to the legibility of my typed code. It’s been a huge revelation. Consider:
def build_result(errors: SupportsErrorIter, ctx: Context) -> Result:
r = Result()
for filename, err in errors.iter_errors(): ...
Can you guess what errors
is? Can you imagine how to write unit tests for this helper?
I may have a real object which looks very unlike SupportsErrorIter. Maybe it’s my FilesContainEmailDataValidator. But because the interface is declared, I can reason better about the code than if the type were not there at all.
Typeshed initially established the legibility of the SupportsX
naming scheme, and I am there for it. I’m sold. It allows two inferences for the human reader: (1) this object supports this interface and (2) this other thing is an interface!
Some will likely complain that this is “enterprise-y” or “Java-like” – both meant pejoratively – but the joy of Python still remains. Nobody will require you to write protocols, or to name them per the convention, but the tools will be there for you when and if you need them.
At the end of all that, a question: are folks receptive to and comfortable with the SupportsX
naming scheme being something documented as a stylistic convention or recommendation? In the typing RTD site or stdlib Protocol docs?
EDIT / PS: I now realize I’ve gone way OT. Oops / sorry. Just wrote down what I thought. I’ll let this post stand but can move to a new thread if it seems appropriate.