* Added object.__sizeof__
* Removed __sizeof__ inherited from object
* Made sqlite3 classes for Python 2 inherit from object
* Removed __sizeof__ inherited from object
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
**test_next.py**:
```python
z = (x*2 for x in range(10))
reveal_type(next(z, None))
```
Before:
```shell
test_next.py:2: error: Revealed type is 'builtins.int*'
```
After:
```shell
test_next.py:2: error: Revealed type is 'Union[builtins.int*, builtins.None]'
```
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*]'
```
* Define __slots__ for object as Iterable[str] / Iterable[Union[str, unicode]]
* A string as __slots__ value is also valid and represents a single item
```
$ python3.4
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> list(filter(None, [False, True]))
[True]
>>>
```
python/mypy#2380 showed a discrepancy between object and FunctionType in stdlib2. The first defined __doc__ to be str, the second Optional[str]. As FunctionType depends on object, this is no longer valid.
As suggested by @gvanrossum in python/mypy#2380, all __doc__ should be considered Optional.
(Final verdict was just to remove most __doc__ attributes since it's inherited from object.)
Reason: https://github.com/python/mypy/issues/328
* Revert "Make fromkeys() take an Iterable instead of a sequence. (#605)"
This reverts commit 66e8d4a14f.
* Revert "Make dict.fromkeys() a classmethod (which it is). (#604)"
This reverts commit 26dfcb6859.
* Add an explanation for fromkeys() being a staticmethod.
As of Python 2.7, you can pass in multiple iterables into some set
methods like union, intersection, difference, and update. So, for
example, `new_set = a.union(b, c)`.
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]