Debian Codegraph @

docs/source/faq.rst

raw
1 Frequently Asked Questions
2 ==========================
3
4 Why have both dynamic and static typing?
5 ****************************************
6
7 Dynamic typing can be flexible, powerful, convenient and easy. But
8 it's not always the best approach; there are good reasons why many
9 developers choose to use statically typed languages or static typing
10 for Python.
11
12 Here are some potential benefits of mypy-style static typing:
13
14 - Static typing can make programs easier to understand and
15 maintain. Type declarations can serve as machine-checked
16 documentation. This is important as code is typically read much more
17 often than modified, and this is especially important for large and
18 complex programs.
19
20 - Static typing can help you find bugs earlier and with less testing
21 and debugging. Especially in large and complex projects this can be
22 a major time-saver.
23
24 - Static typing can help you find difficult-to-find bugs before your
25 code goes into production. This can improve reliability and reduce
26 the number of security issues.
27
28 - Static typing makes it practical to build very useful development
29 tools that can improve programming productivity or software quality,
30 including IDEs with precise and reliable code completion, static
31 analysis tools, etc.
32
33 - You can get the benefits of both dynamic and static typing in a
34 single language. Dynamic typing can be perfect for a small project
35 or for writing the UI of your program, for example. As your program
36 grows, you can adapt tricky application logic to static typing to
37 help maintenance.
38
39 See also the `front page <https://www.mypy-lang.org>`_ of the mypy web
40 site.
41
42 Would my project benefit from static typing?
43 ********************************************
44
45 For many projects dynamic typing is perfectly fine (we think that
46 Python is a great language). But sometimes your projects demand bigger
47 guns, and that's when mypy may come in handy.
48
49 If some of these ring true for your projects, mypy (and static typing)
50 may be useful:
51
52 - Your project is large or complex.
53
54 - Your codebase must be maintained for a long time.
55
56 - Multiple developers are working on the same code.
57
58 - Running tests takes a lot of time or work (type checking helps
59 you find errors quickly early in development, reducing the number of
60 testing iterations).
61
62 - Some project members (devs or management) don't like dynamic typing,
63 but others prefer dynamic typing and Python syntax. Mypy could be a
64 solution that everybody finds easy to accept.
65
66 - You want to future-proof your project even if currently none of the
67 above really apply. The earlier you start, the easier it will be to
68 adopt static typing.
69
70 Can I use mypy to type check my existing Python code?
71 *****************************************************
72
73 Mypy supports most Python features and idioms, and many large Python
74 projects are using mypy successfully. Code that uses complex
75 introspection or metaprogramming may be impractical to type check, but
76 it should still be possible to use static typing in other parts of a
77 codebase that are less dynamic.
78
79 Will static typing make my programs run faster?
80 ***********************************************
81
82 Mypy only does static type checking and it does not improve
83 performance. It has a minimal performance impact. In the future, there
84 could be other tools that can compile statically typed mypy code to C
85 modules or to efficient JVM bytecode, for example, but this is outside
86 the scope of the mypy project.
87
88 Is mypy free?
89 *************
90
91 Yes. Mypy is free software, and it can also be used for commercial and
92 proprietary projects. Mypy is available under the MIT license.
93
94 Can I use duck typing with mypy?
95 ********************************
96
97 Mypy provides support for both `nominal subtyping
98 <https://en.wikipedia.org/wiki/Nominative_type_system>`_ and
99 `structural subtyping
100 <https://en.wikipedia.org/wiki/Structural_type_system>`_.
101 Structural subtyping can be thought of as "static duck typing".
102 Some argue that structural subtyping is better suited for languages with duck
103 typing such as Python. Mypy however primarily uses nominal subtyping,
104 leaving structural subtyping mostly opt-in (except for built-in protocols
105 such as :py:class:`~collections.abc.Iterable` that always support structural
106 subtyping). Here are some reasons why:
107
108 1. It is easy to generate short and informative error messages when
109 using a nominal type system. This is especially important when
110 using type inference.
111
112 2. Python provides built-in support for nominal :py:func:`isinstance` tests and
113 they are widely used in programs. Only limited support for structural
114 :py:func:`isinstance` is available, and it's less type safe than nominal type tests.
115
116 3. Many programmers are already familiar with static, nominal subtyping and it
117 has been successfully used in languages such as Java, C++ and
118 C#. Fewer languages use structural subtyping.
119
120 However, structural subtyping can also be useful. For example, a "public API"
121 may be more flexible if it is typed with protocols. Also, using protocol types
122 removes the necessity to explicitly declare implementations of ABCs.
123 As a rule of thumb, we recommend using nominal classes where possible, and
124 protocols where necessary. For more details about protocol types and structural
125 subtyping see :ref:`protocol-types` and :pep:`544`.
126
127 I like Python and I have no need for static typing
128 **************************************************
129
130 The aim of mypy is not to convince everybody to write statically typed
131 Python -- static typing is entirely optional, now and in the
132 future. The goal is to give more options for Python programmers, to
133 make Python a more competitive alternative to other statically typed
134 languages in large projects, to improve programmer productivity, and
135 to improve software quality.
136
137 How are mypy programs different from normal Python?
138 ***************************************************
139
140 Since you use a vanilla Python implementation to run mypy programs,
141 mypy programs are also Python programs. The type checker may give
142 warnings for some valid Python code, but the code is still always
143 runnable. Also, a few Python features are still not
144 supported by mypy, but this is gradually improving.
145
146 The obvious difference is the availability of static type
147 checking. The section :ref:`common_issues` mentions some
148 modifications to Python code that may be required to make code type
149 check without errors. Also, your code must make defined
150 attributes explicit.
151
152 Mypy supports modular, efficient type checking, and this seems to
153 rule out type checking some language features, such as arbitrary
154 monkey patching of methods.
155
156 How is mypy different from Cython?
157 **********************************
158
159 :doc:`Cython <cython:index>` is a variant of Python that supports
160 compilation to CPython C modules. It can give major speedups to
161 certain classes of programs compared to CPython, and it provides
162 static typing (though this is different from mypy). Mypy differs in
163 the following aspects, among others:
164
165 - Cython is much more focused on performance than mypy. Mypy is only
166 about static type checking, and increasing performance is not a
167 direct goal.
168
169 - The mypy syntax is arguably simpler and more "Pythonic" (no cdef/cpdef, etc.) for statically typed code.
170
171 - The mypy syntax is compatible with Python. Mypy programs are normal
172 Python programs that can be run using any Python
173 implementation. Cython has many incompatible extensions to Python
174 syntax, and Cython programs generally cannot be run without first
175 compiling them to CPython extension modules via C. Cython also has a
176 pure Python mode, but it seems to support only a subset of Cython
177 functionality, and the syntax is quite verbose.
178
179 - Mypy has a different set of type system features. For example, mypy
180 has genericity (parametric polymorphism), function types and
181 bidirectional type inference, which are not supported by
182 Cython. (Cython has fused types that are different but related to
183 mypy generics. Mypy also has a similar feature as an extension of
184 generics.)
185
186 - The mypy type checker knows about the static types of many Python
187 stdlib modules and can effectively type check code that uses them.
188
189 - Cython supports accessing C functions directly and many features are
190 defined in terms of translating them to C or C++. Mypy just uses
191 Python semantics, and mypy does not deal with accessing C library
192 functionality.
193
194 Does it run on PyPy?
195 *********************
196
197 Somewhat. With PyPy 3.8, mypy is at least able to type check itself.
198 With older versions of PyPy, mypy relies on `typed-ast
199 <https://github.com/python/typed_ast>`_, which uses several APIs that
200 PyPy does not support (including some internal CPython APIs).
201
202 Mypy is a cool project. Can I help?
203 ***********************************
204
205 Any help is much appreciated! `Contact
206 <https://www.mypy-lang.org/contact.html>`_ the developers if you would
207 like to contribute. Any help related to development, design,
208 publicity, documentation, testing, web site maintenance, financing,
209 etc. can be helpful. You can learn a lot by contributing, and anybody
210 can help, even beginners! However, some knowledge of compilers and/or
211 type systems is essential if you want to work on mypy internals.