Static Duck Typing in Python with Protocols
Basic as it is, it is worth reminding folks reading this and similar articles that Python, even with type annotations, <i>does not have a compile pass</i> and that IDEs like PyCharm tend to fail open when they can&#x27;t figure out how to type check something.<p>I see a lot of folks coming to Python from TypeScript and assuming that there&#x27;s any enforcement of type annotations a la tsc. There&#x27;s not, not unless you run a tool like MyPy on your code and configure it to be strict enough to provide the type assurance you need (what level of strictness is appropriate depends on the project and the programmers).<p>I also see folks getting lulled into a false sense of security by their IDE. PyCharm is great, and it highlights many typing errors in code as you write them. But it gives up <i>very</i> easily in the presence of any indirection at all (many basic decorators which don&#x27;t change function signatures, for example, are enough to cause it to stop trying to type check decorated functions at all). And when it gives up, there&#x27;s no indication of that; giving up looks the same as error-free code.<p>That&#x27;s not an indictment of any tools. Python is the way it is for good reason, and it&#x27;s type annotation system is good and getting better all the time (e.g. Protocols as discussed in this article). PyCharm is also super helpful and given how it performs it&#x27;s type checking it&#x27;s borderline miraculous that it works as well as it does in the first place.<p>Rather, this is a reminder, especially for new python programmers: don&#x27;t assume that type annotations give you anything (except documentation, which it&#x27;s on you to keep accurate) for free.