Skip to content

Commit 8d5fc1e

Browse files
committed
Merge branch 'master' into normalize-type-union
2 parents f024e89 + 43dc4c3 commit 8d5fc1e

File tree

139 files changed

+5378
-10013
lines changed

Some content is hidden

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

139 files changed

+5378
-10013
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ python:
1515

1616
install:
1717
- pip install -r test-requirements.txt
18+
- python2 -m pip install --user typing
1819
- python setup.py install
1920

2021
script:

LICENSE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ DEALINGS IN THE SOFTWARE.
2727
= = = = =
2828

2929
Portions of mypy are licensed under different licenses. The files
30-
under stdlib-samples and lib-typing are licensed under the PSF 2
31-
License, reproduced below.
30+
under stdlib-samples are licensed under the PSF 2 License, reproduced below.
3231

3332
= = = = =
3433

README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ programs, even if they have type errors:
9797
[statically typed parts]: http://mypy.readthedocs.io/en/latest/basics.html#function-signatures
9898

9999

100+
IDE & Linter Integrations
101+
-------------------------
102+
103+
Mypy can be integrated into popular IDEs:
104+
105+
* Vim: [vim-mypy](https://github.com/Integralist/vim-mypy)
106+
* Emacs: using [Flycheck](https://github.com/flycheck/) and [Flycheck-mypy](https://github.com/lbolla/emacs-flycheck-mypy/issues)
107+
* Sublime Text: [SublimeLinter-contrib-mypy]
108+
* Atom: [linter-mypy](https://atom.io/packages/linter-mypy)
109+
* PyCharm: PyCharm integrates [its own implementation of PEP 484](https://www.jetbrains.com/help/pycharm/2017.1/type-hinting-in-pycharm.html).
110+
111+
Mypy can also be integrated into [Flake8] using [flake8-mypy].
112+
113+
[Flake8]: http://flake8.pycqa.org/
114+
[flake8-mypy]: https://github.com/ambv/flake8-mypy
115+
116+
100117
Web site and documentation
101118
--------------------------
102119

@@ -157,6 +174,15 @@ If you want to contribute, first clone the mypy git repository:
157174

158175
$ git clone --recurse-submodules https://github.com/python/mypy.git
159176

177+
If you've already cloned the repo without `--recurse-submodules`,
178+
you need to pull in the typeshed repo as follows:
179+
180+
$ git submodule init
181+
$ git submodule update
182+
183+
Either way you should now have a subdirectory `typeshed` containing a
184+
clone of the typeshed repo (`https://github.com/python/typeshed`).
185+
160186
From the mypy directory, use pip to install mypy:
161187

162188
$ cd mypy
@@ -170,10 +196,6 @@ the above as root. For example, in Ubuntu:
170196
Now you can use the `mypy` program just as above. In case of trouble
171197
see "Troubleshooting" above.
172198

173-
The mypy wiki contains some useful information for contributors:
174-
175-
https://github.com/python/mypy/wiki/Developer-Guides
176-
177199

178200
Working with the git version of mypy
179201
------------------------------------

ROADMAP.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Mypy Roadmap
2+
3+
The goal of the roadmap is to document areas the mypy core team is
4+
planning to work on in the future or is currently working on. PRs
5+
targeting these areas are very welcome, but please check first with a
6+
core team member that nobody else is working on the same thing.
7+
8+
**Note:** This doesn’t include everything that the core team will work
9+
on, and everything is subject to change. Near-term plans are likely
10+
more accurate.
11+
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))
37+
38+
- Document basic properties of all type operations used within mypy,
39+
including compatibility, proper subtyping, joins and meets.
40+
([issue](https://github.com/python/mypy/issues/3454))
41+
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.
48+
([issue](https://github.com/python/mypy/labels/topic-usability))
49+
50+
- Resolve [#2008](https://github.com/python/mypy/issues/2008) (we are
51+
converging on approach 4).
52+
53+
## July-December 2017
54+
55+
- Invest some effort into systematically filling in missing
56+
annotations and stubs in typeshed, with focus on features heavily
57+
used at Dropbox. Better support for ORMs will be a separate
58+
project.
59+
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+
66+
- Add support for protocols and structural subtyping (PEP 544).
67+
68+
- Switch completely to pytest and remove the custom testing framework.
69+
([issue](https://github.com/python/mypy/issues/1673))
70+
71+
- Make it possible to run mypy as a daemon to avoid reprocessing the
72+
entire program on each run. This will improve performance
73+
significantly. Even when using the incremental mode, processing a
74+
large number of files is not cheap.
75+
76+
- Refactor and simplify specific tricky parts of mypy internals, such
77+
as the [conditional type binder](https://github.com/python/mypy/issues/3457),
78+
[symbol tables](https://github.com/python/mypy/issues/3458) or
79+
the various [semantic analysis passes](https://github.com/python/mypy/issues/3459).
80+
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.
85+
([issue](https://github.com/python/mypy/issues/1240))
86+
87+
- Add support for statically typed
88+
[protobufs](https://developers.google.com/protocol-buffers/).
89+
90+
- Provide much faster, reliable interactive feedback through
91+
fine-grained incremental type checking, built on top the daemon
92+
mode.
93+
94+
- Start work on editor plugins and support for selected IDE features.
95+
96+
- Turn on `--strict-optional` by default.

appveyor.yml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
environment:
22
matrix:
33

4-
- PYTHON: "C:\\Python35"
5-
PYTHON_VERSION: "3.5.1"
6-
PYTHON_ARCH: "32"
7-
8-
- PYTHON: "C:\\Python35-x64"
9-
PYTHON_VERSION: "3.5.1"
10-
PYTHON_ARCH: "64"
11-
12-
- PYTHON: "C:\\Python36"
13-
PYTHON_VERSION: "3.6.x"
14-
PYTHON_ARCH: "32"
15-
164
- PYTHON: "C:\\Python36-x64"
175
PYTHON_VERSION: "3.6.x"
186
PYTHON_ARCH: "64"
197

20-
218
install:
9+
- "git config core.symlinks true"
10+
- "git reset --hard"
2211
- "%PYTHON%\\python.exe -m pip install -r test-requirements.txt"
2312
- "git submodule update --init typeshed"
13+
- "cd typeshed && git config core.symlinks true && git reset --hard && cd .."
2414
- "%PYTHON%\\python.exe setup.py -q install"
2515

2616
build: off
@@ -47,3 +37,5 @@ skip_commits:
4737
- .travis.yml
4838
- CREDITS
4939
- LICENSE
40+
41+
skip_branch_with_pr: true

conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
import os.path
2+
3+
import pytest
4+
15
pytest_plugins = [
26
'mypy.test.data',
37
]
8+
9+
10+
def pytest_configure(config):
11+
mypy_source_root = os.path.dirname(os.path.abspath(__file__))
12+
if os.getcwd() != mypy_source_root:
13+
os.chdir(mypy_source_root)

docs/source/cheat_sheet.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ When you're puzzled or when things are complicated
134134
# type: (*str, **str) -> str
135135
request = make_request(*args, **kwargs)
136136
return self.do_api_query(request)
137-
138137
139138
# Use `ignore` to suppress type-checking on a given line, when your
140139
# code confuses mypy or runs into an outright bug in mypy.
@@ -150,6 +149,18 @@ When you're puzzled or when things are complicated
150149
reveal_type(c) # -> error: Revealed type is 'builtins.list[builtins.str]'
151150
print(c) # -> [4] the object is not cast
152151
152+
# if you want dynamic attributes on your class, have it override __setattr__ or __getattr__
153+
# in a stub or in your source code.
154+
# __setattr__ allows for dynamic assignment to names
155+
# __getattr__ allows for dynamic access to names
156+
class A:
157+
# this will allow assignment to any A.x, if x is the same type as `value`
158+
def __setattr__(self, name, value):
159+
# type: (str, int) -> None
160+
...
161+
a.foo = 42 # works
162+
a.bar = 'Ex-parrot' # fails type checking
163+
153164
# TODO: explain "Need type annotation for variable" when
154165
# initializing with None or an empty container
155166

docs/source/cheat_sheet_py3.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ When you're puzzled or when things are complicated
122122
# dynamic to write a type for.
123123
x = mystery_function() # type: Any
124124
125+
# This is how to deal with varargs.
126+
# This makes each positional arg and each keyword arg a 'str'.
127+
def call(self, *args: str, **kwargs: str) -> str:
128+
request = make_request(*args, **kwargs)
129+
return self.do_api_query(request)
130+
125131
# Use `ignore` to suppress type-checking on a given line, when your
126132
# code confuses mypy or runs into an outright bug in mypy.
127133
# Good practice is to comment every `ignore` with a bug link
@@ -136,6 +142,19 @@ When you're puzzled or when things are complicated
136142
reveal_type(c) # -> error: Revealed type is 'builtins.list[builtins.str]'
137143
print(c) # -> [4] the object is not cast
138144
145+
# if you want dynamic attributes on your class, have it override __setattr__ or __getattr__
146+
# in a stub or in your source code.
147+
# __setattr__ allows for dynamic assignment to names
148+
# __getattr__ allows for dynamic access to names
149+
class A:
150+
# this will allow assignment to any A.x, if x is the same type as `value`
151+
def __setattr__(self, name: str, value: int) -> None: ...
152+
# this will allow access to any A.x, if x is compatible with the return type
153+
def __getattr__(self, name: str) -> int: ...
154+
a.foo = 42 # works
155+
a.bar = 'Ex-parrot' # fails type checking
156+
157+
139158
# TODO: explain "Need type annotation for variable" when
140159
# initializing with None or an empty container
141160

docs/source/command_line.rst

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ flag (or its long form ``--help``)::
1414
[--disallow-untyped-calls] [--disallow-untyped-defs]
1515
[--check-untyped-defs] [--disallow-subclassing-any]
1616
[--warn-incomplete-stub] [--warn-redundant-casts]
17-
[--warn-no-return] [--warn-unused-ignores] [--show-error-context]
18-
[-i] [--cache-dir DIR] [--strict-optional]
19-
[--strict-optional-whitelist [GLOB [GLOB ...]]] [--strict]
17+
[--no-warn-no-return] [--warn-return-any] [--warn-unused-ignores]
18+
[--show-error-context] [-i] [--quick-and-dirty] [--cache-dir DIR]
19+
[--strict-optional]
20+
[--strict-optional-whitelist [GLOB [GLOB ...]]]
2021
[--junit-xml JUNIT_XML] [--pdb] [--show-traceback] [--stats]
2122
[--inferstats] [--custom-typing MODULE]
2223
[--custom-typeshed-dir DIR] [--scripts-are-modules]
2324
[--config-file CONFIG_FILE] [--show-column-numbers]
24-
[--find-occurrences CLASS.MEMBER] [--strict-boolean]
25+
[--find-occurrences CLASS.MEMBER] [--strict] [--strict-boolean]
2526
[--cobertura-xml-report DIR] [--html-report DIR]
2627
[--linecount-report DIR] [--linecoverage-report DIR]
2728
[--memory-xml-report DIR] [--old-html-report DIR]
@@ -298,10 +299,31 @@ Here are some more useful flags:
298299
the base class even though that may not actually be the case. This
299300
flag makes mypy raise an error instead.
300301

301-
- ``--incremental`` is an experimental option that enables incremental
302-
type checking. When enabled, mypy caches results from previous runs
302+
.. _incremental:
303+
304+
- ``--incremental`` is an experimental option that enables a module
305+
cache. When enabled, mypy caches results from previous runs
303306
to speed up type checking. Incremental mode can help when most parts
304-
of your program haven't changed since the previous mypy run.
307+
of your program haven't changed since the previous mypy run. A
308+
companion flag is ``--cache-dir DIR``, which specifies where the
309+
cache files are written. By default this is ``.mypy_cache`` in the
310+
current directory. While the cache is only read in incremental
311+
mode, it is written even in non-incremental mode, in order to "warm"
312+
the cache. To disable writing the cache, use
313+
``--cache-dir=/dev/null`` (UNIX) or ``--cache-dir=nul`` (Windows).
314+
Cache files belonging to a different mypy version are ignored.
315+
316+
.. _quick-mode:
317+
318+
- ``--quick-and-dirty`` is an experimental, unsafe variant of
319+
:ref:`incremental mode <incremental>`. Quick mode is faster than
320+
regular incremental mode, because it only re-checks modules that
321+
were modified since their cache file was last written (regular
322+
incremental mode also re-checks all modules that depend on one or
323+
more modules that were re-checked). Quick mode is unsafe because it
324+
may miss problems caused by a change in a dependency. Quick mode
325+
updates the cache, but regular incremental mode ignores cache files
326+
written by quick mode.
305327

306328
- ``--python-version X.Y`` will make mypy typecheck your code as if it were
307329
run under Python version X.Y. Without this option, mypy will default to using
@@ -366,11 +388,6 @@ Here are some more useful flags:
366388
- ``--warn-return-any`` causes mypy to generate a warning when returning a value
367389
with type ``Any`` from a function declared with a non- ``Any`` return type.
368390

369-
- ``--strict-boolean`` will make using non-boolean expressions in conditions
370-
an error. This means ``if x`` and ``while x`` are disallowed when ``x`` has any
371-
type other than ``bool``. Instead use explicit checks like ``if x > 0`` or
372-
``while x is not None``.
373-
374391
- ``--strict`` mode enables all optional error checking flags. You can see the
375392
list of flags enabled by strict mode in the full ``mypy -h`` output.
376393

docs/source/common_issues.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ not support ``sort()``) as a list and sort it in-place:
180180
# Type of x is List[int] here.
181181
x.sort() # Okay!
182182
183-
.. _invariance-vs-covariance:
183+
.. _variance:
184184

185185
Invariance vs covariance
186186
------------------------

0 commit comments

Comments
 (0)