[Python-ideas] Proposal: Use mypy syntax for function annotations
Andrew Barnert
abarnert at yahoo.com
Thu Aug 14 05:08:12 CEST 2014
On Aug 13, 2014, at 18:56, Łukasz Langa <lukasz at langa.pl> wrote:
> On Aug 13, 2014, at 6:39 PM, Andrew Barnert <abarnert at yahoo.com.dmarc.invalid> wrote:
>
>> On Wednesday, August 13, 2014 12:45 PM, Guido van Rossum <guido at python.org> wrote:
>>
>>> def word_count(input: List[str]) -> Dict[str, int]:
>>> result = {} #type: Dict[str, int]
>>> for line in input:
>>> for word in line.split():
>>> result[word] = result.get(word, 0) + 1
>>> return result
>>
>> I just realized why this bothers me.
>>
>> This function really, really ought to be taking an Iterable[String]
>
> You do realize String also happens to be an Iterable[String], right?
Of course, but that's not a new problem, so I didn't want to bring it up. The fact that the static type checker couldn't reject word_count(f.read()) is annoying, but that's not the fault of the static type checking proposal.
> One of my big dreams about Python is that one day we'll drop support for strings being iterable. Nothing of value would be lost and that would enable us to use isinstance(x, Iterable) and more importantly isinstance(x, Sequence). Funny that this surfaces now, too.
IIRC, str doesn't implement Container, and therefore doesn't implement Sequence, because its __contains__ method is substring match instead of containment. So if you really want to treat sequences of strings separately from strings, you can. If only that really _were_ more important than Iterable, but I think the opposite is true.
But anyway, this is probably off topic, so I'll stop here.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140813/314f9871/attachment-0001.html>
More information about the Python-ideas
mailing list