
On Thu, Aug 14, 2014 at 4:27 AM, Terry Reedy <tjreedy@udel.edu> wrote:
My main concern with static typing is that it tends to be anti-duck-typing, while I consider duck-typing to be a major *feature* of Python. The example in the page above is "def fib(n: int):". Fib should get an count (non-negative integer) value, but it need not be an int, and 'half' the ints do not qualify. Reading the tutorial, I could not tell if it supports numbers.Number (which should approximate the domain from above.)
This is a good point. But I think that static typing and duck typing are not mutually exclusive. TypeScript does this very nicely by defining structural interfaces (http://www.typescriptlang.org/Handbook#interfaces). With them is possible to define a given behaviour and any object capable of providing that behaviour is accepted, without having to be part of any specific type hierarchy or having to explicitly register as implementation of certain specification. That's basically what duck typing means. For example: interface Named { name: string; say(): string; } function doSomething(x: Named) { console.log(x.name); } doSomething({name: "hello", say: function() { return this.name }}); // OK doSomething({something: "hello"}); // ERROR I think something like this is a must have for mypy. In Python, I've been playing with something similar (https://github.com/ceronman/typeannotations) but for runtime checking only:
class Person(Interface): ... name = str ... age = int ... def say_hello(name: str) -> str: ... pass
Any object defining those the name, age and say_hello() members is a valid implementation of that interface. For example:
class Developer: ... def __init__(self, name, age): ... self.name = name ... self.age = age ... def say_hello(self, name: str) -> str: ... return 'hello ' + name ... isinstance(Developer('bill', 20), Person) True
Are there any plans for adding something like this to mypy? Manuel.