Skip to content

Commit feb20fc

Browse files
authored
urllib.request: loosen data type (#7211)
Fixes #7208 This is what urllib.request claims to support over here: https://github.com/python/cpython/blob/ad4e8d2b871c4f5bce27520627bbb2e0e544bc24/Lib/urllib/request.py#L1276 There is additional validation logic (e.g. strs don't work), but a lot of what determines what works is over here: https://github.com/python/cpython/blob/ad4e8d2b871c4f5bce27520627bbb2e0e544bc24/Lib/http/client.py#L1039
1 parent e976ee1 commit feb20fc

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

stdlib/urllib/request.pyi

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import ssl
22
import sys
3-
from _typeshed import StrOrBytesPath
3+
from _typeshed import StrOrBytesPath, SupportsRead
44
from email.message import Message
55
from http.client import HTTPMessage, HTTPResponse, _HTTPConnectionProtocol
66
from http.cookiejar import CookieJar
7-
from typing import IO, Any, Callable, ClassVar, Mapping, MutableMapping, NoReturn, Pattern, Sequence, TypeVar, overload
7+
from typing import IO, Any, Callable, ClassVar, Iterable, Mapping, MutableMapping, NoReturn, Pattern, Sequence, TypeVar, overload
88
from urllib.error import HTTPError
99
from urllib.response import addclosehook, addinfourl
1010

1111
_T = TypeVar("_T")
1212
_UrlopenRet = Any
13+
_DataType = bytes | SupportsRead[bytes] | Iterable[bytes] | None
1314

1415
def urlopen(
1516
url: str | Request,
16-
data: bytes | None = ...,
17+
data: _DataType | None = ...,
1718
timeout: float | None = ...,
1819
*,
1920
cafile: str | None = ...,
@@ -51,7 +52,7 @@ class Request:
5152
host: str
5253
origin_req_host: str
5354
selector: str
54-
data: bytes | None
55+
data: _DataType
5556
headers: MutableMapping[str, str]
5657
unredirected_hdrs: dict[str, str]
5758
unverifiable: bool
@@ -60,7 +61,7 @@ class Request:
6061
def __init__(
6162
self,
6263
url: str,
63-
data: bytes | None = ...,
64+
data: _DataType = ...,
6465
headers: MutableMapping[str, str] = ...,
6566
origin_req_host: str | None = ...,
6667
unverifiable: bool = ...,
@@ -83,7 +84,7 @@ class Request:
8384
class OpenerDirector:
8485
addheaders: list[tuple[str, str]]
8586
def add_handler(self, handler: BaseHandler) -> None: ...
86-
def open(self, fullurl: str | Request, data: bytes | None = ..., timeout: float | None = ...) -> _UrlopenRet: ...
87+
def open(self, fullurl: str | Request, data: _DataType = ..., timeout: float | None = ...) -> _UrlopenRet: ...
8788
def error(self, proto: str, *args: Any) -> _UrlopenRet: ...
8889
def close(self) -> None: ...
8990

@@ -242,7 +243,7 @@ def urlretrieve(
242243
url: str,
243244
filename: StrOrBytesPath | None = ...,
244245
reporthook: Callable[[int, int, int], None] | None = ...,
245-
data: bytes | None = ...,
246+
data: _DataType = ...,
246247
) -> tuple[str, HTTPMessage]: ...
247248
def urlcleanup() -> None: ...
248249

0 commit comments

Comments
 (0)