[Python-ideas] Proposal: Use mypy syntax for function annotations

Guido van Rossum guido at python.org
Thu Aug 14 07:44:13 CEST 2014


I'd be more interested in Jukka's specific proposal. (Note that in mypy,
AnyStr is the type that you call String here -- it's either bytes or str;
and it seems that you're introducing AnyStr here as a type variable -- mypy
conventionally uses T for this, but you have to define it first.)


On Wed, Aug 13, 2014 at 10:39 PM, Haoyi Li <haoyi.sg at gmail.com> wrote:

> > I heartily agree. But just for the type theorists amongst us, if I
> really wanted to write the most general type, how would I express that the
> AnyStr in the return type matches the one in the argument? (I think
> pytypedecl would use something like T <= AnyStr.)
>
> To borrow Scala syntax, it would look something like
>
>
> def word_count[AnyStr <: String](input: Iterable[AnyStr]): Dict[AnyStr, Int]
>
> Where *word_count* is a generic function on the type *AnyStr*, which is
> not just *any* type but a type bound by the restriction it is a subclass of
> *String*. Thus you can force that the *AnyStr* going in and the *AnyStr*
> going out are the same one.
>
> I'm not sure if mypy allows for type-bounds, but that's the way you
> achieve what you want if it does, or will, in future =P
>
>
>  On Wed, Aug 13, 2014 at 10:24 PM, Guido van Rossum <guido at python.org>
> wrote:
>
>>  On Wed, Aug 13, 2014 at 9:06 PM, Jukka Lehtosalo <jlehtosalo at gmail.com>
>> wrote:
>>>
>>>
>>> You could use AnyStr to make the example work with bytes as well:
>>>
>>>   def word_count(input: Iterable[AnyStr]) -> Dict[AnyStr, int]:
>>>       result = {}  #type: Dict[AnyStr, int]
>>>
>>>       for line in input:
>>>           for word in line.split():
>>>               result[word] = result.get(word, 0) + 1
>>>       return result
>>>
>>> Again, if this is just a simple utility function that you use once or
>>> twice, I see no reason to spend a lot of effort in coming up with the most
>>> general signature. Types are an abstraction and they can't express
>>> everything precisely -- there will always be a lot of cases where you can't
>>> express the most general type. However, I think that relatively simple
>>> types work well enough most of the time, and give the most bang for the
>>> buck.
>>>
>>
>> I heartily agree. But just for the type theorists amongst us, if I really
>> wanted to write the most general type, how would I express that the AnyStr
>> in the return type matches the one in the argument? (I think pytypedecl
>> would use something like T <= AnyStr.)
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>


-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140813/166402e5/attachment-0001.html>


More information about the Python-ideas mailing list