Skip to content

Commit 285b48d

Browse files
committed
Merge remote-tracking branch 'upstream/master' into variance
2 parents 064dca8 + bbb2dd4 commit 285b48d

File tree

175 files changed

+4120
-4037
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+4120
-4037
lines changed

README.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ What is mypy?
88
-------------
99

1010
Mypy is an optional static type checker for Python. You can add type
11-
annotations to your Python programs and use mypy to type check them
12-
statically to find errors before running them. You can also
13-
seamlessly mix dynamic and static typing in your programs, so you can
14-
always fall back to dynamic typing. Mypy programs are valid Python
15-
3.x and you use a normal Python interpreter to run them. There is
16-
essentially no performance overhead when using mypy, since mypy does
17-
not introduce additional runtime type checking.
11+
hints to your Python programs using the upcoming standard for type
12+
annotations introduced in Python 3.5 beta 1 (PEP 484), and use mypy to
13+
type check them statically. Find bugs in your programs without even
14+
running them!
15+
16+
The type annotation notation has also been backported to earlier
17+
Python 3.x versions. Mypy programs are valid Python 3.x and you use a
18+
normal Python interpreter to run them. There is essentially no
19+
performance overhead when using mypy, since mypy does not introduce
20+
runtime type checking.
21+
22+
You can mix dynamic and static typing in your programs. You can always
23+
fall back to dynamic typing when static typing is not convenient, such
24+
as for legacy code.
1825

1926
Here is a small example to whet your appetite:
2027

@@ -118,28 +125,20 @@ Development status
118125
------------------
119126

120127
Mypy is work in progress and is not yet production quality (though
121-
mypy development is already done in mypy!).
128+
mypy development is already done using mypy!).
122129

123130
Here are some of the more significant Python features not supported
124131
right now (but all of these will improve):
125132

126-
- Python 2.x support not really yet usable
133+
- Python 2.x support not usable yet
127134
- properties with setters not supported
128-
- somewhat limited operator overloading
135+
- limited metaclass support
129136
- only a subset of Python standard library modules are supported, and some
130137
only partially
131-
- limited metaclass support
138+
- 3rd party module support is limited
132139

133-
Some mypy-specific features are also not supported or only partially
134-
supported, including these:
135-
136-
- function overloading does not work properly in all cases, including
137-
some instances of method overriding, and keyword arguments
138-
- no 'Dynamic' classes
139-
- there is no way to use dynamic typing by default for top-level code
140-
141-
The current development focus is to support static type checking with a good
142-
subset of Python features (both 2.x and 3.x).
140+
The current development focus is to have a good coverage of Python
141+
features and the standard library (initially 3.x, and later 2.7).
143142

144143

145144
Issue tracker
@@ -150,6 +149,8 @@ tracker:
150149

151150
https://github.com/JukkaL/mypy/issues
152151

152+
Feel free to also ask questions on the tracker.
153+
153154

154155
Help wanted
155156
-----------

docs/source/class_basics.rst

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,15 @@ initialized within the class. Mypy infers the types of attributes:
2121
2222
This is a bit like each class having an implicitly defined
2323
``__slots__`` attribute. In Python semantics this is only enforced
24-
during type checking: at runtime we use standard Python semantics. You
25-
can selectively define a class as *dynamic*; dynamic classes have
26-
Python-like compile-time semantics, and they allow you to assign to
27-
arbitrary attributes anywhere in a program without the type checker
28-
complaining:
24+
during type checking: at runtime we use standard Python semantics.
2925

30-
.. code-block:: python
31-
32-
from typing import Dynamic
33-
34-
class A(Dynamic):
35-
pass
36-
37-
a = A()
38-
a.x = 2 # OK, no need to define x explicitly.
39-
40-
Mypy also lets you read arbitrary attributes of dynamic class
41-
instances. This limits type checking effectiveness, so you should only
42-
use dynamic classes when you really need them.
43-
44-
.. note::
45-
46-
Dynamic classes are not implemented in the current mypy version.
47-
48-
You can declare variables in the class body explicitly using
49-
``Undefined`` or a type comment:
26+
You can declare types of variables in the class body explicitly using
27+
a type comment:
5028

5129
.. code-block:: python
5230
5331
class A:
54-
x = Undefined(List[int]) # Declare attribute x of type List[int]
55-
y = 0 # type: Any # Declare attribute y of type Any
32+
x = None # type: List[int] # Declare attribute x of type List[int]
5633
5734
a = A()
5835
a.x = [1] # OK
@@ -67,10 +44,10 @@ in a method:
6744
6845
class A:
6946
def __init__(self) -> None:
70-
self.x = Undefined(List[int]) # OK
47+
self.x = [] # type: List[int]
7148
7249
def f(self) -> None:
73-
self.y = 0 # type: Any # OK
50+
self.y = 0 # type: Any
7451
7552
You can only define an instance variable within a method if you assign
7653
to it explicitly using ``self``:

