Add stubs for HTTP Handler classes in py2/urllib2 & py3/urllib.request (#2710)

* HTTP Handler class annotations for py2/urllib2 & py3/urllib.request

Add full annotations for the following classes:

* Python 2:

    * `urllib2.AbstractHTTPHandler`
    * `urllib2.HTTPHandler`
    * `urllib2.HTTPsHandler`

* Python 3:

    * `urllib.request.AbstractHTTPHandler`
    * `urllib.request.HTTPHandler`
    * `urllib.request.HTTPsHandler`

This information is largely undocumented, and was obtained by directly examining
the Python source code:

* Python 2 (v2.7.15) - https://github.com/python/cpython/blob/v2.7.15/Lib/urllib2.py#L1115-L1243
* Python 3 (v3.7.1) - https://github.com/python/cpython/blob/v3.7.1/Lib/urllib/request.py#L1224-L1364

`urllib2.AbstractHTTPHandler.do_open` takes as a parameter either
`HTTPConnection` or `HTTPSConnection`--one of the classes, not an instance of
either--and constructs an object using only a few of the parameters that either
constructor could use. `HTTPConnectionProtocol` in `stdlib/2/httplib.pyi`
follows a similar patten to `HTTPConnectionProtocol` added to
`stdlib/3/http/client.pyi` in pull request #2582 to describe the type of the
`http_class` that is passed to `do_open`.
This commit is contained in:
Michael Brandt
2019-02-11 03:25:02 -07:00
committed by Sebastian Rittau
parent f8612a77bb
commit 1442cc02bf
3 changed files with 38 additions and 16 deletions

View File

@@ -3,8 +3,9 @@
# Generated by stubgen and manually massaged a bit.
# Needs lots more work!
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Protocol
import mimetools
import ssl
class HTTPMessage(mimetools.Message):
def addcontinue(self, key: str, more: str) -> None: ...
@@ -38,6 +39,15 @@ class HTTPResponse:
def getheader(self, name, default: Optional[Any] = ...): ...
def getheaders(self): ...
# This is an API stub only for HTTPConnection and HTTPSConnection, as used in
# urllib2.AbstractHTTPHandler.do_open, which takes either the class
# HTTPConnection or the class HTTPSConnection, *not* an instance of either
# class. do_open does not use all of the parameters of HTTPConnection.__init__
# or HTTPSConnection.__init__, so HTTPConnectionProtocol only implements the
# parameters that do_open does use.
class HTTPConnectionProtocol(Protocol):
def __call__(self, host: str, timeout: int = ..., **http_con_args: Any) -> HTTPConnection: ...
class HTTPConnection:
response_class = ... # type: Any
default_port = ... # type: Any