Modify the Python 3 stubs for requests to be Python 2-compatible

This works towards fixing #924. To make the changes easier to review, I'm planning to
fix the issue in two steps:
- This commit makes the 3 stubs compatible with py2
- The next PR will move the 3 stubs to 2and3 and remove the 2 stubs

I wrote this code by diffing the files in third_party/{2,3}/requests and making
appropriate changes to the Python 3 stubs (mostly str/Text stuff). I verified
that the 3 stubs now pass mypy when parsed as 2.7, but I don't have an annotated
requests-using codebase to test on.
This commit is contained in:
Jelle Zijlstra
2017-03-16 23:46:47 -07:00
committed by Łukasz Langa
parent da6d4314d3
commit 97e1ffff40
8 changed files with 53 additions and 47 deletions

View File

@@ -1,6 +1,6 @@
# Stubs for requests.adapters (Python 3)
from typing import Any, Container, Union, Tuple
from typing import Any, Container, Union, Text, Tuple
from . import models
from .packages.urllib3 import poolmanager
from .packages.urllib3 import response
@@ -47,7 +47,7 @@ class BaseAdapter:
def send(self, request: PreparedRequest, stream: bool=False,
timeout: Union[None, float, Tuple[float, float]]=None,
verify: bool=False,
cert: Union[None, Union[str, bytes], Container[Union[str, bytes]]]=None
cert: Union[None, Union[bytes, Text], Container[Union[bytes, Text]]]=None
) -> Response: ...
def close(self) -> None: ...
class HTTPAdapter(BaseAdapter):

View File

@@ -4,24 +4,23 @@ from typing import Optional, Union, Any, Iterable, Mapping, MutableMapping, Tupl
from .models import Response
_ParamsMappingValueType = Union[str, bytes, int, float, Iterable[Union[str, bytes, int, float]]]
_ParamsMappingValueType = Union[Text, bytes, int, float, Iterable[Union[Text, bytes, int, float]]]
_Data = Union[None, bytes, MutableMapping[Text, Text], IO]
def request(method: str, url: str, **kwargs) -> Response: ...
def get(url: Union[str, bytes],
def get(url: Union[Text, bytes],
params: Optional[
Union[
Mapping[Union[str, bytes, int, float], _ParamsMappingValueType],
Union[str, bytes],
Tuple[Union[str, bytes, int, float], _ParamsMappingValueType],
Mapping[str, _ParamsMappingValueType],
Mapping[bytes, _ParamsMappingValueType],
Mapping[int, _ParamsMappingValueType],
Mapping[float, _ParamsMappingValueType]]]=None,
Union[Mapping[Union[Text, bytes, int, float], _ParamsMappingValueType],
Union[Text, bytes],
Tuple[Union[Text, bytes, int, float], _ParamsMappingValueType],
Mapping[Text, _ParamsMappingValueType],
Mapping[bytes, _ParamsMappingValueType],
Mapping[int, _ParamsMappingValueType],
Mapping[float, _ParamsMappingValueType]]] = None,
**kwargs) -> Response: ...
def options(url: str, **kwargs) -> Response: ...
def head(url: str, **kwargs) -> Response: ...
def post(url: str, data: _Data = ..., json: Optional[MutableMapping] = ..., **kwargs) -> Response: ...
def put(url: str, data: _Data = ..., **kwargs) -> Response: ...
def patch(url: str, data: _Data = ..., **kwargs) -> Response: ...
def delete(url: str, **kwargs) -> Response: ...
def options(url: Union[str, Text], **kwargs) -> Response: ...
def head(url: Union[str, Text], **kwargs) -> Response: ...
def post(url: Union[str, Text], data: _Data = ..., json: Optional[MutableMapping] = ..., **kwargs) -> Response: ...
def put(url: Union[str, Text], data: _Data = ..., **kwargs) -> Response: ...
def patch(url: Union[str, Text], data: _Data = ..., **kwargs) -> Response: ...
def delete(url: Union[str, Text], **kwargs) -> Response: ...

View File

@@ -1,12 +1,14 @@
# Stubs for requests.cookies (Python 3)
import sys
from typing import Any, MutableMapping
# import cookielib
from http import cookiejar as cookielib
import collections
from . import compat
# cookielib = compat.cookielib
if sys.version_info < (3, 0):
from cookielib import CookieJar
else:
from http.cookiejar import CookieJar
class MockRequest:
type = ... # type: Any
@@ -39,7 +41,7 @@ def remove_cookie_by_name(cookiejar, name, domain=..., path=...): ...
class CookieConflictError(RuntimeError): ...
class RequestsCookieJar(cookielib.CookieJar, MutableMapping):
class RequestsCookieJar(CookieJar, MutableMapping):
def get(self, name, default=..., domain=..., path=...): ...
def set(self, name, value, **kwargs): ...
def iterkeys(self): ...

View File