docs/source/conf.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
# If extensions (or modules to document with autodoc) are in another directory,
1919
# add these directories to sys.path here. If the directory is relative to the
2020
# documentation root, use os.path.abspath to make it absolute, like shown here.
21-
#sys.path.insert(0, os.path.abspath('.'))
21+
sys.path.insert(0, os.path.abspath('../..'))
22+
23+
from mypy.version import __version__ as mypy_version
2224

2325
# -- General configuration ------------------------------------------------
2426

@@ -51,9 +53,9 @@
5153
# built documents.
5254
#
5355
# The short X.Y version.
54-
version = '0.0.1.dev1'
56+
version = mypy_version.split('-')[0]
5557
# The full version, including alpha/beta/rc tags.
56-
release = '0.0.1.dev1'
58+
release = mypy_version
5759

5860
# The language for content autogenerated by Sphinx. Refer to documentation
5961
# for a list of supported languages.

docs/source/dynamic_typing.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ operations:
7070
o.foo() # Error!
7171
o + 2 # Error!
7272
open(o) # Error!
73-
n = Undefined(int)
73+
n = 1 # type: int
7474
n = o # Error!
7575
76-
You can use ``cast()`` (see chapter :ref:`casts`) to go from a general
77-
type such as ``object`` to a more specific type (subtype) such as
78-
``int``. ``cast()`` is not needed with dynamically typed values
79-
(values with type ``Any``).
76+
You can use ``cast()`` (see chapter :ref:`casts`) or ``isinstance`` to
77+
go from a general type such as ``object`` to a more specific
78+
type (subtype) such as ``int``. ``cast()`` is not needed with
79+
dynamically typed values (values with type ``Any``).

docs/source/kinds_of_types.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ to a variable with a more precise type:
4040

4141
.. code-block:: python
4242
43-
a, s = Undefined(Any), Undefined(str)
44-
a = 2 # OK
45-
s = a # OK
43+
a = None # type: Any
44+
s = '' # type: str
45+
a = 2 # OK
46+
s = a # OK
4647
4748
Declared (and inferred) types are erased at runtime (they are
4849
basically treated as comments), and thus the above code does not
@@ -220,8 +221,8 @@ string-literal types with non-string-literal types freely:
220221

221222
.. code-block:: python
222223
223-
a = Undefined(List['A']) # OK
224-
n = Undefined('int') # OK, though not useful
224+
def f(a: List['A']) -> None: ... # OK
225+
def g(n: 'int') -> None: ... # OK, though not useful
225226
226227
class A: pass
227228

docs/source/type_inference_and_annotations.rst

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,23 @@ to ``Any``, which is discussed later.
6060
Explicit types for variables
6161
****************************
6262

63-
.. code-block:: python
64-
65-
s = Undefined(str) # Declare type of x to be str.
66-
s = 'x' # OK
67-
s = 1 # Type check error
68-
69-
The Undefined call evaluates to a special ``Undefined`` object that
70-
raises an exception on any operation (except for ``is``, which cannot
71-
be overloaded):
63+
You can override the inferred type of a variable by using a
64+
special type comment after an assignment statement:
7265

7366
.. code-block:: python
7467
75-
s = Undefined(str)
76-
if s: # Runtime error: undefined value
77-
print('hello')
68+
x = 1 # type: Union[int, str]
7869
79-
You can also override the inferred type of a variable by using a
80-
special comment after an assignment statement:
70+
Without the type comment, the type of ``x`` would be just ``int``. We
71+
use an annotation to give it a more general type ``Union[int, str]``.
72+
Mypy checks that the type of the initializer is compatible with the
73+
declared type. This example is not valid, since the initializer is
74+
a floating point number, and this is incompatible with the declared
75+
type:
8176

8277
.. code-block:: python
8378
84-
x = 1 # type: Union[int, str]
85-
86-
Here the ``# type:`` comment applies both to the assignment target, in
87-
this case ``x``, and also the initializer expression, via context.
88-
The type checker infers the value of a variable from the initializer.
79+
x = 1.1 # type: Union[int, str] # Error!
8980
9081
Explicit types for collections
9182
******************************
@@ -116,11 +107,10 @@ type separately:
116107

117108
.. code-block:: python
118109
119-
n, s = Undefined(int), Undefined(str) # Declare an integer and a string
120110
i, found = 0, False # type: int, bool
121111
122-
When using the latter form, you can optionally use parentheses around
123-
the types, assignment targets and assigned expression:
112+
You can optionally use parentheses around the types, assignment targets
113+
and assigned expression:
124114

