[Python-ideas] Proposal: Use mypy syntax for function annotations
Steven D'Aprano
steve at pearwood.info
Sun Aug 17 14:31:27 CEST 2014
On Sun, Aug 17, 2014 at 12:13:24PM +0200, willvarfar at gmail.com wrote:
> When discussed on reddit programming ("proggit" as its called), there were
> plenty of people saying they really didn't like the mypy syntax:
>
> http://www.reddit.com/r/programming/comments/2disob/proposal_for_python_type_annotations_from_guido/
>
> (Declaration: I am the author of obiwan https://pypi.python.org/pypi/obiwan
> and I got a lot of upvotes on that proggit thread when I promoted the
> obiwan style instead)
My thoughts on the obiwan style?
Quicker, easier, more seductive. Easily they flow. But once you start
down the dark path, forever will it dominate your destiny.
*wink*
Your example:
[mypy]
def word_count(input: List[str]) -> Dict[str, int]:
[obiwan]
def word_count(input: [str]) -> {str, int}:
Now consider that annotations are just expressions. Inside or outside of
a function parameter definition, Dict[str, int] is exactly the same
thing: it's an abstract type. I should be able to use it at runtime:
isinstance(x, word_count.__annotations__['return'])
But obiwan's style (like the old Jedi himself) twists the truth around.
The return annotation is only a type from a certain perspective, but in
reality it is an instance of a concrete class, not an abstract class.
It's not even a dict. {str, int} is a set, but obiwan uses it to
represent a dict. (Although maybe that's a typo, and you meant to write
{str: int}.)
--
Steven
More information about the Python-ideas
mailing list