Skip to content

Commit a9de04c

Browse files
committed
Merge branch 'master' into generic-classmethod-type
2 parents e59c7ae + 402d734 commit a9de04c

Some content is hidden

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

79 files changed

+3069
-1129
lines changed

.travis.yml

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,46 @@
1-
sudo: false
1+
if: tag IS present OR type = pull_request OR (branch = master AND type = push) # we only CI the master, tags and PRs
2+
23
language: python
34
# cache package wheels (1 cache per python version)
45
cache: pip
5-
python:
6-
- "3.4"
6+
# newer python versions are available only on xenial (while some older only on trusty) Ubuntu distribution
7+
dist: xenial
8+
sudo: required
9+
10+
env:
11+
TOXENV=py
12+
EXTRA_ARGS="-n 12"
13+
14+
jobs:
15+
include:
16+
- name: "run test suite with python 3.4"
17+
python: 3.4
18+
dist: trusty
719
# Specifically request 3.5.1 because we need to be compatible with that.
8-
- "3.5.1"
9-
- "3.6"
10-
- "3.7-dev"
20+
- name: "run test suite with python 3.5.1"
21+
python: 3.5.1
22+
dist: trusty
23+
- name: "run test suite with python 3.6"
24+
python: 3.6 # 3.6.3 pip 9.0.1
25+
- name: "run test suite with python 3.7"
26+
python: 3.7 # 3.7.0 pip 10.0.1
27+
- name: "run test suite with python 3.8-dev"
28+
python: 3.8-dev
29+
- name: "type check our own code"
30+
python: 3.7
31+
env:
32+
- TOXENV=type
33+
- EXTRA_ARGS=
34+
- name: "check code style with flake8"
35+
python: 3.7
36+
env:
37+
- TOXENV=lint
38+
- EXTRA_ARGS=
1139

1240
install:
13-
- pip install -U pip setuptools wheel
14-
- pip install -r test-requirements.txt
15-
- python2 -m pip install --user typing
16-
- pip install .
41+
- pip install -U pip setuptools
42+
- pip install -U tox
43+
- tox --notest
1744

1845
script:
19-
- pytest -n12
20-
- if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then flake8 -j12; fi
21-
- if [[ $TRAVIS_PYTHON_VERSION == '3.5.1' ]]; then python3 -m mypy --config-file mypy_self_check.ini -p mypy; fi
46+
- tox -- $EXTRA_ARGS

appveyor.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@ cache:
44
environment:
55
matrix:
66

7-
- PYTHON: "C:\\Python36-x64"
8-
PYTHON_VERSION: "3.6.x"
7+
- PYTHON: "C:\\Python37-x64"
8+
PYTHON_VERSION: "3.7.x"
99
PYTHON_ARCH: "64"
1010

1111
install:
1212
- "git config core.symlinks true"
1313
- "git reset --hard"
14-
- "%PYTHON%\\python.exe -m pip install -r test-requirements.txt"
15-
- "C:\\Python27\\python.exe -m pip install typing"
1614
- "git submodule update --init typeshed"
1715
- "cd typeshed && git config core.symlinks true && git reset --hard && cd .."
18-
- "%PYTHON%\\python.exe -m pip install ."
16+
- "%PYTHON%\\python.exe -m pip install -U setuptools tox"
17+
- "%PYTHON%\\python.exe -m tox -e py37 --notest"
1918

2019
build: off
2120

2221
test_script:
23-
# lint and mypy self check run in Travis
24-
- "%PYTHON%\\python.exe -m pytest -k \"not (PythonEvaluationSuite and python2)\""
22+
- "%PYTHON%\\python.exe -m tox -e py37"
2523

2624
skip_commits:
2725
files:

docs/source/builtin_types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ strings and and ``Dict[Any, Any]`` is a dictionary of dynamically typed
3535
correspond to Python protocols. For example, a ``str`` object or a
3636
``List[str]`` object is valid
3737
when ``Iterable[str]`` or ``Sequence[str]`` is expected. Note that even though
38-
they are similar to abstract base classes defined in ``abc.collections``
38+
they are similar to abstract base classes defined in ``collections.abc``
3939
(formerly ``collections``), they are not identical, since the built-in
4040
collection type objects do not support indexing.

docs/source/command_line.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ For more details, see :ref:`no_strict_optional`.
273273

274274
For example, by default mypy will assume that the ``x`` parameter
275275
is of type ``Optional[int]`` in the code snippet below since
276-
the default pararameter is ``None``:
276+
the default parameter is ``None``:
277277

278278
.. code-block:: python
279279

docs/source/generics.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ Generic protocols can also be recursive. Example:
653653
654654
result = last(L()) # Inferred type of 'result' is 'int'
655655
656+
.. _generic-type-aliases:
657+
656658
Generic type aliases
657659
********************
658660

