5 ms·
For those who have never used type checking in Python, what's a good introduction?
by stereo 6y ago
For those who have never used type checking in Python, what's a good introduction?
- high_density 6y agoread mypy docs, but use google's pytype... pylance is much stricter, but it seems to have a lot of false-(+)s from what I've used, the implementation varies wildly...
- aodin 6y agoFor the actual type annotations themselves, the official docs are great: https://docs.python.org/3/library/typing.html https://docs.python.org/3/library/typing.html Know that the signatures vary with version, so select the correct one. To check these annotations, you'll need a third-party type checker somewhere in your build process. I use pylance with VS Code, as it can detect errors as I type: https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance https://marketplace.visualstudio.com/items?itemName=ms-pytho...
- tgb 6y agoThis doesn't seem like an ideal introduction. I don't think it actually covers type checking at all (besides the note at the top) since that's relegated to third parties.
- ThePadawan 6y agoTo learn how to use it, I would recommend setting up your IDE (I recommend VS Code) to use mypy. Then just open a small script you know well, let's say less than 200 lines, and check what "missing" type annotations mypy complains about. For most cases, the IDE will be able to tell you what it recommends, and from then you can start reading the docs for specific types for a deeper dive.
- dharbin 6y agoPylance is much faster and can index a large virtualenv without issue, IMO
- mshekow 6y agoYou may also find my introduction to Python type hints helpful, see https://www.augmentedmind.de/2020/10/11/static-python-type-hints/ https://www.augmentedmind.de/2020/10/11/static-python-type-h...
- Geminidog 6y agoSounds like you've never dealt with a type checking language in general. I recommend using a language that forces you to do type checking like haskell. Haskell is super hard so just use it until you see the light and understand why typing is better. I would say get good enough with haskell to write a 2 human player tic-tac-toe game by following some online tutorials. Then you'll be able to pick up types in python like it was nothing. Otherwise if you have used a type checked language... Python types are really straight forward. I never needed a tutorial. Whenever I needed a feature (such as type variables) I would look it up via google. Anyway, I don't actually know if you've ever played with a type checked language but I'm assuming you haven't because python type checking is pretty easy to pick up without a tutorial based resource if you have had prior experience in other languages.