Commit Graph

49 Commits

Author SHA1 Message Date
Jukka Lehtosalo f2579e532f Update signature of dict.get (#852)
Without this change, mypy can't infer proper types for cases like
`d.get(k, [])` where it needs type context to infer the type of
`[]`. We add the value type to the second argument to `get` using
union types, and this provides the context. This doesn't affect
the effective signature of `get`, other than providing the type
context for mypy.

Also removed some related redundant method definitions where we can
just inherit the base class definition. This makes it easier to
keep the method signatures consistent.

Note that this requires a few mypy PRs before mypy will be able to
use this effectively:

* https://github.com/python/mypy/pull/2718
* https://github.com/python/mypy/pull/2715
2017-01-20 07:41:54 -08:00
Jelle Zijlstra b96bd698ab Fix return values of athrow and aclose. (#845)
I misread the PEP here (https://www.python.org/dev/peps/pep-0525/#asynchronous-generator-object); both of these return the yielded type.
2017-01-19 12:21:51 -08:00
Jelle Zijlstra df9d11bf71 add AsyncGenerator to typing.pyi, collections/abc.pyi and collections/__init__.pyi (#815)
This parallels https://github.com/python/typing/pull/346
2017-01-18 13:24:28 -08:00
Roy Williams 6008b9dbb1 Overload signature of get to return an Optional value and to allow default to take any type to match runtime behavior.
This chage more closely matches the behavior of `get` at runtime.  Users can pass whatever they want in to the default
parameter and it will be returned if the key is absent.  Additionally, `get` should return an `Optional` if called with
only one parameter.

```python
z = {'a': 22}
reveal_type(z.get('b'))
reveal_type(z.get('b', 22))
reveal_type(z.get('b', 'hello'))
```

Before:
```shell
test_get_default.py:2: error: Revealed type is 'builtins.int*'
test_get_default.py:3: error: Revealed type is 'builtins.int*'
test_get_default.py:4: error: Revealed type is 'builtins.int*'
test_get_default.py:4: error: Argument 2 to "get" of "dict" has incompatible type "str"; expected "int"
```

After:
```shell
test_get_default.py:2: error: Revealed type is 'Union[builtins.int*, builtins.None]'
test_get_default.py:3: error: Revealed type is 'builtins.int'
test_get_default.py:4: error: Revealed type is 'Union[builtins.int, builtins.str*]'
```
2017-01-11 22:02:29 -08:00
Lukasz Langa 67e38b6806 Fixing flake8 E231 errors 2016-12-19 23:53:19 -08:00
Lukasz Langa fe0e3744cc Fixing flake8 E261 errors 2016-12-19 22:09:35 -08:00
Lukasz Langa b84f20a011 Fixing flake8 W errors 2016-12-19 21:52:56 -08:00
Guido van Rossum b46280ec78 Make FrozenSet covariant. (#769)
Follow-up for PR #726.
2016-12-13 11:40:18 -08:00
Calen Pennington 5ba7abc559 Add GenericMeta (so that classes can be Generic when they already hav… (#703) 2016-12-02 12:09:59 -08:00
Guido van Rossum ec4987fa5d Fix AwaitableGenerator inheritance; Awaitable is the return value, not the yielded value (#679) 2016-11-10 11:57:31 -08:00
TrueBrain cecb64b59f In TextIO, 'errors' is Optional (#665) 2016-11-05 15:02:35 -07:00
Guido van Rossum 53014497c4 Add Coroutine. Fix signature for Generator.throw(). (#656) 2016-11-04 11:46:50 -07:00
Elazar 7224b67532 Remove @builtinclass (#626) 2016-10-22 12:25:02 -07:00
Elazar 1d820d48cf Add signatures for cast() and NewType (#549)
(Mypy ignores these in favor of hard-coded behaviors, but the signatures here may be useful for other tools.)
2016-09-14 14:55:06 -07:00
Elazar e25b882041 Full signature for namedtuple (#541) 2016-09-13 16:17:26 -07:00
Guido van Rossum 8b0e6b886d Fix signature for __exit__ (#542) 2016-09-13 11:00:01 -07:00
Guido van Rossum 25cb6e4ab4 Make Mapping covariant. (#512)
* Make Mapping covariant.

Fixes #510.

This requires a `# type: ignore` on `__getitem__` and `get` because
mypy complains about covariant parameters.

FWIW typing.py needs to be changed too.  (It was covariant in the
value but invariant in the key -- typeshed was invariant in both.)

* Delete outdated comment.

* Backpeddle a bit -- Mapping key type should not be covariant.
2016-09-05 20:57:37 +01:00
Michael Lee 97bc450acd Make typing.IO inherit Iterator, not Iterable (#502)
In Python, it's possible to use the `next` builtin method on file
objects produced by `open`. This change modifies `typing.IO` so this
usage will successfully typecheck.
2016-08-27 23:31:16 -07:00
Tomasz Elendt 34175b888d Fix __setitem__(slice, ...) of various mutable sequences (#496)
Adds support of assigning iterables to slice of mutable sequences and
bytes to bytearray's slice.
2016-08-23 19:02:50 -07:00
Tomasz Elendt 9451fa922c Add start and stop arguments to Sequence.index in Py >= 3.5 (#489)
Fixes #486.
2016-08-22 14:53:48 -07:00
Nicholas Bishop a20d5c481a Add type stub for typing.get_type_hints (#480)
Fixes issue #477
2016-08-16 20:41:01 -07:00
Emanuel Barry 0989cb1cb3 Add FrozenSet to the typing stub files (#474) 2016-08-13 19:34:38 -07:00
Guido van Rossum de4e87f574 Changes required by mypy async-await support (#435) 2016-08-03 17:01:35 -07:00
Jakub Stasiak 44b367c7fe Add re fullmatch() information (#381) 2016-07-26 07:43:29 -07:00
Michael Lee 9ecc1f2147 Add stubs for NewType helper function (#398)
(Note that this is not enough to support NewType(). However it's a prerequisite for the mypy implementation.)
2016-07-26 07:35:28 -07:00
Michael Lee 368c703078 Add dict views to python 2 (#376) 2016-07-21 11:28:35 -07:00
Guido van Rossum 89dfe15008 Add TYPE_CHECKING = True 2016-06-10 17:27:12 -07:00
Scott G. Ainsworth 82b9baed3b Changed all regular expression findall() to return list[Any]. Findall() can return both list[AnyStr] and list[Tuple[AnyStr, AnyStr]]. (#269) 2016-06-05 17:50:12 -07:00
Guido van Rossum 25a45d6daf Revert "WIP: Mapping.{get,pop} can return default type (#223)"
This reverts commit d43adbe97e.

Here's a simple example of code that breaks with this PR:

from typing import Mapping, Dict, Tuple
a = {('0', '0'): 42}  # type: Mapping[Tuple[str, str], int]
b = a.get(('1', '1'), 0)

This gives an error on the last line:
error: No overload variant of "get" of "dict" matches argument types [Tuple[builtins.str, builtins.str], builtins.int]
2016-05-30 11:55:30 -07:00
Valérian Rousset d43adbe97e WIP: Mapping.{get,pop} can return default type (#223)
* Mapping.{get,pop} can return default type

* Mapping values are covariant
2016-05-30 10:05:44 -07:00
Guido van Rossum b6b8554c6b Add stub version of Type[C] (#216) 2016-05-24 16:44:13 -07:00
detlefla e2ce50b525 enable string arguments for start, end, and span methods of Match object 2016-05-07 09:53:24 -07:00
rwbarton 2b80cdf75e Use overloading rather than Union for MutableMapping.update (#174)
See https://github.com/python/mypy/issues/1430 for motivation.
2016-05-01 17:50:01 +01:00
Guido van Rossum 78e6e9b740 Add Text to typing.pyi; it's an alias for unicode in 2.7, for str in 3. 2016-04-16 13:42:17 -07:00
Guido van Rossum 23ec1ff96a Break cycle between typing and collections. (Requires a fix in mypy.) 2016-04-15 21:31:49 -07:00
Ivan Levkivskyi db3cc2e8da Enable typing.DefaultDict as an alias for collections.defaultdict (#139) 2016-04-09 09:32:20 -07:00
David Fisher 9f4a8d3df6 Address review feedback 2016-03-11 16:01:30 -08:00
David Fisher d390ef3161 Make all function annotations accessible from builtins complete 2016-03-11 14:27:28 -08:00
David Fisher 7c8797441b Make Generator __iter__ method return itself 2016-01-19 16:49:20 -08:00
Guido van Rossum 5463335be2 Make deque inherit from MutableMapping (with difficulty). Fixes #43. 2016-01-14 09:14:38 -08:00
Guido van Rossum 8539624f05 Fix Reversible.__reversed__() return type. (I should do more testing.) 2016-01-11 16:04:44 -08:00
Guido van Rossum 1c02f14dc1 Make Reversible covariant. Fixes #19. (Though it does not seem to be *necessary*.) 2016-01-11 15:32:43 -08:00
Guido van Rossum 0f21b59a30 Some updates now typing.Container is defined. 2016-01-06 16:38:14 -08:00
Guido van Rossum a4c268c4e4 The typing stub should not import asyncio. Fixes https://github.com/JukkaL/mypy/issues/1050 . 2015-12-05 14:22:45 -08:00
Roy Williams 7f22db0492 Add Async classes to typing stub. 2015-12-01 23:36:19 -08:00
Matthias Kramm 94c9ce8fd0 Consistently use '= ...' for optional parameters. 2015-11-09 13:55:02 -08:00
Matthias Kramm 4fe8915d44 Add missing '-> None' to all __init__ methods. 2015-11-09 10:25:11 -08:00
Ben Longbons f2a12774ee Use SupportsBytes 2015-10-12 10:45:04 -07:00
Matthias Kramm 337abed05a add (overwrite with) mypy stubs, if available 2015-09-30 09:59:44 -07:00