docs/source/kinds_of_types.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ Any)`` function signature. Example:
186186
arbitrary_call(open) # Error: does not return an int
187187
arbitrary_call(1) # Error: 'int' is not callable
188188
189+
In situations where more precise or complex types of callbacks are
190+
necessary one can use flexible :ref:`callback protocols <callback_protocols>`.
189191
Lambdas are also supported. The lambda argument and return value types
190192
cannot be given explicitly; they are always inferred based on context
191193
using bidirectional type inference:
@@ -546,7 +548,8 @@ assigning the type to a variable:
546548
.. note::
547549

548550
A type alias does not create a new type. It's just a shorthand notation for
549-
another type -- it's equivalent to the target type.
551+
another type -- it's equivalent to the target type except for
552+
:ref:`generic aliases <generic-type-aliases>`.
550553

551554
.. _named-tuples:
552555

docs/source/protocols.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,54 @@ in ``typing`` such as ``Iterable``.
409409
``isinstance()`` with protocols is not completely safe at runtime.
410410
For example, signatures of methods are not checked. The runtime
411411
implementation only checks that all protocol members are defined.
412+
413+
.. _callback_protocols:
414+
415+
Callback protocols
416+
******************
417+
418+
Protocols can be used to define flexible callback types that are hard
419+
(or even impossible) to express using the ``Callable[...]`` syntax, such as variadic,
420+
overloaded, and complex generic callbacks. They are defined with a special ``__call__``
421+
member:
422+
423+
.. code-block:: python
424+
425+
from typing import Optional, Iterable, List
426+
from typing_extensions import Protocol
427+
428+
class Combiner(Protocol):
429+
def __call__(self, *vals: bytes, maxlen: Optional[int] = None) -> List[bytes]: ...
430+
431+
def batch_proc(data: Iterable[bytes], cb_results: Combiner) -> bytes:
432+
for item in data:
433+
...
434+
435+
def good_cb(*vals: bytes, maxlen: Optional[int] = None) -> List[bytes]:
436+
...
437+
def bad_cb(*vals: bytes, maxitems: Optional[int]) -> List[bytes]:
438+
...
439+
440+
batch_proc([], good_cb) # OK
441+
batch_proc([], bad_cb) # Error! Argument 2 has incompatible type because of
442+
# different name and kind in the callback
443+
444+
Callback protocols and ``Callable[...]`` types can be used interchangeably.
445+
Keyword argument names in ``__call__`` methods must be identical, unless
446+
a double underscore prefix is used. For example:
447+
448+
.. code-block:: python
449+
450+
from typing import Callable, TypeVar
451+
from typing_extensions import Protocol
452+
453+
T = TypeVar('T')
454+
455+
class Copy(Protocol):
456+
def __call__(self, __origin: T) -> T: ...
457+
458+
copy_a: Callable[[T], T]
459+
copy_b: Copy
460+
461+
copy_a = copy_b # OK
462+
copy_b = copy_a # Also OK

docs/source/running_mypy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Missing imports
131131
When you import a module, mypy may report that it is unable to
132132
follow the import.
133133

134-
This could happen if the code is importing a non-existant module
134+
This could happen if the code is importing a non-existent module
135135
or if the code is importing a library that does not use type hints.
136136
Specifically, the library is neither declared to be a
137137
:ref:`PEP 561 compliant package <installed-packages>` nor has registered

extensions/mypy_extensions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,21 @@ class NoReturn: pass
139139

140140
def trait(cls):
141141
return cls
142+
143+
144+
# TODO: We may want to try to properly apply this to any type
145+
# variables left over...
146+
class _FlexibleAliasClsApplied:
147+
def __init__(self, val):
148+
self.val = val
149+
150+
def __getitem__(self, args):
151+
return self.val
152+
153+
154+
class _FlexibleAliasCls:
155+
def __getitem__(self, args):
156+
return _FlexibleAliasClsApplied(args[-1])
157+
158+
159+
FlexibleAlias = _FlexibleAliasCls()

extensions/setup.py

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

33
from setuptools import setup
44

5-
version = '0.4.0-dev'
5+
version = '0.5.0-dev'
66
description = 'Experimental type system extensions for programs checked with the mypy typechecker.'
77
long_description = '''
88
Mypy Extensions

mypy/bogus_type.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""A Bogus[T] type alias for marking when we subvert the type system
2+
3+
We need this for compiling with mypyc, which inserts runtime
4+
typechecks that cause problems when we subvert the type system. So
5+
when compiling with mypyc, we turn those places into Any, while
6+
keeping the types around for normal typechecks.
7+
8+
Since this causes the runtime types to be Any, this is best used
9+
in places where efficient access to properties is not important.
10+
For those cases some other technique should be used.
11+
"""
12+
13+
from mypy_extensions import FlexibleAlias
14+
from typing import TypeVar, Any
15+
16+
T = TypeVar('T')
17+
18+
SUPPRESS_BOGUS_TYPES = False
19+
if SUPPRESS_BOGUS_TYPES:
20+
Bogus = FlexibleAlias[T, Any]
21+
else:
22+
Bogus = FlexibleAlias[T, T]

0 commit comments

Comments
 (0)