Change `str` to `Union[str, unicode]` where it seems safe.
I didn't change it in non-covariant contexts since that can
break user code.
I manually verified a subset of the changes by calling the
relevant function with unicode literals (ascii-only).
I'm using `Union[str, unicode]` instead of just `unicode`
so that the stub still works with tools that don't promote
`str` objects to `unicode`.
This type is something core to Python and is useful when typing web applications,
but doesn't actually exist in the stdlib anywhere. I put this in wsgiref, but I am
open to suggestions as for a better place.
(Original PR by @rowillia.)
**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]'
```
Also adds the bare minimum of stubs for
cryptography.hazmat.primitives.asymmetric to define the types
referenced here. (cryptography is a full project in its own right,
with lots of types and internal references.)
This tries to use bytes in places where the module documentation
emphasized that this was opaque bytes and str for arguments and
return values that the module seemd to be treating as regular Python
strings, even though this distinction is not horribly meaningful for
Python 2.
The python2 unittest `assertIsInstance` and `assertNotIsInstance` stub
did not allow using a tuple of classes, but that behavior is [in the
documentation][1]. This commit copies the type stub for the isinstance
built-in to the stubs for `assertIsInstance` and `assertNotIsInstance`
I made this commit in response to @gvanrossum's request in
python/typeshed#802 (which fixed this issue for python 3) that I apply
the same fix to python 2.
[1]: https://docs.python.org/2.7/library/unittest.html#unittest.TestCase.assertNotIsInstance
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*]'
```
1. Mentioned review requirement for core contributors.
2. Made stub coding style on par with requirements specified on
the Mypy wiki (we can merge them now).
According to the documentation in the typing module, TypeVars cannot
have only a single constraint. Attempting to do so will actually result
in an exception at runtime. (However, this error is currently ignored
by mypy -- see https://github.com/python/mypy/pull/2626 for a related
pending pull request).
This commit changes all instances of TypeVars using a single constraint
(e.g. `T = TypeVar('T', Foo)`) to use bounds instead (e.g.
`T = TypeVar('T', bound=Foo)`.
This seems to be the correct fix for plistlib after reading the module
docs, but it's less obvious this is correct for unittest. The unittest
module originally had `_FT = TypeVar('_FT', Callable[[Any], Any])` -- an
alternative fix would have been to do `_FT = Callable[[Any], Any]`.
Although I'm not entirely sure what it means to have a bound be a
Callable, I decided to make the assumption that the original authors
probably meant to use TypeVars instead of type aliases for a reason
(possibly to handle classes implementing `__call__`?)
* Add support for request.get's 'params' param
Requests defines the following API:
`get(url, params=None, **kwargs)`
* Improve typing for requests.get(params)
Add support for string form, and tighten restrictions for the dict form
to allow only string keys/vals. Technically, anything is allowed since
the code (I guess) runs `str(key)` and `str(value)`, but it seems better
to keep the stub somewhat strict so it can help pick up potential
errors.
Improve operator methods for dateutil.relativedelta stubs:
* `__add__` operator method could return other types than `relativedelta` (`datetime.date` or `datetime.datetime`)
* use specific types of operators args instead of Any
* mypy currently does not handle `Union` in op methods (see python/mypy#2129, python/mypy#1442, python/mypy#1264 for details), so I've overloaded it directly
Starting with python/mypy#2521 mypy is performing stricter function signature
checks.
This makes the stubs diverge from the actual implementation but makes the stubs
internally consistent. Since this is an actual typing issue in the base
implementation, we need to defer to the original authors to fix it.
Sadly, in this case the breakage is rather fundamental and unlikely to get
fixed by upstream. Consider:
```
class AWSAuthConnection(object):
def make_request(self, method, path, headers=None, data='', host=None,
auth_path=None, sender=None, override_num_retries=None,
params=None, retry_handler=None): ...
class AWSQueryConnection(AWSAuthConnection):
def make_request(self, action, params=None, path='/', verb='GET'): ...
```
Hence, until we have a workaround for the error produced by Mypy, we're
excluding those stubs from being tested against.