Hacker News new | past | comments | ask | show | jobs | submit login

> numpy will very often return either a float or an array depending on the input operations, keyword arguments, etc

That makes it harder to find good types, but it doesn't rule it out.

Either you can tell what the shape of the output should be without calling the function in question or you can't.

If you can't, you should be checking the shape of the output after the call, and the type of the output should be some kind of union so the type system encourages this.

On the other hand, if you can look at the arguments and easily know how the output should look, there's a fair chance you can push this knowledge into the type system. I've done similar things in flow. As a recent example, the download function in the google storage API returns a promise of the file's contents, unless the argument contains a 'destination' key in which case it returns a promise of void.

Describing that as the intersection of the two function types got me the checking I want. Amusingly, I don't think there's a way to write a function with this type without a cast through any, but that's not a problem for describing an untyped API.

All of that said, I don't know how well this works out with numpy and mypy in particular.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: