Skip to content

Commit 1593090

Browse files
committed
JsonValue = None | bool | int | float | str | list["JsonValue"] | dict[str, "JsonValue"]
1 parent bc836d2 commit 1593090

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

mureq.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
DEFAULT_UA = "Python " + sys.version.split()[0]
3939

4040
Headers = MutableMapping[str, str] | HTTPMessage
41+
JsonValue = None | bool | int | float | str | list["JsonValue"] | dict[str, "JsonValue"]
4142

4243

4344
def request(method, url, *, read_limit=None, **kwargs):
@@ -145,7 +146,7 @@ def yield_response(
145146
:return: http.client.HTTPResponse, yielded as context manager
146147
:rtype: http.client.HTTPResponse
147148
:raises: HTTPException
148-
"""
149+
""" # noqa: E501
149150
method = method.upper()
150151
headers = cast("MutableMapping[str, str]", _prepare_outgoing_headers(headers))
151152
enc_params = _prepare_params(params)
@@ -203,45 +204,51 @@ class Response:
203204

204205
__slots__ = ("body", "headers", "status_code", "url")
205206

206-
def __init__(self, url, status_code, headers, body):
207+
def __init__(
208+
self,
209+
url: str,
210+
status_code: int,
211+
headers: Headers,
212+
body: bytes,
213+
) -> None:
207214
self.url, self.status_code, self.headers, self.body = (
208215
url,
209216
status_code,
210217
headers,
211218
body,
212219
)
213220

214-
def __repr__(self):
221+
def __repr__(self) -> str:
215222
return f"Response(status_code={self.status_code:d})"
216223

217224
@property
218-
def ok(self):
225+
def ok(self) -> bool:
219226
"""Ok returns whether the response had a successful status code
220227
(anything other than a 40x or 50x).
221228
"""
222229
return not (400 <= self.status_code < 600)
223230

224231
@property
225-
def content(self):
232+
def content(self) -> bytes:
226233
"""Content returns the response body (the `body` member). This is an
227234
alias for compatibility with requests.Response.
228235
"""
229236
return self.body
230237

231-
def raise_for_status(self):
238+
def raise_for_status(self) -> None:
232239
"""raise_for_status checks the response's success code, raising an
233240
exception for error codes.
234241
"""
235242
if not self.ok:
236243
raise HTTPErrorStatus(self.status_code)
237244

238-
def json(self):
245+
def json(self) -> JsonValue:
239246
"""Attempts to deserialize the response body as UTF-8 encoded JSON."""
240247
import json as jsonlib
241248

242249
return jsonlib.loads(self.body)
243250

244-
def _debugstr(self):
251+
def _debugstr(self) -> str:
245252
buf = io.StringIO()
246253
print("HTTP", self.status_code, file=buf)
247254
for k, v in self.headers.items():
@@ -267,10 +274,10 @@ class HTTPErrorStatus(HTTPException):
267274
called explicitly.
268275
"""
269276

270-
def __init__(self, status_code):
277+
def __init__(self, status_code: int) -> None:
271278
self.status_code = status_code
272279

273-
def __str__(self):
280+
def __str__(self) -> str:
274281
return f"HTTP response returned error code {self.status_code:d}"
275282

276283

@@ -285,11 +292,11 @@ class UnixHTTPConnection(HTTPConnection):
285292
Unix domain stream socket instead of a TCP address.
286293
"""
287294

288-
def __init__(self, path, timeout=DEFAULT_TIMEOUT):
289-
super(UnixHTTPConnection, self).__init__("localhost", timeout=timeout)
295+
def __init__(self, path: str, timeout: float = DEFAULT_TIMEOUT) -> None:
296+
super().__init__("localhost", timeout=timeout)
290297
self._unix_path = path
291298

292-
def connect(self):
299+
def connect(self) -> None:
293300
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
294301
try:
295302
sock.settimeout(self.timeout)
@@ -300,7 +307,7 @@ def connect(self):
300307
self.sock = sock
301308

302309

303-
def _check_redirect(url, status, response_headers):
310+
def _check_redirect(url: str, status: int, response_headers: Headers) -> str | None:
304311
"""Return the URL to redirect to, or None for no redirection."""
305312
if status not in (301, 302, 303, 307, 308):
306313
return None

ruff.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ target-version = "py311"
88

99
[lint]
1010
select = ["ALL"]
11-
ignore = ["C901", "D107", "D401", "EM101", "PLR0912", "PLR0913", "TRY003"]
11+
ignore = ["C901", "D105", "D107", "D205", "D401", "EM101", "FIX003", "PLR0912", "PLR0913", "PLR2004", "PTH118", "TD001", "TD002", "TD003", "TD004", "TRY003"]
1212

1313
[format]
1414
# Same as Black.

0 commit comments

Comments
 (0)