/usr/share/doc/mypy-doc/html/faq.html is in mypy-doc 0.560-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | <!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Frequently Asked Questions — Mypy 0.560 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="index" title="Index"
href="genindex.html"/>
<link rel="search" title="Search" href="search.html"/>
<link rel="top" title="Mypy 0.560 documentation" href="index.html"/>
<link rel="next" title="Mypy syntax cheat sheet (Python 2)" href="cheat_sheet.html"/>
<link rel="prev" title="New features in Python 3.6" href="python36.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> Mypy
</a>
<div class="version">
0.560
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="introduction.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="basics.html">Basics</a></li>
<li class="toctree-l1"><a class="reference internal" href="getting_started.html">Getting started</a></li>
<li class="toctree-l1"><a class="reference internal" href="builtin_types.html">Built-in types</a></li>
<li class="toctree-l1"><a class="reference internal" href="python2.html">Type checking Python 2 code</a></li>
<li class="toctree-l1"><a class="reference internal" href="type_inference_and_annotations.html">Type inference and type annotations</a></li>
<li class="toctree-l1"><a class="reference internal" href="kinds_of_types.html">Kinds of types</a></li>
<li class="toctree-l1"><a class="reference internal" href="class_basics.html">Class basics</a></li>
<li class="toctree-l1"><a class="reference internal" href="dynamic_typing.html">Dynamically typed code</a></li>
<li class="toctree-l1"><a class="reference internal" href="function_overloading.html">Function Overloading</a></li>
<li class="toctree-l1"><a class="reference internal" href="casts.html">Casts</a></li>
<li class="toctree-l1"><a class="reference internal" href="duck_type_compatibility.html">Duck type compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="common_issues.html">Common issues</a></li>
<li class="toctree-l1"><a class="reference internal" href="generics.html">Generics</a></li>
<li class="toctree-l1"><a class="reference internal" href="supported_python_features.html">Supported Python features and modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="additional_features.html">Additional features</a></li>
<li class="toctree-l1"><a class="reference internal" href="command_line.html">The mypy command line</a></li>
<li class="toctree-l1"><a class="reference internal" href="config_file.html">The mypy configuration file</a></li>
<li class="toctree-l1"><a class="reference internal" href="python36.html">New features in Python 3.6</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Frequently Asked Questions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#why-have-both-dynamic-and-static-typing">Why have both dynamic and static typing?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#would-my-project-benefit-from-static-typing">Would my project benefit from static typing?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#can-i-use-mypy-to-type-check-my-existing-python-code">Can I use mypy to type check my existing Python code?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#will-static-typing-make-my-programs-run-faster">Will static typing make my programs run faster?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-do-i-type-check-my-python-2-code">How do I type check my Python 2 code?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#is-mypy-free">Is mypy free?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#can-i-use-structural-subtyping">Can I use structural subtyping?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#i-like-python-and-i-have-no-need-for-static-typing">I like Python and I have no need for static typing</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-are-mypy-programs-different-from-normal-python">How are mypy programs different from normal Python?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-is-mypy-different-from-pypy">How is mypy different from PyPy?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-is-mypy-different-from-cython">How is mypy different from Cython?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-is-mypy-different-from-nuitka">How is mypy different from Nuitka?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-is-mypy-different-from-rpython-or-shed-skin">How is mypy different from RPython or Shed Skin?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#mypy-is-a-cool-project-can-i-help">Mypy is a cool project. Can I help?</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="cheat_sheet.html">Mypy syntax cheat sheet (Python 2)</a></li>
<li class="toctree-l1"><a class="reference internal" href="cheat_sheet_py3.html">Mypy syntax cheat sheet (Python 3)</a></li>
<li class="toctree-l1"><a class="reference internal" href="revision_history.html">Revision history</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Mypy</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li>Frequently Asked Questions</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/faq.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="frequently-asked-questions">
<h1>Frequently Asked Questions<a class="headerlink" href="#frequently-asked-questions" title="Permalink to this headline">¶</a></h1>
<div class="section" id="why-have-both-dynamic-and-static-typing">
<h2>Why have both dynamic and static typing?<a class="headerlink" href="#why-have-both-dynamic-and-static-typing" title="Permalink to this headline">¶</a></h2>
<p>Dynamic typing can be flexible, powerful, convenient and easy. But
it’s not always the best approach; there are good reasons why many
developers choose to use statically typed languages.</p>
<p>Here are some potential benefits of mypy-style static typing:</p>
<ul class="simple">
<li>Static typing can make programs easier to understand and
maintain. Type declarations can serve as machine-checked
documentation. This is important as code is typically read much more
often than modified, and this is especially important for large and
complex programs.</li>
<li>Static typing can help you find bugs earlier and with less testing
and debugging. Especially in large and complex projects this can be
a major time-saver.</li>
<li>Static typing can help you find difficult-to-find bugs before your
code goes into production. This can improve reliability and reduce
the number of security issues.</li>
<li>Static typing makes it practical to build very useful development
tools that can improve programming productivity or software quality,
including IDEs with precise and reliable code completion, static
analysis tools, etc.</li>
<li>You can get the benefits of both dynamic and static typing in a
single language. Dynamic typing can be perfect for a small project
or for writing the UI of your program, for example. As your program
grows, you can adapt tricky application logic to static typing to
help maintenance.</li>
</ul>
<p>See also the <a class="reference external" href="http://www.mypy-lang.org">front page</a> of the mypy web
site.</p>
</div>
<div class="section" id="would-my-project-benefit-from-static-typing">
<h2>Would my project benefit from static typing?<a class="headerlink" href="#would-my-project-benefit-from-static-typing" title="Permalink to this headline">¶</a></h2>
<p>For many projects dynamic typing is perfectly fine (we think that
Python is a great language). But sometimes your projects demand bigger
guns, and that’s when mypy may come in handy.</p>
<p>If some of these ring true for your projects, mypy (and static typing)
may be useful:</p>
<ul class="simple">
<li>Your project is large or complex.</li>
<li>Your codebase must be maintained for a long time.</li>
<li>Multiple developers are working on the same code.</li>
<li>Running tests takes a lot of time or work (type checking may help
you find errors early in development, reducing the number of testing
iterations).</li>
<li>Some project members (devs or management) don’t like dynamic typing,
but others prefer dynamic typing and Python syntax. Mypy could be a
solution that everybody finds easy to accept.</li>
<li>You want to future-proof your project even if currently none of the
above really apply.</li>
</ul>
</div>
<div class="section" id="can-i-use-mypy-to-type-check-my-existing-python-code">
<h2>Can I use mypy to type check my existing Python code?<a class="headerlink" href="#can-i-use-mypy-to-type-check-my-existing-python-code" title="Permalink to this headline">¶</a></h2>
<p>It depends. Compatibility is pretty good, but some Python features are
not yet implemented or fully supported. The ultimate goal is to make
using mypy practical for most Python code. Code that uses complex
introspection or metaprogramming may be impractical to type check, but
it should still be possible to use static typing in other parts of a
program.</p>
</div>
<div class="section" id="will-static-typing-make-my-programs-run-faster">
<h2>Will static typing make my programs run faster?<a class="headerlink" href="#will-static-typing-make-my-programs-run-faster" title="Permalink to this headline">¶</a></h2>
<p>Mypy only does static type checking and it does not improve
performance. It has a minimal performance impact. In the future, there
could be other tools that can compile statically typed mypy code to C
modules or to efficient JVM bytecode, for example, but this is outside
the scope of the mypy project. It may also be possible to modify
existing Python VMs to take advantage of static type information, but
whether this is feasible is still unknown. This is nontrivial since
the runtime types do not necessarily correspond to the static types.</p>
</div>
<div class="section" id="how-do-i-type-check-my-python-2-code">
<h2>How do I type check my Python 2 code?<a class="headerlink" href="#how-do-i-type-check-my-python-2-code" title="Permalink to this headline">¶</a></h2>
<p>You can use a <a class="reference external" href="https://www.python.org/dev/peps/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code">comment-based function annotation syntax</a>
and use the <code class="docutils literal"><span class="pre">--py2</span></code> command-line option to type check your Python 2 code.
You’ll also need to install <code class="docutils literal"><span class="pre">typing</span></code> for Python 2 via <code class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">typing</span></code>.</p>
</div>
<div class="section" id="is-mypy-free">
<h2>Is mypy free?<a class="headerlink" href="#is-mypy-free" title="Permalink to this headline">¶</a></h2>
<p>Yes. Mypy is free software, and it can also be used for commercial and
proprietary projects. Mypy is available under the MIT license.</p>
</div>
<div class="section" id="can-i-use-structural-subtyping">
<h2>Can I use structural subtyping?<a class="headerlink" href="#can-i-use-structural-subtyping" title="Permalink to this headline">¶</a></h2>
<p>Mypy provides support for both <a class="reference external" href="https://en.wikipedia.org/wiki/Nominative_type_system">nominal subtyping</a> and
<a class="reference external" href="https://en.wikipedia.org/wiki/Structural_type_system">structural subtyping</a>.
Some argue that structural subtyping is better suited for languages with duck
typing such as Python. Mypy however primarily uses nominal subtyping,
leaving structural subtyping mostly opt-in (except for built-in protocols
such as <code class="docutils literal"><span class="pre">Iterable</span></code> that always support structural subtyping). Here are some
reasons why:</p>
<ol class="arabic simple">
<li>It is easy to generate short and informative error messages when
using a nominal type system. This is especially important when
using type inference.</li>
<li>Python provides built-in support for nominal <code class="docutils literal"><span class="pre">isinstance()</span></code> tests and
they are widely used in programs. Only limited support for structural
<code class="docutils literal"><span class="pre">isinstance()</span></code> is available, and it’s less type safe than
nominal type tests.</li>
<li>Many programmers are already familiar with static, nominal subtyping and it
has been successfully used in languages such as Java, C++ and
C#. Fewer languages use structural subtyping.</li>
</ol>
<p>However, structural subtyping can also be useful. For example, a “public API”
may be more flexible if it is typed with protocols. Also, using protocol types
removes the necessity to explicitly declare implementations of ABCs.
As a rule of thumb, we recommend using nominal classes where possible, and
protocols where necessary. For more details about protocol types and structural
subtyping see <a class="reference internal" href="class_basics.html#protocol-types"><span class="std std-ref">Protocols and structural subtyping</span></a> and
<a class="reference external" href="https://www.python.org/dev/peps/pep-0544/">PEP 544</a>.</p>
</div>
<div class="section" id="i-like-python-and-i-have-no-need-for-static-typing">
<h2>I like Python and I have no need for static typing<a class="headerlink" href="#i-like-python-and-i-have-no-need-for-static-typing" title="Permalink to this headline">¶</a></h2>
<p>That wasn’t really a question, was it? Mypy is not aimed at replacing
Python. The goal is to give more options for Python programmers, to
make Python a more competitive alternative to other statically typed
languages in large projects, to improve programmer productivity and to
improve software quality.</p>
</div>
<div class="section" id="how-are-mypy-programs-different-from-normal-python">
<h2>How are mypy programs different from normal Python?<a class="headerlink" href="#how-are-mypy-programs-different-from-normal-python" title="Permalink to this headline">¶</a></h2>
<p>Since you use a vanilla Python implementation to run mypy programs,
mypy programs are also Python programs. The type checker may give
warnings for some valid Python code, but the code is still always
runnable. Also, some Python features and syntax are still not
supported by mypy, but this is gradually improving.</p>
<p>The obvious difference is the availability of static type
checking. The section <a class="reference internal" href="common_issues.html#common-issues"><span class="std std-ref">Common issues</span></a> mentions some
modifications to Python code that may be required to make code type
check without errors. Also, your code must make attributes explicit and
use a explicit protocol representation. For example, you may want to
subclass an Abstract Base Class such as <code class="docutils literal"><span class="pre">typing.Iterable</span></code>.</p>
<p>Mypy will support modular, efficient type checking, and this seems to
rule out type checking some language features, such as arbitrary
runtime addition of methods. However, it is likely that many of these
features will be supported in a restricted form (for example, runtime
modification is only supported for classes or methods registered as
dynamic or ‘patchable’).</p>
</div>
<div class="section" id="how-is-mypy-different-from-pypy">
<h2>How is mypy different from PyPy?<a class="headerlink" href="#how-is-mypy-different-from-pypy" title="Permalink to this headline">¶</a></h2>
<p><em>This answer relates to PyPy as a Python implementation. See also the answer related to RPython below.</em></p>
<p>Mypy and PyPy are orthogonal. Mypy does static type checking, i.e. it
is basically a linter, but static typing has no runtime effect,
whereas the PyPy is an Python implementation. You can use PyPy to run
mypy programs.</p>
</div>
<div class="section" id="how-is-mypy-different-from-cython">
<h2>How is mypy different from Cython?<a class="headerlink" href="#how-is-mypy-different-from-cython" title="Permalink to this headline">¶</a></h2>
<p><a class="reference external" href="http://cython.org/">Cython</a> is a variant of Python that supports
compilation to CPython C modules. It can give major speedups to
certain classes of programs compared to CPython, and it provides
static typing (though this is different from mypy). Mypy differs in
the following aspects, among others:</p>
<ul class="simple">
<li>Cython is much more focused on performance than mypy. Mypy is only
about static type checking, and increasing performance is not a
direct goal.</li>
<li>The mypy syntax is arguably simpler and more “Pythonic” (no cdef/cpdef, etc.) for statically typed code.</li>
<li>The mypy syntax is compatible with Python. Mypy programs are normal
Python programs that can be run using any Python
implementation. Cython has many incompatible extensions to Python
syntax, and Cython programs generally cannot be run without first
compiling them to CPython extension modules via C. Cython also has a
pure Python mode, but it seems to support only a subset of Cython
functionality, and the syntax is quite verbose.</li>
<li>Mypy has a different set of type system features. For example, mypy
has genericity (parametric polymorphism), function types and
bidirectional type inference, which are not supported by
Cython. (Cython has fused types that are different but related to
mypy generics. Mypy also has a similar feature as an extension of
generics.)</li>
<li>The mypy type checker knows about the static types of many Python
stdlib modules and can effectively type check code that uses them.</li>
<li>Cython supports accessing C functions directly and many features are
defined in terms of translating them to C or C++. Mypy just uses
Python semantics, and mypy does not deal with accessing C library
functionality.</li>
</ul>
</div>
<div class="section" id="how-is-mypy-different-from-nuitka">
<h2>How is mypy different from Nuitka?<a class="headerlink" href="#how-is-mypy-different-from-nuitka" title="Permalink to this headline">¶</a></h2>
<p><a class="reference external" href="http://nuitka.net/">Nuitka</a> is a static compiler that can translate
Python programs to C++. Nuitka integrates with the CPython
runtime. Nuitka has additional future goals, such as using type
inference and whole-program analysis to further speed up code. Here
are some differences:</p>
<ul class="simple">
<li>Nuitka is primarily focused on speeding up Python code. Mypy focuses
on static type checking and facilitating better tools.</li>
<li>Whole-program analysis tends to be slow and scale poorly to large or
complex programs. It is still unclear if Nuitka can solve these
issues. Mypy does not use whole-program analysis and will support
modular type checking (though this has not been implemented yet).</li>
</ul>
</div>
<div class="section" id="how-is-mypy-different-from-rpython-or-shed-skin">
<h2>How is mypy different from RPython or Shed Skin?<a class="headerlink" href="#how-is-mypy-different-from-rpython-or-shed-skin" title="Permalink to this headline">¶</a></h2>
<p><a class="reference external" href="http://doc.pypy.org/en/latest/coding-guide.html">RPython</a> and <a class="reference external" href="http://shed-skin.blogspot.co.uk/">Shed
Skin</a> are basically statically
typed subsets of Python. Mypy does the following important things
differently:</p>
<ul class="simple">
<li>RPython is primarily designed for implementing virtual machines;
mypy is a general-purpose tool.</li>
<li>Mypy supports both static and dynamic typing. Dynamically typed and
statically typed code can be freely mixed and can interact
seamlessly.</li>
<li>Mypy aims to support (in the future) fast and modular type
checking. Both RPython and Shed Skin use whole-program type
inference which is very slow, does not scale well to large programs
and often produces confusing error messages. Mypy can support
modularity since it only uses local type inference; static type
checking depends on having type annotations for functions
signatures.</li>
<li>Mypy will support introspection, dynamic loading of code and many
other dynamic language features (though using these may make static
typing less effective). RPython and Shed Skin only support a
restricted Python subset without several of these features.</li>
<li>Mypy supports user-defined generic types.</li>
</ul>
</div>
<div class="section" id="mypy-is-a-cool-project-can-i-help">
<h2>Mypy is a cool project. Can I help?<a class="headerlink" href="#mypy-is-a-cool-project-can-i-help" title="Permalink to this headline">¶</a></h2>
<p>Any help is much appreciated! <a class="reference external" href="http://www.mypy-lang.org/contact.html">Contact</a> the developers if you would
like to contribute. Any help related to development, design,
publicity, documentation, testing, web site maintenance, financing,
etc. can be helpful. You can learn a lot by contributing, and anybody
can help, even beginners! However, some knowledge of compilers and/or
type systems is essential if you want to work on mypy internals.</p>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="cheat_sheet.html" class="btn btn-neutral float-right" title="Mypy syntax cheat sheet (Python 2)" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="python36.html" class="btn btn-neutral" title="New features in Python 3.6" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2017, Jukka Lehtosalo.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'0.560',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>
|