<div dir="ltr">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.)<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Aug 13, 2014 at 10:39 PM, Haoyi Li <span dir="ltr"><<a href="mailto:haoyi.sg@gmail.com" target="_blank">haoyi.sg@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="">> <span style="font-family:arial,sans-serif;font-size:13px">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.)</span><div>
<span style="font-family:arial,sans-serif;font-size:13px"><br></span></div></div><div><span style="font-family:arial,sans-serif;font-size:13px">To borrow Scala syntax, it would look something like</span></div><div><pre style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13px;margin-top:15px;margin-bottom:15px;border:1px solid rgb(221,221,221);line-height:19px;overflow:auto;padding:6px 10px;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-wrap:normal;color:rgb(51,51,51);background-color:rgb(248,248,248)">
<span style="font-weight:bold">def</span> <span>word_count</span><span style="font-weight:bold">[</span><span style="color:rgb(68,85,136);font-weight:bold">AnyStr</span> <span style="font-weight:bold"><:</span> <span style="color:rgb(68,85,136);font-weight:bold">String</span><span style="font-weight:bold">](</span><span>input</span><span style="font-weight:bold">:</span> <span style="color:rgb(68,85,136);font-weight:bold">Iterable</span><span style="font-weight:bold">[</span><span style="color:rgb(68,85,136);font-weight:bold">AnyStr</span><span style="font-weight:bold">])</span><span style="font-weight:bold">:</span> <span style="color:rgb(68,85,136);font-weight:bold">Dict</span><span style="font-weight:bold">[</span><span style="color:rgb(68,85,136);font-weight:bold">AnyStr</span>, <span style="color:rgb(68,85,136);font-weight:bold">Int</span><span style="font-weight:bold">]</span></pre>
</div><div>Where <b>word_count</b> is a generic function on the type <b>AnyStr</b>, which is not just *any* type but a type bound by the restriction it is a subclass of <b>String</b>. Thus you can force that the <b>AnyStr</b> going in and the <b>AnyStr</b> going out are the same one.</div>
<div><br></div><div>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</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
<div><div class="h5">
On Wed, Aug 13, 2014 at 10:24 PM, Guido van Rossum <span dir="ltr"><<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5">
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>On Wed, Aug 13, 2014 at 9:06 PM, Jukka Lehtosalo <span dir="ltr"><<a href="mailto:jlehtosalo@gmail.com" target="_blank">jlehtosalo@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br><div dir="ltr"><div class="gmail_extra">You could use AnyStr to make the example work with bytes as well:<br><br> def word_count(input: Iterable[AnyStr]) -> Dict[AnyStr, int]:<br>
result = {} #type: Dict[AnyStr, int]<div><br>
for line in input:<br>
for word in line.split():<br>
result[word] = result.get(word, 0) + 1<br>
return result<br>
<br></div></div><div class="gmail_extra">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.<span><font color="#888888"><br>
</font></span></div></div></blockquote><div><br></div></div><div>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.)<br>
</div></div><div><br>-- <br>--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)
</div></div></div>
<br></div></div><div class="">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br></div></blockquote></div><br></div>
</blockquote></div><br><br clear="all"><br>-- <br>--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)
</div>