125115
.. code-block:: python
126116
@@ -130,7 +120,7 @@ the types, assignment targets and assigned expression:
130120
(i, found) = (0, False) # type: (int, bool) # OK
131121
132122
Starred expressions
133-
******************************
123+
*******************
134124

135125
In most cases, mypy can infer the type of starred expressions from the
136126
right-hand side of an assignment, but not always:

lib-python/3.2/posixpath.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from genericpath import *
1818

1919
from typing import (
20-
Tuple, BinaryIO, TextIO, Pattern, AnyStr, List, Set, Any, Union, Undefined
20+
Tuple, BinaryIO, TextIO, Pattern, AnyStr, List, Set, Any, Union
2121
)
2222

2323
__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
@@ -249,17 +249,16 @@ def expanduser(path: AnyStr) -> AnyStr:
249249
i = path.find(sep, 1)
250250
if i < 0:
251251
i = len(path)
252-
userhome = Undefined(Union[str, bytes])
253252
if i == 1:
253+
userhome = None # type: Union[str, bytes]
254254
if 'HOME' not in os.environ:
255255
import pwd
256256
userhome = pwd.getpwuid(os.getuid()).pw_dir
257257
else:
258258
userhome = os.environ['HOME']
259259
else:
260260
import pwd
261-
name = Undefined(Union[str, bytes])
262-
name = path[1:i]
261+
name = path[1:i] # type: Union[str, bytes]
263262
if isinstance(name, bytes):
264263
name = str(name, 'ASCII')
265264
try:
@@ -311,15 +310,15 @@ def expandvars(path: AnyStr) -> AnyStr:
311310
if not m:
312311
break
313312
i, j = m.span(0)
314-
name = Undefined(Union[str, bytes])
313+
name = None # type: Union[str, bytes]
315314
name = m.group(1)
316315
if name.startswith(start) and name.endswith(end):
317316
name = name[1:-1]
318317
if isinstance(name, bytes):
319318
name = str(name, 'ASCII')
320319
if name in os.environ:
321320
tail = path[j:]
322-
value = Undefined(Union[str, bytes])
321+
value = None # type: Union[str, bytes]
323322
value = os.environ[name]
324323
if isinstance(path, bytes):
325324
value = value.encode('ASCII')

lib-python/3.2/subprocess.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ class Popen(args, bufsize=0, executable=None,
348348
import errno
349349

350350
from typing import (
351-
Any, Tuple, List, Sequence, Undefined, Callable, Mapping, cast, Set, Dict,
352-
IO, TextIO, AnyStr
351+
Any, Tuple, List, Sequence, Callable, Mapping, cast, Set, Dict, IO,
352+
TextIO, AnyStr
353353
)
354354
from types import TracebackType
355355

lib-python/3.2/tempfile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
from typing import (
4040
Any as _Any, Callable as _Callable, Iterator as _Iterator,
41-
Undefined as _Undefined, List as _List, Tuple as _Tuple, Dict as _Dict,
42-
Iterable as _Iterable, IO as _IO, cast as _cast,
41+
List as _List, Tuple as _Tuple, Dict as _Dict, Iterable as _Iterable,
42+
IO as _IO, cast as _cast,
4343
)
4444
from types import TracebackType as _TracebackType
4545

@@ -502,7 +502,7 @@ class SpooledTemporaryFile:
502502
when a fileno is needed.
503503
"""
504504
_rolled = False
505-
_file = _Undefined # type: _Any # BytesIO, StringIO or TemporaryFile
505+
_file = None # type: _Any # BytesIO, StringIO or TemporaryFile
506506

507507
def __init__(self, max_size: int = 0, mode: str = 'w+b',
508508
buffering: int = -1, encoding: str = None,

lib-python/3.2/test/test_pprint.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import collections
77
import itertools
88

9-
from typing import List, Any, Dict, Tuple, cast, Undefined, Callable
9+
from typing import List, Any, Dict, Tuple, cast, Callable
1010

1111
# list, tuple and dict subclasses that do or don't overwrite __repr__
1212
class list2(list):
@@ -457,8 +457,7 @@ def test_sort_unorderable_values(self) -> None:
457457
keys = [Unorderable() for i in range(n)]
458458
random.shuffle(keys)
459459
skeys = sorted(keys, key=id)
460-
clean = Undefined(Callable[[str], str])
461-
clean = lambda s: s.replace(' ', '').replace('\n','')
460+
clean = lambda s: s.replace(' ', '').replace('\n','') # type: Callable[[str], str]
462461

463462
self.assertEqual(clean(pprint.pformat(set(keys))),
464463
'{' + ','.join(map(repr, skeys)) + '}')

0 commit comments

Comments
 (0)