Skip to content

Commit 297a53d

Browse files
chore(deps): lock file maintenance (#986)
1 parent 39bfb62 commit 297a53d

4 files changed

Lines changed: 1630 additions & 1615 deletions

File tree

‎src/apify_client/errors.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ class ApifyApiError(ApifyClientError):
4141

4242
def __new__(cls, response: HttpResponse, attempt: int, *, method: str = 'GET') -> Self: # noqa: ARG004
4343
"""Dispatch to the subclass matching the response's HTTP status code, if any."""
44-
target_cls: type[ApifyApiError] = cls
44+
instance = super().__new__(cls)
4545
if cls is ApifyApiError:
4646
status = response.status_code
4747
mapped = _STATUS_TO_CLASS.get(status)
4848
if mapped is None and status >= HTTPStatus.INTERNAL_SERVER_ERROR:
4949
mapped = ServerError
5050
if mapped is not None:
51-
target_cls = mapped
52-
return super().__new__(target_cls)
51+
# Retagging is runtime-equivalent to allocating `mapped` directly, but keeps the return type `Self`.
52+
instance.__class__ = mapped
53+
return instance
5354

5455
def __init__(self, response: HttpResponse, attempt: int, *, method: str = 'GET') -> None:
5556
"""Initialize the API error from a failed response.

‎tests/unit/test_client_errors.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
from typing import TYPE_CHECKING, Any
5+
from unittest.mock import Mock
56

67
import pytest
78
from werkzeug import Response
@@ -243,6 +244,20 @@ def test_apify_api_error_dispatches_all_mapped_statuses(
243244
assert exc.value.status_code == status_code
244245

245246

247+
def test_apify_api_error_subclass_constructed_directly_keeps_its_class() -> None:
248+
"""Only `ApifyApiError` itself dispatches - a subclass constructed directly is never re-dispatched by status."""
249+
response = Mock()
250+
response.status_code = 404
251+
response.json.return_value = {'error': {'type': 'record-not-found', 'message': 'nope'}}
252+
253+
# 404 maps to `NotFoundError`, but the explicit class must win over the status.
254+
error = ServerError(response, 1)
255+
256+
assert type(error) is ServerError
257+
assert error.status_code == 404
258+
assert error.type == 'record-not-found'
259+
260+
246261
def test_apify_api_error_falls_back_for_unparsable_body(httpserver: HTTPServer) -> None:
247262
"""When the body can't be parsed, status-based dispatch still applies and `.type` is None."""
248263
httpserver.expect_request('/unparsable').respond_with_data('<not json>', status=418, content_type='text/html')

0 commit comments

Comments
 (0)