News for hackers. Stuff that matters.
Re:Type checking in Python by eevee on Sunday May 18, 2014 @04:47PM (#7764357)
Even that trivial `print` annotation breaks duck-typing; the print function can actually accept anything that responds to `__str__`.

Similarly, is there any reason the example `add` function in the OP shouldn't be able to add strings, or lists?

I've seen a lot of these quick-runtime-type-checking libraries, but they always have the same problem as manual type-checking: they make the constraints much stricter than they need to be and prevent entire classes of useful behavior.

Re:Type checking in Python by acjohnson55 on Sunday May 18, 2014 @10:48PM (#7765448)
In some languages, like Scala, you can use structural type annotations to handle this situation.
Re:Type checking in Python by eevee on Sunday May 18, 2014 @10:54PM (#7765473)
Python has the abc module, but it would be a bit wordier than "arg=list". Maybe if the builtins were treated as the corresponding abcs.
Re:Type checking in Python by acjohnson55 on Monday May 19, 2014 @08:05AM (#7766995)
I think `abc` would be overkill for this. My understanding is that you might use that to add behavior to a bunch of classes that aren't related hiearchically. But for type safety, all you need to do is check for the single magic method.
Re:Type checking in Python by eevee on Tuesday May 20, 2014 @02:31PM (#7774291)
You can use it for that, but you can also use it to check for rough interfaces. (The classes are rigged to fool isinstance() via dark magic.) It's fairly common that you want more than one magic method, but don't care too much what the type is as long as it's sorta like a `list` — thus, MutableMapping.