3 ms·
The most underrated part of type annotations are so-called stub files [0], which allow you to keep your annotations separate from the actual code, thus reducing
by BerislavLopac 8y ago
The most underrated part of type annotations are so-called stub files [0], which allow you to keep your annotations separate from the actual code, thus reducing its size while still benefiting from the static analysis with mypy.
Unfortunately the IDE support for those files (.pyi extension) is quite limited -- I have even suggested to the PyCharm team to implement an option to automatically save all annotations to stub files, while still displaying them alongside the code in the editor.
[0] https://www.python.org/dev/peps/pep-0484/#stub-files https://www.python.org/dev/peps/pep-0484/#stub-files
- NelsonMinar 8y agoI'm sort of sad that Python has reinvented .h files.
- ASalazarMX 8y agoSame here. Fortunately those files are entirely optional and apparently seldomly used, judging by the limited IDE support after 5 years.
- DonaldPShimoda 8y agoWhoa, that's neat! I had no idea! Does PyCharm support these? Like if I write the stub files manually, will type hints and autocompletion work correctly in the main file? This could be a great way to reduce the amount of syntactic overhead, at least in function definitions and top-level code.
- BerislavLopac 8y agoAt the moment, you can click on a (say) class definition, and, if it has the corresponding entry in the pyi file, it will open it. But I would like it to be a bit more integrated. Another advantage is that you can use pyi files for static checking, but remove them when deploying the application.
- tlavoie 8y agoSounds sort of like the .mli files in Ocaml. As I understand it, the compiler uses (and enforces) the types found therein, though can infer a lot from the code in .ml files as well.