@@ -1,6 +1,6 @@
# Stubs for requests.models (Python 3)
from typing import Any, List, MutableMapping, Iterator, Dict
from typing import Any, List, MutableMapping, Iterator, Dict, Text
import datetime
from . import hooks
@@ -80,10 +80,10 @@ class Request(RequestHooksMixin):
def prepare(self): ...
class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
method = ... # type: Optional[str]
url = ... # type: Optional[str]
method = ... # type: Optional[Union[str, Text]]
url = ... # type: Optional[Union[str, Text]]
headers = ... # type: CaseInsensitiveDict
body = ... # type: Optional[Union[str, bytes]]
body = ... # type: Optional[Union[bytes, Text]]
hooks = ... # type: Any
def __init__(self) -> None: ...
def prepare(self, method=..., url=..., headers=..., files=..., data=..., params=...,

View File

@@ -1,21 +1,28 @@
# Stubs for requests.packages.urllib3.connection (Python 3.4)
import sys
from typing import Any
from . import packages
from http.client import HTTPConnection as _HTTPConnection
import ssl
# from httplib import HTTPConnection as _HTTPConnection # python 2
from . import exceptions
from .packages import ssl_match_hostname
from .util import ssl_
from . import util
import http.client
if sys.version_info <= (3, 0):
from httplib import HTTPConnection as _HTTPConnection
from httplib import HTTPException as HTTPException
class ConnectionError(Exception): ...
else:
from http.client import HTTPConnection as _HTTPConnection
from http.client import HTTPException as HTTPException
from builtins import ConnectionError as ConnectionError
class DummyConnection: ...
BaseSSLError = ssl.SSLError
ConnectionError = __builtins__.ConnectionError
HTTPException = http.client.HTTPException
ConnectTimeoutError = exceptions.ConnectTimeoutError
SystemTimeWarning = exceptions.SystemTimeWarning
@@ -61,4 +68,4 @@ class VerifiedHTTPSConnection(HTTPSConnection):
is_verified = ... # type: Any
def connect(self): ...
UnverifiedHTTPSConnection = ... # type: Any
UnverifiedHTTPSConnection = HTTPSConnection

View File

@@ -2,11 +2,9 @@
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.
from typing import Any
import io
from typing import Any, IO
from . import _collections
from . import exceptions
# from .packages import six
from .connection import HTTPException as HTTPException, BaseSSLError as BaseSSLError
from .util import response
@@ -28,7 +26,7 @@ class GzipDecoder:
def __getattr__(self, name): ...
def decompress(self, data): ...
class HTTPResponse(io.IOBase):
class HTTPResponse(IO[Any]):
CONTENT_DECODERS = ... # type: Any
REDIRECT_STATUSES = ... # type: Any
headers = ... # type: Any

View File

@@ -92,19 +92,19 @@ class Session(SessionRedirectMixin):
cert: Union[Text, Tuple[Text, Text], None] = ...,
json: Optional[MutableMapping] = ...,
) -> Response: ...
def get(self, url: Union[str, bytes], **kwargs) -> Response: ...
def options(self, url: Union[str, bytes], **kwargs) -> Response: ...
def head(self, url: Union[str, bytes], **kwargs) -> Response: ...
def post(self, url: Union[str, bytes], data: _Data = ..., json: Optional[MutableMapping] = ..., **kwargs) -> Response: ...
def put(self, url: Union[str, bytes], data: _Data = ..., **kwargs) -> Response: ...
def patch(self, url: Union[str, bytes], data: _Data = ..., **kwargs) -> Response: ...
def delete(self, url: Union[str, bytes], **kwargs) -> Response: ...
def get(self, url: Union[Text, bytes], **kwargs) -> Response: ...
def options(self, url: Union[Text, bytes], **kwargs) -> Response: ...
def head(self, url: Union[Text, bytes], **kwargs) -> Response: ...
def post(self, url: Union[Text, bytes], data: _Data = ..., json: Optional[MutableMapping] = ..., **kwargs) -> Response: ...
def put(self, url: Union[Text, bytes], data: _Data = ..., **kwargs) -> Response: ...
def patch(self, url: Union[Text, bytes], data: _Data = ..., **kwargs) -> Response: ...
def delete(self, url: Union[Text, bytes], **kwargs) -> Response: ...
def send(self, request, **kwargs): ...
def merge_environment_settings(self, url, proxies, stream, verify, cert): ...
def get_adapter(self, url): ...
def close(self) -> None: ...
def mount(self, prefix:
Union[str, bytes],
Union[Text, bytes],
adapter: BaseAdapter) -> None: ...
def session() -> Session: ...

View File

@@ -1,9 +1,9 @@
# Stubs for requests.structures (Python 3)
from typing import Any, Iterator, MutableMapping, Tuple, Union
from typing import Any, Iterator, MutableMapping, Text, Tuple, Union
class CaseInsensitiveDict(MutableMapping[str, Union[str, bytes]]):
def lower_items(self) -> Iterator[Tuple[str, Union[str, bytes]]]: ...
class CaseInsensitiveDict(MutableMapping[str, Union[Text, bytes]]):
def lower_items(self) -> Iterator[Tuple[str, Union[Text, bytes]]]: ...
class LookupDict(dict):
name = ... # type: Any