@@ -46,30 +46,29 @@ Arguments with default values can be annotated as follows:
4646 def greeting (name : str , prefix : str = ' Mr.' ) -> str :
4747 return ' Hello, {} {} ' .format(name, prefix)
4848
49- Mixing dynamic and static typing
50- ********************************
49+ Running mypy
50+ ************
5151
52- Mixing dynamic and static typing within a single file is often
53- useful. For example, if you are migrating existing Python code to
54- static typing, it may be easiest to do this incrementally, such as by
55- migrating a few functions at a time. Also, when prototyping a new
56- feature, you may decide to first implement the relevant code using
57- dynamic typing and only add type signatures later, when the code is
58- more stable.
52+ You can type check a program by using the ``mypy `` tool, which is
53+ basically a linter -- it checks your program for errors without actually
54+ running it::
5955
60- .. code-block :: python
56+ $ mypy program.py
6157
62- def f ():
63- 1 + ' x ' # No static type error (dynamically typed)
58+ All errors reported by mypy are essentially warnings that you are free
59+ to ignore, if you so wish.
6460
65- def g () -> None :
66- 1 + ' x' # Type check error (statically typed)
61+ The next chapter explains how to download and install mypy:
62+ :ref: `getting-started `.
63+
64+ More command line options are documented in :ref: `command-line `.
6765
6866.. note ::
6967
70- The earlier stages of mypy, known as the semantic analysis, may
71- report errors even for dynamically typed functions. However, you
72- should not rely on this, as this may change in the future.
68+ Depending on how mypy is configured, you may have to run mypy like
69+ this::
70+
71+ $ python3 -m mypy program.py
7372
7473The typing module
7574*****************
@@ -87,37 +86,33 @@ them (we'll explain ``Iterable`` later in this document):
8786 print (' Hello, {} ' .format(name))
8887
8988 For brevity, we often omit the ``typing `` import in code examples, but
90- you should always include it in modules that contain statically typed
91- code.
92-
93- The presence or absence of the ``typing `` module does not affect
94- whether your code is type checked; it is only required when you use
95- one or more special features it defines.
89+ mypy will give an error if you use definitions such as ``Iterable ``
90+ without first importing them.
9691
97- Type checking programs
98- **********************
99-
100- You can type check a program by using the ``mypy `` tool, which is
101- basically a linter -- it checks your program for errors without actually
102- running it::
92+ Mixing dynamic and static typing
93+ ********************************
10394
104- $ mypy program.py
95+ Mixing dynamic and static typing within a single file is often
96+ useful. For example, if you are migrating existing Python code to
97+ static typing, it may be easiest to do this incrementally, such as by
98+ migrating a few functions at a time. Also, when prototyping a new
99+ feature, you may decide to first implement the relevant code using
100+ dynamic typing and only add type signatures later, when the code is
101+ more stable.
105102
106- All errors reported by mypy are essentially warnings that you are free
107- to ignore, if you so wish.
103+ .. code-block :: python
108104
109- The next chapter explains how to download and install mypy :
110- :ref: ` getting-started `.
105+ def f () :
106+ 1 + ' x ' # No static type error (dynamically typed)
111107
112- More command line options are documented in :ref: `command-line `.
108+ def g () -> None :
109+ 1 + ' x' # Type check error (statically typed)
113110
114111 .. note ::
115112
116- Depending on how mypy is configured, you may have to explicitly use
117- the Python 3 interpreter to run mypy. The mypy tool is an ordinary
118- mypy (and so also Python) program. For example::
119-
120- $ python3 -m mypy program.py
113+ The earlier stages of mypy, known as the semantic analysis, may
114+ report errors even for dynamically typed functions. However, you
115+ should not rely on this, as this may change in the future.
121116
122117.. _library-stubs :
123118
@@ -147,7 +142,7 @@ the builtins contains a definition like this for ``chr``:
147142
148143 def chr (code : int ) -> str : ...
149144
150- In stub files we don't care about the function bodies, so we use
145+ In stub files we don't care about the function bodies, so we use
151146an ellipsis instead. That ``... `` is three literal dots!
152147
153148Mypy complains if it can't find a stub (or a real module) for a
@@ -175,7 +170,7 @@ typeshed yet).
175170
176171That's it! Now you can access the module in mypy programs and type check
177172code that uses the library. If you write a stub for a library module,
178- consider making it available for other programmers that use mypy
173+ consider making it available for other programmers that use mypy
179174by contributing it back to the typeshed repo.
180175
181176There is more information about creating stubs in the
0 commit comments