On Wed, Aug 13, 2014 at 9:06 PM, Jukka Lehtosalo
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)