/usr/share/doc/mypy-doc/html/python2.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 | <!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>Type checking Python 2 code — 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="Type inference and type annotations" href="type_inference_and_annotations.html"/>
<link rel="prev" title="Built-in types" href="builtin_types.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 current"><a class="current reference internal" href="#">Type checking Python 2 code</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#multi-line-python-2-function-annotations">Multi-line Python 2 function annotations</a></li>
<li class="toctree-l2"><a class="reference internal" href="#additional-notes">Additional notes</a></li>
</ul>
</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"><a class="reference internal" href="faq.html">Frequently Asked Questions</a></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>Type checking Python 2 code</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/python2.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="type-checking-python-2-code">
<span id="python2"></span><h1>Type checking Python 2 code<a class="headerlink" href="#type-checking-python-2-code" title="Permalink to this headline">¶</a></h1>
<p>For code that needs to be Python 2.7 compatible, function type
annotations are given in comments, since the function annotation
syntax was introduced in Python 3. The comment-based syntax is
specified in <a class="reference external" href="https://www.python.org/dev/peps/pep-0484">PEP 484</a>.</p>
<p>Run mypy in Python 2 mode by using the <code class="docutils literal"><span class="pre">--py2</span></code> option:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ mypy --py2 program.py
</pre></div>
</div>
<p>To run your program, you must have the <code class="docutils literal"><span class="pre">typing</span></code> module in your
Python 2 module search path. Use <code class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">typing</span></code> to install the
module. This also works for Python 3 versions prior to 3.5 that don’t
include <code class="docutils literal"><span class="pre">typing</span></code> in the standard library.</p>
<p>The example below illustrates the Python 2 function type annotation
syntax. This syntax is also valid in Python 3 mode:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>
<span class="k">def</span> <span class="nf">hello</span><span class="p">():</span> <span class="c1"># type: () -> None</span>
<span class="k">print</span> <span class="s1">'hello'</span>
<span class="k">class</span> <span class="nc">Example</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lst</span><span class="p">,</span> <span class="n">opt</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># type: (List[str], int, *str, **bool) -> int</span>
<span class="sd">"""Docstring comes after type comment."""</span>
<span class="o">...</span>
</pre></div>
</div>
<p>It’s worth going through these details carefully to avoid surprises:</p>
<ul class="simple">
<li>You don’t provide an annotation for the <code class="docutils literal"><span class="pre">self</span></code> / <code class="docutils literal"><span class="pre">cls</span></code> variable of
methods.</li>
<li>Docstring always comes <em>after</em> the type comment.</li>
<li>For <code class="docutils literal"><span class="pre">*args</span></code> and <code class="docutils literal"><span class="pre">**kwargs</span></code> the type should be prefixed with
<code class="docutils literal"><span class="pre">*</span></code> or <code class="docutils literal"><span class="pre">**</span></code>, respectively (except when using the multi-line
annotation syntax described below). Again, the above example
illustrates this.</li>
<li>Things like <code class="docutils literal"><span class="pre">Any</span></code> must be imported from <code class="docutils literal"><span class="pre">typing</span></code>, even if they
are only used in comments.</li>
<li>In Python 2 mode <code class="docutils literal"><span class="pre">str</span></code> is implicitly promoted to <code class="docutils literal"><span class="pre">unicode</span></code>, similar
to how <code class="docutils literal"><span class="pre">int</span></code> is compatible with <code class="docutils literal"><span class="pre">float</span></code>. This is unlike <code class="docutils literal"><span class="pre">bytes</span></code> and
<code class="docutils literal"><span class="pre">str</span></code> in Python 3, which are incompatible. <code class="docutils literal"><span class="pre">bytes</span></code> in Python 2 is
equivalent to <code class="docutils literal"><span class="pre">str</span></code>. (This might change in the future.)</li>
</ul>
<div class="section" id="multi-line-python-2-function-annotations">
<span id="multi-line-annotation"></span><h2>Multi-line Python 2 function annotations<a class="headerlink" href="#multi-line-python-2-function-annotations" title="Permalink to this headline">¶</a></h2>
<p>Mypy also supports a multi-line comment annotation syntax. You
can provide a separate annotation for each argument using the variable
annotation syntax. When using the single-line annotation syntax
described above, functions with long argument lists tend to result in
overly long type comments and it’s often tricky to see which argument
type corresponds to which argument. The alternative, multi-line
annotation syntax makes long annotations easier to read and write.</p>
<p>Here is an example (from PEP 484):</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">send_email</span><span class="p">(</span><span class="n">address</span><span class="p">,</span> <span class="c1"># type: Union[str, List[str]]</span>
<span class="n">sender</span><span class="p">,</span> <span class="c1"># type: str</span>
<span class="n">cc</span><span class="p">,</span> <span class="c1"># type: Optional[List[str]]</span>
<span class="n">bcc</span><span class="p">,</span> <span class="c1"># type: Optional[List[str]]</span>
<span class="n">subject</span><span class="o">=</span><span class="s1">''</span><span class="p">,</span>
<span class="n">body</span><span class="o">=</span><span class="bp">None</span> <span class="c1"># type: List[str]</span>
<span class="p">):</span>
<span class="c1"># type: (...) -> bool</span>
<span class="sd">"""Send an email message. Return True if successful."""</span>
<span class="o"><</span><span class="n">code</span><span class="o">></span>
</pre></div>
</div>
<p>You write a separate annotation for each function argument on the same
line as the argument. Each annotation must be on a separate line. If
you leave out an annotation for an argument, it defaults to
<code class="docutils literal"><span class="pre">Any</span></code>. You provide a return type annotation in the body of the
function using the form <code class="docutils literal"><span class="pre">#</span> <span class="pre">type:</span> <span class="pre">(...)</span> <span class="pre">-></span> <span class="pre">rt</span></code>, where <code class="docutils literal"><span class="pre">rt</span></code> is the
return type. Note that the return type annotation contains literal
three dots.</p>
<p>Note that when using multi-line comments, you do not need to prefix the
types of your <code class="docutils literal"><span class="pre">*arg</span></code> and <code class="docutils literal"><span class="pre">**kwarg</span></code> parameters with <code class="docutils literal"><span class="pre">*</span></code> or <code class="docutils literal"><span class="pre">**</span></code>.
For example, here is how you would annotate the first example using
multi-line comments.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>
<span class="k">class</span> <span class="nc">Example</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">lst</span><span class="p">,</span> <span class="c1"># type: List[str]</span>
<span class="n">opt</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="c1"># type: int</span>
<span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="c1"># type: str</span>
<span class="o">**</span><span class="n">kwargs</span> <span class="c1"># type: bool</span>
<span class="p">):</span>
<span class="c1"># type: (...) -> int</span>
<span class="sd">"""Docstring comes after type comment."""</span>
<span class="o">...</span>
</pre></div>
</div>
</div>
<div class="section" id="additional-notes">
<h2>Additional notes<a class="headerlink" href="#additional-notes" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>You should include types for arguments with default values in the
annotation. The <code class="docutils literal"><span class="pre">opt</span></code> argument of <code class="docutils literal"><span class="pre">method</span></code> in the example at the
beginning of this section is an example of this.</li>
<li>The annotation can be on the same line as the function header or on
the following line.</li>
<li>The type syntax for variables is the same as for Python 3.</li>
<li>You don’t need to use string literal escapes for forward references
within comments.</li>
<li>Mypy uses a separate set of library stub files in <a class="reference external" href="https://github.com/python/typeshed">typeshed</a> for Python 2. Library support
may vary between Python 2 and Python 3.</li>
</ul>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="type_inference_and_annotations.html" class="btn btn-neutral float-right" title="Type inference and type annotations" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="builtin_types.html" class="btn btn-neutral" title="Built-in types" 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>
|