Hacker News new | ask | show | jobs
by tabbott 1709 days ago
I agree with all the benefits of mypy cited in this article. For me, most important thing for the long-term health of a codebase is its readability/maintainability, and mypy static typing makes such a huge difference for that in large Python codebases. I'm really excited to see large libraries doing this migration.

I'll add for folks thinking about this transition that we took a pretty different strategy for converting Zulip to be type-checked: https://blog.zulip.com/2016/10/13/static-types-in-python-oh-...

The post is from 2016 and thus a bit stale in terms of the names of mypy options and the like, but the incremental approach we took involved only using mypy's native exclude tooling, and might be useful for some projects thinking about doing this transition.

One particular convention that I think many other projects may find useful is how we do `type: ignore` in comments in the Zulip codebase, which is to have a second comment on the line explaining why we needed a `type: ignore`, like so:

* # type: ignore[type-var] # https://github.com/python/typeshed/issues/4234

* # type: ignore[attr-defined] # private member missing from stubs

* # type: ignore[assignment] # Apparent mypy bug with Optional[int] setter.

* # type: ignore[misc] # This is an undocumented internal API

We've find this to be a lot more readable than using the commit message to record why we needed a `type: ignore`, and in particular it makes the work of removing these with time feel a lot more manageable to have the information organized this way.

(And we can have a linter enforce that `type: ignore` always comes with such a comment).

1 comments

I really like this documented type ignore strategy and will start incorporating it in our codebase. Thanks for sharing.