<div dir="ltr"><div><div>If we're looking for precedents in other languages, the Dialyzer project for Erlang is worth a look. It does completely optional static type checking without affecting the runtime at all.<br><br>

</div>It was pretty widely adopted and it works well in my experience. I think it's a bit different from what's being proposed here (it does type inference as well, for instance), but it is a more or less successful example of adding optional static checks to a dynamically typed language.<br>

<br></div>Based on my experiences with Dialyzer and the way I've used function annotations in Python to date, I'm really excited about this proposal.<br><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">

On Thu, Aug 14, 2014 at 3:11 PM, Dennis Brakhane <span dir="ltr"><<a href="mailto:brakhane@googlemail.com" target="_blank">brakhane@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Am 14.08.2014 22:16, schrieb Sunjay Varma:<br>
<div class="">> One of the many benefits of Python is that you can use objects with<br>
> equivalent interfaces in functions that may not have expected that<br>
> type while they were being written.<br>
</div>Exactly. As others have already noted, Guido's example is actually<br>
restricting code from calling it with a file object for no particular<br>
reason.<br>
<br>
Another thing is that it restricts the entries of the iterables to be<br>
str, while it actually only requires them to provide a split method.<br>
<br>
For example, and I'm aware that this is a contrieved case, let's assume<br>
word_count would actually be somehow extremely optimized code that<br>
I don't want to rewrite.<br>
<br>
I now want to count all prime factors in a list of integers.<br>
<br>
Without annotations I *could* do something like this ("Language for<br>
consenting adults"):<br>
<br>
  class IntWrapper(int):<br>
      def split(self):<br>
          return primefactors(self)<br>
<br>
  word_count(IntWrapper(i) for i in my_integer_list)<br>
<br>
I don't want to argue whether that's good code or not (it probably<br>
isn't), it would be possible to write such code and it will work correctly.<br>
<br>
For mypy to accept such code, the type declaration of word_count would<br>
have to be something like<br>
<br>
  def word_count(input: Iterable[has("split() -> T")]) -> Dict[T, int]<br>
<br>
Which would require a very complex type system.<br>
<br>
If I understand MyPy correctly, the above would be possible, but it<br>
would have to look something like<br>
<br>
  cast(Dict[int, int], word_count(cast(List[str], (IntWrapper(i) for i<br>
in my_integer_list)))<br>
<br>
One could argue that this ugly piece of code is the rightful punishment<br>
for abusing duck typing, but I'm not convinced<br>
you should be forced to make the code unreadable (and therefore refactor<br>
your code and reimplement word_count yourself).<br>
<div class=""><br>
<br>
> We have to be careful with this. If we do accept it (or any of the<br>
> many alternatives suggested so far), then we should choose a few use<br>
> cases and focus on solving them as best as possible.<br>
</div>Yes, my fear is that we will either end up with a broken type system<br>
like Java (which provides more problems than it solves) or end up with a<br>
hugely complex type system and type hierachy like Scala<br>
(see <a href="http://www.scala-lang.org/api/2.11.2/#scala.collection.MapLike" target="_blank">http://www.scala-lang.org/api/2.11.2/#scala.collection.MapLike</a> for<br>
a quick example what you end up with)<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">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></div></blockquote></div><br></div>