|
6 | 6 | Welcome to mypy documentation!
|
7 | 7 | ==============================
|
8 | 8 |
|
9 |
| -Mypy is a static type checker for Python 3. If you sprinkle |
10 |
| -your code with type annotations, mypy can type check your code and find common |
11 |
| -bugs. As mypy is a static analyzer, or a lint-like tool, the type |
12 |
| -annotations are just hints for mypy and don't interfere when running your program. |
13 |
| -You run your program with a standard Python interpreter, and the annotations |
14 |
| -are treated effectively as comments. |
15 |
| - |
16 |
| -Using the Python 3 annotation syntax (using :pep:`484` and :pep:`526` notation), |
17 |
| -you will be able to |
18 |
| -efficiently annotate your code and use mypy to check the code for common errors. |
19 |
| -Mypy has a powerful and easy-to-use type system with modern features such as |
20 |
| -type inference, generics, callable types, tuple types, union types, and |
21 |
| -structural subtyping. |
22 |
| - |
23 |
| -As a developer, you decide how to use mypy in your workflow. You can always |
24 |
| -escape to dynamic typing as mypy's approach to static typing doesn't restrict |
25 |
| -what you can do in your programs. Using mypy will make your programs easier to |
26 |
| -understand, debug, and maintain. |
| 9 | +Mypy is a static type checker for Python. |
| 10 | + |
| 11 | +Type checkers help ensure that you're using variables and functions in your code |
| 12 | +correctly. With mypy, add type hints (:pep:`484`) |
| 13 | +to your Python programs, and mypy will warn you when you use those types |
| 14 | +incorrectly. |
| 15 | + |
| 16 | +Python is a dynamic language, so usually you'll only see errors in your code |
| 17 | +when you attempt to run it. Mypy is a *static* checker, so it finds bugs |
| 18 | +in your programs without even running them! |
| 19 | + |
| 20 | +Here is a small example to whet your appetite: |
| 21 | + |
| 22 | +.. code-block:: python |
27 | 23 |
|
28 |
| -This documentation provides a short introduction to mypy. It will help you |
29 |
| -get started writing statically typed code. Knowledge of Python and a |
30 |
| -statically typed object-oriented language, such as Java, are assumed. |
| 24 | + number = input("What is your favourite number?") |
| 25 | + print("It is", number + 1) # error: Unsupported operand types for + ("str" and "int") |
| 26 | +
|
| 27 | +Adding type hints for mypy does not interfere with the way your program would |
| 28 | +otherwise run. Think of type hints as similar to comments! You can always use |
| 29 | +the Python interpreter to run your code, even if mypy reports errors. |
| 30 | + |
| 31 | +Mypy is designed with gradual typing in mind. This means you can add type |
| 32 | +hints to your code base slowly and that you can always fall back to dynamic |
| 33 | +typing when static typing is not convenient. |
| 34 | + |
| 35 | +Mypy has a powerful and easy-to-use type system, supporting features such as |
| 36 | +type inference, generics, callable types, tuple types, union types, |
| 37 | +structural subtyping and more. Using mypy will make your programs easier to |
| 38 | +understand, debug, and maintain. |
31 | 39 |
|
32 | 40 | .. note::
|
33 | 41 |
|
|
0 commit comments