Skip to content

Commit ea1efef

Browse files
authored
Improve http types (#48)
1 parent cd142ee commit ea1efef

File tree

2 files changed

+104
-82
lines changed

2 files changed

+104
-82
lines changed

googleapiclient-stubs/errors.pyi

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import httplib2 # type: ignore[import-untyped]
12
from _typeshed import Incomplete
23

34
class Error(Exception): ...
45

56
class HttpError(Error):
6-
resp: Incomplete
7-
content: Incomplete
8-
uri: Incomplete
7+
resp: httplib2.Response
8+
content: bytes
9+
uri: str | None
910
error_details: str
10-
reason: Incomplete
11-
def __init__(self, resp, content, uri: Incomplete | None = None) -> None: ...
11+
reason: str
12+
def __init__(
13+
self, resp: httplib2.Response, content: bytes, uri: str | None = None
14+
) -> None: ...
1215
@property
13-
def status_code(self): ...
16+
def status_code(self) -> int: ...
1417

1518
class InvalidJsonError(Error): ...
1619
class UnknownFileType(Error): ...
@@ -23,11 +26,14 @@ class InvalidChunkSizeError(Error): ...
2326
class InvalidNotificationError(Error): ...
2427

2528
class BatchError(HttpError):
26-
resp: Incomplete
27-
content: Incomplete
28-
reason: Incomplete
29+
resp: httplib2.Response | None
30+
content: str | None # type: ignore[assignment]
31+
reason: str
2932
def __init__(
30-
self, reason, resp: Incomplete | None = None, content: Incomplete | None = None
33+
self,
34+
reason: str,
35+
resp: httplib2.Response | None = None,
36+
content: str | None = None,
3137
) -> None: ...
3238

3339
class UnexpectedMethodError(Error):

googleapiclient-stubs/http.pyi

Lines changed: 88 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import collections.abc
2+
import io
3+
import logging
4+
import typing
5+
6+
import httplib2 # type: ignore[import-untyped]
17
from _typeshed import Incomplete
28

39
from googleapiclient.errors import (
@@ -10,123 +16,133 @@ from googleapiclient.errors import (
1016
)
1117
from googleapiclient.model import JsonModel as JsonModel
1218

13-
LOGGER: Incomplete
14-
DEFAULT_CHUNK_SIZE: Incomplete
19+
LOGGER: logging.Logger
20+
DEFAULT_CHUNK_SIZE: int
1521
MAX_URI_LENGTH: int
1622
MAX_BATCH_LIMIT: int
1723
DEFAULT_HTTP_TIMEOUT_SEC: int
1824

1925
class MediaUploadProgress:
20-
resumable_progress: Incomplete
21-
total_size: Incomplete
22-
def __init__(self, resumable_progress, total_size) -> None: ...
23-
def progress(self): ...
26+
resumable_progress: int
27+
total_size: int
28+
def __init__(self, resumable_progress: int, total_size: int) -> None: ...
29+
def progress(self) -> float: ...
2430

2531
class MediaDownloadProgress:
26-
resumable_progress: Incomplete
27-
total_size: Incomplete
28-
def __init__(self, resumable_progress, total_size) -> None: ...
29-
def progress(self): ...
32+
resumable_progress: int
33+
total_size: int
34+
def __init__(self, resumable_progress: int, total_size: int) -> None: ...
35+
def progress(self) -> float: ...
36+
37+
_T = typing.TypeVar("_T")
3038

3139
class MediaUpload:
32-
def chunksize(self) -> None: ...
33-
def mimetype(self): ...
34-
def size(self) -> None: ...
35-
def resumable(self): ...
36-
def getbytes(self, begin, end) -> None: ...
37-
def has_stream(self): ...
38-
def stream(self) -> None: ...
39-
def to_json(self): ...
40+
def chunksize(self) -> int: ...
41+
def mimetype(self) -> str: ...
42+
def size(self) -> int | None: ...
43+
def resumable(self) -> bool: ...
44+
def getbytes(self, begin: int, end: int) -> bytes: ...
45+
def has_stream(self) -> bool: ...
46+
def stream(self) -> io.IOBase: ...
47+
def to_json(self) -> str: ...
4048
@classmethod
41-
def new_from_json(cls, s): ...
49+
def new_from_json(cls: _T, s: str) -> _T: ...
4250

4351
class MediaIoBaseUpload(MediaUpload):
4452
def __init__(
45-
self, fd, mimetype, chunksize=104857600, resumable: bool = False
53+
self,
54+
fd: io.IOBase,
55+
mimetype: str,
56+
chunksize: int = 104857600,
57+
resumable: bool = False,
4658
) -> None: ...
47-
def chunksize(self): ...
48-
def mimetype(self): ...
49-
def size(self): ...
50-
def resumable(self): ...
51-
def getbytes(self, begin, length): ...
52-
def has_stream(self): ...
53-
def stream(self): ...
54-
def to_json(self) -> None: ...
5559

5660
class MediaFileUpload(MediaIoBaseUpload):
5761
def __init__(
5862
self,
59-
filename,
60-
mimetype: Incomplete | None = None,
61-
chunksize=104857600,
63+
filename: str, # TODO: StrPath?
64+
mimetype: str | None = None,
65+
chunksize: int = 104857600,
6266
resumable: bool = False,
6367
) -> None: ...
64-
def __del__(self) -> None: ...
65-
def to_json(self): ...
66-
@staticmethod
67-
def from_json(s): ...
6868

6969
class MediaInMemoryUpload(MediaIoBaseUpload):
7070
def __init__(
7171
self,
72-
body,
72+
body: bytes,
7373
mimetype: str = "application/octet-stream",
74-
chunksize=104857600,
74+
chunksize: int = 104857600,
7575
resumable: bool = False,
7676
) -> None: ...
7777

7878
class MediaIoBaseDownload:
79-
def __init__(self, fd, request, chunksize=104857600) -> None: ...
80-
def next_chunk(self, num_retries: int = 0): ...
81-
82-
class _StreamSlice:
83-
def __init__(self, stream, begin, chunksize) -> None: ...
84-
def read(self, n: int = -1): ...
79+
def __init__(self, fd, request, chunksize: int = 104857600) -> None: ...
80+
def next_chunk(
81+
self, num_retries: int = 0
82+
) -> tuple[MediaDownloadProgress, bool]: ...
8583

8684
class HttpRequest:
87-
uri: Incomplete
88-
method: Incomplete
89-
body: Incomplete
90-
headers: Incomplete
91-
methodId: Incomplete
92-
http: Incomplete
93-
postproc: Incomplete
94-
resumable: Incomplete
95-
response_callbacks: Incomplete
96-
body_size: Incomplete
97-
resumable_uri: Incomplete
85+
uri: str
86+
method: str
87+
body: str | None
88+
headers: dict | None
89+
methodId: str | None
90+
http: httplib2.Http | HttpMock
91+
postproc: collections.abc.Callable[[httplib2.Response, bytes], typing.Any]
92+
resumable: MediaUpload | None
93+
response_callbacks: collections.abc.Callable[[httplib2.Response], typing.Any]
94+
body_size: int
95+
resumable_uri: str | None
9896
resumable_progress: int
9997
def __init__(
10098
self,
101-
http,
102-
postproc,
103-
uri,
99+
http: httplib2.Http | HttpMock,
100+
postproc: collections.abc.Callable[[httplib2.Response, bytes], typing.Any],
101+
uri: str,
104102
method: str = "GET",
105-
body: Incomplete | None = None,
106-
headers: Incomplete | None = None,
107-
methodId: Incomplete | None = None,
108-
resumable: Incomplete | None = None,
103+
body: str | None = None,
104+
headers: dict | None = None,
105+
methodId: str | None = None,
106+
resumable: MediaUpload | None = None,
109107
) -> None: ...
110-
def execute(self, http: Incomplete | None = None, num_retries: int = 0): ...
111-
def add_response_callback(self, cb) -> None: ...
112-
def next_chunk(self, http: Incomplete | None = None, num_retries: int = 0): ...
113-
def to_json(self): ...
108+
def execute(
109+
self, http: httplib2.Http | HttpMock | None = None, num_retries: int = 0
110+
) -> typing.Any: ...
111+
def add_response_callback(
112+
self, cb: collections.abc.Callable[[httplib2.Response], typing.Any]
113+
) -> None: ...
114+
def next_chunk(
115+
self, http: httplib2.Http | HttpMock | None = None, num_retries: int = 0
116+
) -> tuple[MediaUploadProgress, typing.Any]: ...
117+
def to_json(self) -> str: ...
114118
@staticmethod
115-
def from_json(s, http, postproc): ...
119+
def from_json(
120+
s: str,
121+
http: httplib2.Http | HttpMock,
122+
postproc: collections.abc.Callable[[httplib2.Response, bytes], typing.Any],
123+
) -> HttpRequest: ...
116124
@staticmethod
117-
def null_postproc(resp, contents): ...
125+
def null_postproc(
126+
resp: httplib2.Response, contents: bytes
127+
) -> tuple[httplib2.Response, bytes]: ...
118128

119129
class BatchHttpRequest:
120130
def __init__(
121-
self, callback: Incomplete | None = None, batch_uri: Incomplete | None = None
131+
self,
132+
callback: collections.abc.Callable[
133+
[str, typing.Any, HttpError | None], typing.Any
134+
]
135+
| None = None,
136+
batch_uri: str | None = None,
122137
) -> None: ...
123138
def add(
124139
self,
125-
request,
126-
callback: Incomplete | None = None,
127-
request_id: Incomplete | None = None,
140+
request: HttpRequest,
141+
callback: collections.abc.Callable[[str, typing.Any, HttpError], typing.Any]
142+
| None = None,
143+
request_id: str | None = None,
128144
) -> None: ...
129-
def execute(self, http: Incomplete | None = None) -> None: ...
145+
def execute(self, http: httplib2.Http | HttpMock | None = None) -> None: ...
130146

131147
class HttpRequestMock:
132148
resp: Incomplete

0 commit comments

Comments
 (0)