10000 Merge remote-tracking branch 'upstream/master' into literal_hash1 · python/mypy@9be2951 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9be2951

Browse files
committed
Merge remote-tracking branch 'upstream/master' into literal_hash1
Conflicts: mypy/binder.py mypy/nodes.py
2 parents f81d941 + 2d67784 commit 9be2951

Some content is hidden

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

90 files changed

+2258
-796
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ python:
1414
# - "pypy3"
1515

1616
install:
17+
- pip install -U pip setuptools wheel
1718
- pip install -r test-requirements.txt
1819
- python2 -m pip install --user typing
19-
- python setup.py install
20+
- pip install .
2021

2122
script:
22-
- python runtests.py -x lint
23+
- python runtests.py -j16 -x lint
2324
- if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then flake8; fi

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Mypy: Optional Static Typing for Python
22
=======================================
33

4-
[![Build Status](https://travis-ci.org/python/mypy.svg)](https://travis-ci.org/python/mypy)
4+
[![Build Status](https://api.travis-ci.org/python/mypy.svg?branch=master)](https://travis-ci.org/python/mypy)
55
[![Chat at https://gitter.im/python/typing](https://badges.gitter.im/python/typing.svg)](https://gitter.im/python/typing?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
66

77

@@ -31,7 +31,8 @@ type check them statically. Find bugs in your programs without even
3131
running them!
3232

3333
The type annotation standard has also been backported to earlier
34-
Python 3.x versions. Mypy supports Python 3.3 and later.
34+
Python 3.x versions. Mypy supports Python 3.3 and later.
35+
XML based reports do not work on Python 3.3 and 3.4 on Windows.
3536

3637
For Python 2.7, you can add annotations as comments (this is also
3738
specified in [PEP 484](https://www.python.org/dev/peps/pep-0484/)).
@@ -129,7 +130,7 @@ Or you can jump straight to the documentation:
129130
Troubleshooting
130131
---------------
131132

132-
Depending on your configuration, you may have to run `pip3` like
133+
Depending on your configuration, you may have to run `pip` like
133134
this:
134135

135136
$ python3 -m pip install -U mypy
@@ -140,8 +141,8 @@ can install it manually:
140141

141142
$ python3 -m pip install -U typed-ast
142143

143-
If the `mypy` command isn't found after installation: After either
144-
`pip3 install` or `setup.py install`, the `mypy` script and
144+
If the `mypy` command isn't found after installation: After
145+
`python3 -m pip install`, the `mypy` script and
145146
dependencies, including the `typing` module, will be installed to
146147
system-dependent locations. Sometimes the script directory will not
147148
be in `PATH`, and you have to add the target directory to `PATH`
@@ -156,6 +157,9 @@ In Windows, the script is generally installed in
156157

157158
C:\>\Python34\python \Python34\Scripts\mypy PROGRAM
158159

160+
If you are on Windows using Python 3.3 or 3.4, and would like to use XML reports
161+
download LXML from [PyPi](https://pypi.python.org/pypi/lxml).
162+
159163
### Working with `virtualenv`
160164

161165
If you are using [`virtualenv`](https://virtualenv.pypa.io/en/stable/),
@@ -254,9 +258,8 @@ Help wanted
254258
Any help in testing, development, documentation and other tasks is
255259
highly appreciated and useful to the project. There are tasks for
256260
contributors of all experience levels. If you're just getting started,
257-
check out the
258-
[difficulty/easy](https://github.com/python/mypy/labels/difficulty%2Feasy)
259-
label.
261+
ask on the [gitter chat](https://gitter.im/python/typing) for ideas of good
262+
beginner issues.
260263

261264
For more details, see the file [CONTRIBUTING.md](CONTRIBUTING.md).
262265

ROADMAP.md

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,26 @@ core team member that nobody else is working on the same thing.
99
on, and everything is subject to change. Near-term plans are likely
1010
more accurate.
1111

12-
## April-June 2017
13-
14-
- Add more comprehensive testing for `--incremental` and `--quick`
15-
modes to improve reliability. At least write more unit tests with
16-
focus on areas that have previously had bugs.
17-
([issue](https://github.com/python/mypy/issues/3455))
18-
19-
- Speed up `--quick` mode to better support million+ line codebases
20-
through some of these:
21-
22-
- Make it possible to use remote caching for incremental cache
23-
files. This would speed up a cold run with no local cache data.
24-
We need to update incremental cache to use hashes to determine
25-
whether files have changes to allow
26-
[sharing cache data](https://github.com/python/mypy/issues/3403).
27-
28-
- See if we can speed up deserialization of incremental cache
29-
files. Initial experiments aren’t very promising though so there
30-
might not be any easy wins left.
31-
([issue](https://github.com/python/mypy/issues/3456))
32-
33-
- Improve support for complex signatures such as `open(fn, 'rb')` and
34-
specific complex decorators such as `contextlib.contextmanager`
35-
through type checker plugins/hooks.
36-
([issue](https://github.com/python/mypy/issues/1240))
12+
## July-December 2017
13+
14+
- Fix remaining highest-priority TypedDict issues and make TypedDict
15+
an officially supported mypy feature.
3716

3817
- Document basic properties of all type operations used within mypy,
3918
including compatibility, proper subtyping, joins and meets.
4019
([issue](https://github.com/python/mypy/issues/3454))
4120

42-
- Make TypedDict an officially supported mypy feature. This makes it
43-
possible to give precise types for dictionaries that represent JSON
44-
objects, such as `{"path": "/dir/fnam.ext", "size": 1234}`.
45-
([issue](https://github.com/python/mypy/issues/3453))
46-
47-
- Make error messages more useful and informative.
21+
- Continue making error messages more useful and informative.
4822
([issue](https://github.com/python/mypy/labels/topic-usability))
4923

5024
- Resolve [#2008](https://github.com/python/mypy/issues/2008) (we are
5125
converging on approach 4).
5226

53-
## July-December 2017
54-
5527
- Invest some effort into systematically filling in missing
5628
annotations and stubs in typeshed, with focus on features heavily
5729
used at Dropbox. Better support for ORMs will be a separate
5830
project.
5931

60-
- Improve opt-in warnings about `Any` types to make it easier to keep
61-
code free from unwanted `Any` types. For example, warn about using
62-
`list` (instead of `List[x]`) and calling `open` if we can’t infer a
63-
precise return type, or using types imported from ignored modules
64-
(they are implicitly `Any`).
65-
6632
- Add support for protocols and structural subtyping (PEP 544).
6733

6834
- Switch completely to pytest and remove the custom testing framework.
@@ -73,24 +39,25 @@ more accurate.
7339
significantly. Even when using the incremental mode, processing a
7440
large number of files is not cheap.
7541

42+
- Provide much faster, reliable interactive feedback through
43+
fine-grained incremental type checking, built on top the daemon
44+
mode.
45+
7646
- Refactor and simplify specific tricky parts of mypy internals, such
7747
as the [conditional type binder](https://github.com/python/mypy/issues/3457),
7848
[symbol tables](https://github.com/python/mypy/issues/3458) or
7949
the various [semantic analysis passes](https://github.com/python/mypy/issues/3459).
8050

81-
- Implement a general type system plugin architecture. It should be
82-
able to support some typical ORM features at least, such as
83-
metaclasses that add methods with automatically inferred signatures
84-
and complex descriptors such as those used by Django models.
51+
- Make the mypy plugin architecture more general and officially
52+
supported. It should be able to support some typical ORM features at
53+
least, such as metaclasses that add methods with automatically
54+
inferred signatures and complex descriptors such as those used by
55+
Django models.
8556
([issue](https://github.com/python/mypy/issues/1240))
8657

8758
- Add support for statically typed
8859
[protobufs](https://developers.google.com/protocol-buffers/).
8960

90-
- Provide much faster, reliable interactive feedback through
91-
fine-grained incremental type checking, built on top the daemon
92-
mode.
93-
9461
- Start work on editor plugins and support for selected IDE features.
9562

9663
- Turn on `--strict-optional` by default.

appveyor.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
cache:
2+
- '%LOCALAPPDATA%\pip\Cache'
3+
14
environment:
25
matrix:
36

@@ -11,7 +14,7 @@ install:
1114
- "%PYTHON%\\python.exe -m pip install -r test-requirements.txt"
1215
- "git submodule update --init typeshed"
1316
- "cd typeshed && git config core.symlinks true && git reset --hard && cd .."
14-
- "%PYTHON%\\python.exe setup.py -q install"
17+
- "%PYTHON%\\python.exe -m pip install ."
1518

1619
build: off
1720

docs/source/basics.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ A function without a type annotation is considered dynamically typed:
1717
1818
You can declare the signature of a function using the Python 3
1919
annotation syntax (Python 2 is discussed later in :ref:`python2`).
20-
This makes the the function statically typed, and that causes type
20+
This makes the function statically typed, and that causes type
2121
checker report type errors within the function.
2222

2323
Here's a version of the above function that is statically typed and

docs/source/command_line.rst

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ flag (or its long form ``--help``)::
1111
usage: mypy [-h] [-v] [-V] [--python-version x.y] [--platform PLATFORM] [-2]
1212
[--ignore-missing-imports]
1313
[--follow-imports {normal,silent,skip,error}]
14+
[--disallow-any {unimported, expr, unannotated, decorated, explicit, generics}]
1415
[--disallow-untyped-calls] [--disallow-untyped-defs]
1516
[--check-untyped-defs] [--disallow-subclassing-any]
1617
[--warn-incomplete-stub] [--warn-redundant-casts]
1718
[--no-warn-no-return] [--warn-return-any] [--warn-unused-ignores]
18-
[--show-error-context] [-i] [--quick-and-dirty] [--cache-dir DIR]
19+
[--show-error-context] [--no-implicit-optional] [-i]
20+
[--quick-and-dirty] [--cache-dir DIR] [--skip-version-check]
1921
[--strict-optional]
2022
[--strict-optional-whitelist [GLOB [GLOB ...]]]
2123
[--junit-xml JUNIT_XML] [--pdb] [--show-traceback] [--stats]
2224
[--inferstats] [--custom-typing MODULE]
2325
[--custom-typeshed-dir DIR] [--scripts-are-modules]
2426
[--config-file CONFIG_FILE] [--show-column-numbers]
25-
[--find-occurrences CLASS.MEMBER] [--strict] [--strict-boolean]
27+
[--find-occurrences CLASS.MEMBER] [--strict]
28+
[--shadow-file SOURCE_FILE SHADOW_FILE] [--any-exprs-report DIR]
2629
[--cobertura-xml-report DIR] [--html-report DIR]
2730
[--linecount-report DIR] [--linecoverage-report DIR]
2831
[--memory-xml-report DIR] [--old-html-report DIR]
@@ -278,7 +281,8 @@ Here are some more useful flags:
278281

279282
- ``--disallow-any`` disallows various types of ``Any`` in a module.
280283
The option takes a comma-separated list of the following values:
281-
``unimported``, ``unannotated``, ``expr``, ``decorated``, ``explicit``.
284+
``unimported``, ``unannotated``, ``expr``, ``decorated``, ``explicit``,
285+
``generics``.
282286

283287
``unimported`` disallows usage of types that come from unfollowed imports
284288
(such types become aliases for ``Any``). Unfollowed imports occur either
@@ -304,6 +308,11 @@ Here are some more useful flags:
304308
``explicit`` disallows explicit ``Any`` in type positions such as type
305309
annotations and generic type parameters.
306310

311+
``generics`` disallows usage of generic types that do not specify explicit
312+
type parameters. Moreover, built-in collections (such as ``list`` and
313+
``dict``) become disallowed as you should use their aliases from the typing
314+
module (such as ``List[int]`` and ``Dict[str, str]``).
315+
307316
- ``--disallow-untyped-defs`` reports an error whenever it encounters
308317
a function definition without type annotations.
309318

@@ -421,13 +430,21 @@ Here are some more useful flags:
421430
- ``--strict`` mode enables all optional error checking flags. You can see the
422431
list of flags enabled by strict mode in the full ``mypy -h`` output.
423432

433+
.. _shadow-file:
434+
424435
- ``--shadow-file SOURCE_FILE SHADOW_FILE`` makes mypy typecheck SHADOW_FILE in
425436
place of SOURCE_FILE. Primarily intended for tooling. Allows tooling to
426437
make transformations to a file before type checking without having to change
427438
the file in-place. (For example, tooling could use this to display the type
428439
of an expression by wrapping it with a call to reveal_type in the shadow
429440
file and then parsing the output.)
430441

442+
.. _no-implicit-optional:
443+
444+
- ``--no-implicit-optional`` causes mypy to stop treating arguments
445+
with a ``None`` default value as having an implicit ``Optional[...]``
446+
type.
447+
431448
For the remaining flags you can read the full ``mypy -h`` output.
432449

433450
.. note::

docs/source/config_file.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ overridden by the pattern sections matching the module name.
124124
.. note::
125125

126126
If multiple pattern sections match a module they are processed in
127-
unspecified order.
127+
order of their occurrence in the config file.
128128

129129
- ``follow_imports`` (string, default ``normal``) directs what to do
130130
with imports when the imported module is found as a ``.py`` file and
@@ -150,8 +150,8 @@ overridden by the pattern sections matching the module name.
150150
- ``disallow_any`` (Comma-separated list, default empty) is an option to
151151
disallow various types of ``Any`` in a module. The flag takes a
152152
comma-separated list of the following arguments: ``unimported``,
153-
``unannotated``, ``expr``, ``decorated``, ``explicit``. For explanations
154-
see the discussion for the :ref:`--disallow-any <disallow-any>` option.
153+
``unannotated``, ``expr``, ``decorated``, ``explicit``, ``generics``.
154+
For explanations see the discussion for the :ref:`--disallow-any <disallow-any>` option.
155155

156156
- ``disallow_untyped_calls`` (Boolean, default False) disallows
157157
calling functions without type annotations from functions with type

docs/source/function_overloading.rst

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ accurately describe the function's behavior.
5656
# It may or may not have type hints; if it does,
5757
# these are checked against the overload definitions
5858
# as well as against the implementation body.
59-
def __getitem__(self, index):
59+
def __getitem__(self, index: Union[int, slice]) -> Union[T, Sequence[T]]:
6060
# This is exactly the same as before.
6161
if isinstance(index, int):
6262
... # Return a T here
@@ -65,6 +65,29 @@ accurately describe the function's behavior.
6565
else:
6666
raise TypeError(...)
6767
68+
Calls to overloaded functions are type checked against the variants,
69+
not against the implementation. A call like ``my_list[5]`` would have
70+
type ``T``, not ``Union[T, Sequence[T]]`` because it matches the
71+
first overloaded definition, and ignores the type annotations on the
72+
implementation of ``__getitem__``. The code in the body of the
73+
definition of ``__getitem__`` is checked against the annotations on
74+
the the corresponding declaration. In this case the body is checked
75+
with ``index: Union[int, slice]`` and a return type
76+
``Union[T, Sequence[T]]``. If there are no annotations on the
77+
corresponding definition, then code in the function body is not type
78+
checked.
79+
80+
The annotations on the function body must be compatible with the
81+
types given for the overloaded variants listed above it. The type
82+
checker will verify that all the types listed the overloaded variants
83+
are compatible with the types given for the implementation. In this
84+
case it checks that the parameter type ``int`` and the return type
85+
``T`` are compatible with ``Union[int, slice]`` and
86+
``Union[T, Sequence[T]]`` for the first variant. For the second
87+
variant it verifies that the parameter type ``slice`` are the return
88+
type ``Sequence[T]`` are compatible with ``Union[int, slice]`` and
89+
``Union[T, Sequence[T]]``.
90+
6891
Overloaded function variants are still ordinary Python functions and
6992
they still define a single runtime object. There is no automatic
7093
dispatch happening, and you must manually handle the different types

docs/source/kinds_of_types.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,9 +1159,16 @@ demonstrates how to work with coroutines. One version
11591159
and one
11601160
`uses yield from <https://github.com/python/mypy/blob/master/test-data/samples/crawl.py>`_.
11611161

1162+
.. _typeddict:
1163+
11621164
TypedDict
11631165
*********
11641166

1167+
.. note::
1168+
1169+
TypedDict is not yet an officially supported feature. It may not work reliably,
1170+
and details of TypedDict may change in future mypy releases.
1171+
11651172
Python programs often use dictionaries with string keys to represent objects.
11661173
Here is a typical example:
11671174

@@ -1255,11 +1262,6 @@ subtyping, as discussed below).
12551262
12561263
pip install --upgrade mypy-extensions
12571264
1258-
.. note::
1259-
1260-
TypedDict is experimental. Details of TypedDict may change in
1261-
future mypy releases.
1262-
12631265
Totality
12641266
--------
12651267

docs/source/revision_history.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ Revision history
33

44
List of major changes:
55

6+
- July 2017
7+
* Publish ``mypy`` version 0.520 on PyPI.
8+
9+
* Add :ref:`fine-grained control of Any types <disallow-any>`.
10+
11+
* Add :ref:`typeddict`.
12+
13+
* Other updates to :ref:`command-line`:
14+
15+
* Add ``--no-implicit-optional``.
16+
17+
* Add ``--shadow-file``.
18+
19+
* Add ``--no-incremental``.
20+
621
- May 2017
722
* Publish ``mypy`` version 0.510 on PyPI.
823

extensions/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from distutils.core import setup
66

7-
version = '0.3.0-dev'
7+
version = '0.3.0'
88
description = 'Experimental type system extensions for programs checked with the mypy typechecker.'
99
long_description = '''
1010
Mypy Extensions

0 commit comments

Comments
 (0)
0