TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Using Mypy in Production

116 pointsby charliermarshalmost 3 years ago

20 comments

codekansasalmost 3 years ago
As someone who owns multiple label makers and organizes my utensils by type when I put them into the dishwasher I really like Mypy. Cool writeup, good points. I think a lot of people (ML scientist people especially) who haven&#x27;t been forced to use a type checker don&#x27;t realize the productivity benefits of having a type checker running on your whole codebase - they only see the annoying slow-down of having to do things a particular way and having the type checker bug you for inconspicuous errors.<p>Example: One pattern (which I don&#x27;t think a lot of people are familiar with?) that I started adopting recently is the use of `Literal` for type-checking strings. For example, instead of something like<p>(on closer reading I realized this was in the blog post as well, but I suspect maybe some ML people will have seen this specific case before)<p><pre><code> class ActivationType(enum.Enum): sigmoid = &quot;sigmoid&quot; tanh = &quot;tanh&quot; def get_activation(key: str | ActivationType) -&gt; nn.Module: key = ActivationType[key] if key == ActivationType.sigmoid: return nn.Sigmoid() if key == ActivationType.tanh: return nn.Tanh() raise KeyError(key) </code></pre> you can do something like this instead:<p><pre><code> from typing import Literal ActivationType = Literal[&quot;sigmoid&quot;, &quot;tanh&quot;] def get_activation(key: ActivationType) -&gt; nn.Module: if key == &quot;sigmoid&quot;: return nn.Sigmoid() if key == &quot;tanh&quot;: return nn.Tanh() raise KeyError(key) </code></pre> The advantage is that you can do something like<p><pre><code> act = get_activation(&quot;tahn&quot;) </code></pre> and Mypy will show an error for your typo (instead of having to run your code and eventually hit the `KeyError`). So if you&#x27;re just trying to quickly implement an idea, you don&#x27;t have to kill brain cells searching for typos.<p>Of course, doesn&#x27;t make a difference if your coworkers all use Vim and Emacs with no extensions...
评论 #32560862 未加载
评论 #32559894 未加载
评论 #32570780 未加载
评论 #32571230 未加载
评论 #32558732 未加载
kungfufrogalmost 3 years ago
I can&#x27;t articulate specifically why but using typing in Python just feels like so much pain compared to other languages that have &quot;opt-in&quot; nominal typing syntax (PHP et al.).<p>The workflow feels frustrating, the ecosystem seems diverse and has no clear &quot;blessed&quot; path, and I&#x27;m still confused about what is bundled by Python and what I need to pull in from an external source. I REALLY want to use mypy but by the time I&#x27;ve figured out how to pull it all together I probably could have finished the program I&#x27;m working on.<p>The relevant factor here might be the size of Python programs I typically work on, somewhere between a few hundred lines and a few thousand.<p>I&#x27;m glad other people are having success because hopefully that&#x27;ll smooth the pavement for the next time I circle around and try to add some meaningful types to my Python programs.
评论 #32558456 未加载
评论 #32562926 未加载
评论 #32558889 未加载
评论 #32562271 未加载
评论 #32559372 未加载
评论 #32604692 未加载
评论 #32559716 未加载
评论 #32558511 未加载
rmnclmntover 2 years ago
&gt; I typically show candidates a snippet that uses typing.Protocol as part of a broader technical discussion, and I can’t recall any candidates having seen that specific construct before<p>I think the `typing.Protocol` [1] (aka &quot;structural subtyping&quot; or &quot;static duck typing&quot;) does not get enough spotlight! This is one of the keys to migrate a very pythonic codebase to type hints, and allows to avoid infinite type hints shenanigans all over the place. Of course, MyPy supports this feature natively [2].<p>[1] <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;typing.html" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;typing.html</a> [2] <a href="https:&#x2F;&#x2F;mypy.readthedocs.io&#x2F;en&#x2F;stable&#x2F;protocols.html" rel="nofollow">https:&#x2F;&#x2F;mypy.readthedocs.io&#x2F;en&#x2F;stable&#x2F;protocols.html</a>
评论 #32561909 未加载
rzimmermanalmost 3 years ago
&gt; Mypy catches bugs<p>100% yes. It’s much better than the examples would lead you to believe. Mypy catches stuff like:<p><pre><code> def f(arg: Optional[Object]): arg.method() # type error if arg is not None: arg.method() # ok </code></pre> It’s half the goodness of what you’d get from a well-typed language like Haskell or Rust but with the ability to say “trust me on this” and disable type checks for a line or two. Honestly I wish go tooling checked for nil pointer use as well as mypy unwraps optional values. Every time I add type hints and use mypy I find bugs. I would never make the case that type hints are better than a strongly typed language (especially with pattern matching), but it’s a great balance when writing python.
评论 #32559578 未加载
tonyalmost 3 years ago
Painpoint with type annotations: not being able to reuse &quot;shapes&quot; of data, e.g. struct-like fields such as TypedDict, NamedTuple, dataclasses.dataclass, and soon *kwargs (PEP 692 [1]) via TypedDict.<p>Right now, there isn&#x27;t a way to load up a JSON &#x2F; YAML &#x2F; TOML into a dictionary, upcast it via a `TypedGuard`, and pass it into a TypedDict &#x2F; NamedTuple &#x2F; dataclass.<p>dataclasses.asdict() or dataclasses.astuple() return naïve &#x2F; untyped tuples and dicts. Also the factory functions will not work with TypedDict or NamedTuple, respectively, even if you duplicate the fields by hand [2].<p>Standard library doesn&#x27;t have runtime validation (e.g. pydantic [3]). If I make a typed NamedTuple&#x2F;TypedDict&#x2F;dataclass with `apples: int`, nothing is raised in runtime when a string is passed.<p>Other issues you may run into using mypy:<p>- pytest fixtures are hard. It&#x27;s repetitious needing to re-annotate them every test.<p>- Django is hard. PEP 681 [4] may not be a saving grace either [5]. Projects like django-stubs don&#x27;t give you completions, it&#x27;d be a dream to see reverse relations in django models.<p>- Some projects out there have very odd packaging and metaprogramming that make typing and completions impossible: faker, FactoryBoy.<p>[1] <a href="https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0692&#x2F;" rel="nofollow">https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0692&#x2F;</a> [2] <a href="https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;typeshed&#x2F;issues&#x2F;8580" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;typeshed&#x2F;issues&#x2F;8580</a> [3] <a href="https:&#x2F;&#x2F;github.com&#x2F;pydantic&#x2F;pydantic" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pydantic&#x2F;pydantic</a> [4] <a href="https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0681&#x2F;" rel="nofollow">https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0681&#x2F;</a> [5] <a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;pyright&#x2F;blob&#x2F;8a1932b&#x2F;specs&#x2F;dataclass_transforms.md#django" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;pyright&#x2F;blob&#x2F;8a1932b&#x2F;specs&#x2F;data...</a>
评论 #32559253 未加载
评论 #32559013 未加载
评论 #32563673 未加载
评论 #32562648 未加载
评论 #32559761 未加载
Mehdi2277almost 3 years ago
For a while I used both mypy and pyright for my team’s codebase. After about half a year I eventually dropped mypy . I think type checking is valuable just that most of errors mypy detected pyright also caught and using newer type features often led to mypy false positives. I had trouble justifying using both when I could require my teammates to install pyright. Advanced type features tend to run into more bugs and while both are well maintained, pyright’s maintenance is magical. I do not know any other open source library that fixes bugs as fast (most bugs are fixed in under a week). The main thing that eventually forced decision was a flaky (depends on cache) mypy crash using paramspecs half a year ago. At time paramspec support was still in progress and there’s a good chance that specific issue is fixed.<p>The main awkwardness of pyright is it’s node library and most python devs I work with don’t interact much with node. But my team has a bash script that installs all our dependencies including node as needed (nvm) which mostly works. One benefit is you can use pyright as an LSP and it works very convenient in vscode.<p>Edit: 3rd party library lacking types is probably biggest issue. As my codebase is mostly typed by itself I’ve started gradually writing type stubs for library apis we use. Only writing stubs for small percent of what we use helps but there’s still a ton to add given codebase was started without types.
评论 #32559050 未加载
评论 #32562292 未加载
tonyalmost 3 years ago
I am moving all my open source projects to `mypy --strict`. Here&#x27;s the diff of adding basic &#x2F; --strict mypy types:<p>libvcs: <a href="https:&#x2F;&#x2F;github.com&#x2F;vcs-python&#x2F;libvcs&#x2F;pull&#x2F;362&#x2F;files" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vcs-python&#x2F;libvcs&#x2F;pull&#x2F;362&#x2F;files</a>, <a href="https:&#x2F;&#x2F;github.com&#x2F;vcs-python&#x2F;libvcs&#x2F;pull&#x2F;390&#x2F;files" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vcs-python&#x2F;libvcs&#x2F;pull&#x2F;390&#x2F;files</a><p>libtmux: <a href="https:&#x2F;&#x2F;github.com&#x2F;tmux-python&#x2F;libtmux&#x2F;pull&#x2F;382&#x2F;files" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tmux-python&#x2F;libtmux&#x2F;pull&#x2F;382&#x2F;files</a>, <a href="https:&#x2F;&#x2F;github.com&#x2F;tmux-python&#x2F;libtmux&#x2F;pull&#x2F;383&#x2F;files" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tmux-python&#x2F;libtmux&#x2F;pull&#x2F;383&#x2F;files</a><p>unihan-etl: <a href="https:&#x2F;&#x2F;github.com&#x2F;cihai&#x2F;unihan-etl&#x2F;pull&#x2F;255&#x2F;files" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;cihai&#x2F;unihan-etl&#x2F;pull&#x2F;255&#x2F;files</a>, <a href="https:&#x2F;&#x2F;github.com&#x2F;cihai&#x2F;unihan-etl&#x2F;pull&#x2F;257&#x2F;files" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;cihai&#x2F;unihan-etl&#x2F;pull&#x2F;257&#x2F;files</a><p>Perks:<p>- code completions (through annotating)<p>- typings can be used downstream (since the above are all now typed python libraries)<p>- maintainability, bug finding + Easy to wire into CI and run locally<p>Longterm, unsure of the return on investment. I do promise to report back if I find it&#x27;s not worth the effort.
评论 #32559006 未加载
j4mieover 2 years ago
The more I think about it, the more I think that the controversy around static type annotations in Python boils down to this:<p><pre><code> Improved readability </code></pre> This is very subjective, and is particularly sensitive in a language like Python which (rightly) has such a strong historical emphasis on readability above almost anything else.<p>My personal opinion is that static type annotations are extremely <i>detrimental</i> to readability. They add jarring line noise that makes reading Python much less like reading English. Hitting a type annotation when reading Python forces my brain into little backtracking loops which hugely diminishes my ability to form a mental model of the code from a quick read.<p>I wonder if people who come to Python from other languages (that are already statically typed) are accustomed to the poor comprehension introduced by types, and so don&#x27;t experience this drawback.
评论 #32562529 未加载
评论 #32562335 未加载
评论 #32562763 未加载
评论 #32562967 未加载
评论 #32562366 未加载
miiiiiikeover 2 years ago
I&#x27;ve been using Python since 2008 and the type annotations are the thing that broke me. I don&#x27;t want to configure another tool on every project just to get (incomplete) type checking. Why do I have to pick a type checker? Just check my types.<p>`typing.Protocol` is a poorly designed `Interface`. `abc` is a band-aid over missing `abstract` class&#x2F;method syntax.<p>Things that should be part of the language are left to libraries. Just add interfaces, enums, and abstract classes&#x2F;methods to the language.<p>I&#x27;m helping a new developer learn Python and having to explain all of the hoops I&#x27;ve been jumping through for the past 15 years is embarrassing. Making things &quot;simpler&quot; is making things more complex.<p>My current project is going to be my last Python project. I&#x27;m tired of add-ons and hacks, I want a complete language.
评论 #32563374 未加载
CiTyBearover 2 years ago
Really like Mypy. I have coded many python micro service with different framework but my minimum core set is:<p><pre><code> - Black (formatting) - Isort (import order) - MyPy (typing) - Pylint (linting) </code></pre> Edit: s&#x2F;unit test&#x2F;linting
评论 #32561885 未加载
gabereiseralmost 3 years ago
Huge fan of Mypy but you lost me at LOC worship.<p>Why do we do this? The more LOC is somehow attributed to more features? That simply isn’t true. More LOC means two things. One, what you are trying to do with the language is pushing its limits. Or two, you don’t understand the domain.<p>Most large monorepo’s I have seen fall into the latter category while few reside in the former. Game engines, mature enterprise software, and a few others are large (probably OP’s codebase too), but we seem to revel in the fact that we have so much code to grok. Sometimes conciseness is better than cleverness.<p>Back to mypy. I’d throw in black as well. Combining black, flake8, mypy, on precommit has loads of advantages. It’s completely opinionated but I find it helps me write better Python code.
评论 #32559009 未加载
评论 #32558946 未加载
BiteCode_devover 2 years ago
Mypy is very useful on big projects, and does catch bugs regularly in my code.<p>The ergonomics improved a lot and it&#x27;s now usable, so the cost ratio&#x2F;benefit is worth it today.<p>But barely.<p>Even assuming you use the latest Python version (lots of project can&#x27;t), you still have to import tons of things you use all the time like Iterable, Self, Callable and so on.<p>Then you have to deal with with the poor Protocol solution for duck typing, aggressive defaults, mypy slowness (before 9.13 it&#x27;s terrible, after it&#x27;s just bad and mypyd is quickly mandatory) and a surprisingly high number of bugs (such frustrating time wasters). Add on that false positives, low support from some popular libs and incompatible type checker implementations, and you get a very much meh experience. Very far from the awesomeness on Python.<p>If you are unlucky and have to use anaconda, mypy dependencies make it extra fun to include.<p>Still, I&#x27;m glad it exists. It&#x27;s still very useful. But thank god hints are optional.
评论 #32563631 未加载
radusover 2 years ago
I really really wanted to like mypy but my experience with mypy and Django has been very poor - it is slow, type inference is not good and most of the errors are false positives. Perhaps I’m spoiled by Typescript or django-stubs is just not quite mature enough.
daniel_rhalmost 3 years ago
This article mentions the woes of circular imports. I thought MyPy let you work around that by doing if False: around your imports<p>eg a.py: import c<p><pre><code> if False: import b class X: def x(self): #type: () -&gt; b.Y from b import something_that_returns_y return something_that_returns_y(self) </code></pre> b.py:<p><pre><code> from a import X class Y: pass def something_that_returns_y(x : X) -&gt; Y: return Y() </code></pre> per <a href="https:&#x2F;&#x2F;github.com&#x2F;asottile&#x2F;flake8-typing-imports" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;asottile&#x2F;flake8-typing-imports</a>
评论 #32558867 未加载
评论 #32558763 未加载
Rekksualmost 3 years ago
As the article mentions, the biggest problem by far with using mypy and the Python static typing ecosystem generally is the lack of third party support, even for big or new projects.<p>Python&#x27;s benefits are as much about the libraries available as they are the language itself, and unfortunately it&#x27;s kind of lacking right now especially compared to e.g. TypeScript support in npm.<p>Waiting for it to get better only goes so far; there isn&#x27;t yet a cultural expectation around publishing typesheds for everything.
whoopdeepooalmost 3 years ago
Would be interesting to seem them try pyright on their codebase. IME pyright is faster, catches more potential bugs, and doesn&#x27;t require its own custom plugin system (which seems to be a major burden on other libraries).
learndeeplyover 2 years ago
&gt; My unsubstantiated guess is that this is one of the most comprehensively-typed Python codebases out there for its size.<p>Not important, but FAANG companies have several orders of magnitude more strictly-typed Python than this.
andelinkalmost 3 years ago
Mypy is so so helpful and I can’t imagine Python without it. (I mean, I can, I did, it just sucked.)
kmbfjrover 2 years ago
Think they’re using a monorepo?
ReflectedImagealmost 3 years ago
Basically, they screwed up by using a monorepo with Python and have decided to try and badly paper over it by using a type checker.<p>For reference, don&#x27;t do either of those things in Python.
评论 #32558492 未加载