Hi Luciano, On 20/04/2021 11:35 pm, Luciano Ramalho wrote:
I am not taking sides now, but I want to share with you a useful diagram to reason about typing support in Python.
I struggled to explain what Python offers until I came up with this diagram:
https://standupdev.com/wiki/doku.php?id=python_protocols#the_typing_map
That's really nice, thanks.
The Typing Map has two orthogonal axis:
- when are types checked: -- runtime checking -- static checking
- how are type checked: -- structural types -- nominal types
The quadrants are informally labeled with the terms in ALL CAPS below.
Traditionally, mainstream languages supported one of two diagonally opposite quadrants: STATIC TYPING and DUCK TYPING.
Now the situation is more complicated.
- Java supports only STATIC TYPING: static checking of nominal types; Python started supporting nominal types with PEP 484
- Before ABCs, Python supported only DUCK TYPING: runtime checking of structural types;
- With ABCs, Python started supporting GOOSE TYPING (a term invented by Alex Martelli, in cc because I just quoted him): runtime checking of nominal types (with subclass hook which is a backdoor to support explicit checks on structural types as well);
- With PEP 544, Python started supporting STATIC DUCK TYPING: static checking of structural types;
There are languages that support multiple quadrants:
- TypeScript, like Python, supports all four quadrants.
- Go supports STATIC TYPING, but it also famously popularized STATIC DUCK TYPING, and even supports GOOSE TYPING with features like type assertions and type switches [1] designed for explicit runtime checking of nominal or structural types.
[1] https://tour.golang.org/methods/16
The Typing Map will be featured in my upcoming PyCon US talk [2]
[2] https://us.pycon.org/2021/schedule/presentation/80/
Cheers,
Luciano
PS. If you are aware of other languages that support more than one of these quadrants, please let me know!
On Tue, Apr 20, 2021 at 6:53 PM Eric Casteleijn <thisfred@gmail.com> wrote:
On Tue, Apr 20, 2021 at 10:03 AM Mark Shannon <mark@hotpy.org> wrote:
... PEP 544 supports structural typing, but to declare a structural type you must inherit from Protocol. That smells a lot like nominal typing to me.
Note that to implement a protocol you do not have to inherit from anything. You create a structural type that subclasses Protocol, but then any object that satisfies that protocol can be passed where that type is expected, without having to inherit anything, so I would argue that this really is structural typing.
-- - eric casteleijn (he/him) _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/R3VP4KOR... Code of Conduct: http://python.org/psf/codeofconduct/