@@ -22,15 +22,16 @@ expected really gives you a much better idea of what is going on and what you
2222can do and can't do.
2323
2424But the key goal is: static type checking! There are a collection of static type
25- checkers, the most "official" and famous of which is MyPy. You can think of this
26- as the "compiler" for compiled languages like C++; it checks to make sure you
27- are not lying about the types. For example, passing in anything that is not an
28- int to ` f ` will fail a mypy check , _ before you run or deploy any code_ .
25+ checkers to choose from. You can think of a type checker as the "compiler" for
26+ compiled languages like C++; it checks to make sure you are not lying about the
27+ types. For example, passing in anything that is not an int to ` f ` will fail the
28+ type checker , _ before you run or deploy any code_ .
2929
30- Your tests cannot test every possible branch, every line of code. MyPy can
31- (though it doesn't by default, due to gradual typing). You may have code that
32- runs rarely, that requires remote resources, that is slow, etc. All those can be
33- checked by MyPy. It also keeps you (too?) truthful in your types.
30+ Your tests cannot test every possible branch, every line of code. A type
31+ checker can (though it doesn't by default, due to gradual typing). You may have
32+ code that runs rarely, that requires remote resources, that is slow, etc. All
33+ those can be checked by the type checker. It also keeps you (too?) truthful in
34+ your types.
3435
3536### Adding types
3637
@@ -45,20 +46,22 @@ There are three ways to add types.
4546 libraries you don't control this way.
4647
4748If you have a library you don't control, you can add "type stubs" for it, then
48- give MyPy your stubs directory. MyPy will pull the types from your stubs. If you
49- are writing code for a Raspberry Pi, for example, you could add the stubs for
49+ give the type checker your stubs directory. It will pull the types from your
50+ stubs. If you are writing code for a Raspberry Pi, for example, you could add
51+ the stubs for
5052the Pi libraries, and then validate your code, without ever even installing the
5153Pi-only libraries!
5254
5355You do not have to add types for every object - most of the time, you just need
54- it for parameters and returns from functions. When running MyPy, you can use
55- ` reveal_type(...) ` to show the inferred type of any object, which is like a
56- print statement but at type-checking time, or ` reveal_locals() ` to see all local
57- types.
56+ it for parameters and returns from functions. When running the type checker, you
57+ can use ` reveal_type(...) ` to show the inferred type of any object, which is
58+ like a print statement but at type-checking time, or ` reveal_locals() ` to see
59+ all local types.
5860
5961### Configuration
6062
61- By default, MyPy does as little as possible, so that you can add it iteratively
63+ By default, the type checker does as little as possible, so that you can add it
64+ iteratively
6265to a code base. By default:
6366
6467- All untyped variables and return values will be ` Any ` .
@@ -74,8 +77,8 @@ suggestions.
7477
7578For a library to support typing, it has to a) add types using any of the three
7679methods, and b) add a ` py.typed ` empty file to indicate that it's okay to look
77- for types inside it. MyPy also looks in ` typeshed ` , which is a library full of
78- type hints for (mostly) the standard library.
80+ for types inside it. The type checker also looks in ` typeshed ` , which is a
81+ library full of type hints for (mostly) the standard library.
7982
8083Third party libraries that are typed sometimes forget this last step, by the
8184way!
@@ -114,8 +117,8 @@ This will print `A` because you removed B via the type narrowing using the
114117
115118### Protocols
116119
117- One of the best features of MyPy is support for structural subtyping via
118- Protocols - formalized duck-typing, basically. This allows cross library
120+ One of the best features of type checkers is support for structural subtyping
121+ via Protocols - formalized duck-typing, basically. This allows cross library
119122interoperability, unlike traditional inheritance. Here’s how it works:
120123
121124``` python
@@ -173,8 +176,8 @@ Static typing has some great features worth checking out:
173176- Literals
174177- TypedDict
175178- Nicer NamedTuple definition (very popular in Python 3 code)
176- - MyPy validates with the Python version you ask for, regardless of what version
177- you are actually running.
179+ - The type checker validates with the Python version you ask for, regardless of
180+ what version you are actually running.
178181
179182## Complete example
180183
0 commit comments