|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | from http import HTTPStatus |
4 | | -from typing import TYPE_CHECKING, Any |
| 4 | +from typing import TYPE_CHECKING, Any, cast |
5 | 5 |
|
6 | 6 | from apify_client._docs import docs_group |
7 | 7 |
|
@@ -41,16 +41,16 @@ class ApifyApiError(ApifyClientError): |
41 | 41 |
|
42 | 42 | def __new__(cls, response: HttpResponse, attempt: int, *, method: str = 'GET') -> Self: # noqa: ARG004 |
43 | 43 | """Dispatch to the subclass matching the response's HTTP status code, if any.""" |
44 | | - instance = super().__new__(cls) |
| 44 | + target = cls |
45 | 45 | if cls is ApifyApiError: |
46 | 46 | status = response.status_code |
47 | 47 | mapped = _STATUS_TO_CLASS.get(status) |
48 | 48 | if mapped is None and status >= HTTPStatus.INTERNAL_SERVER_ERROR: |
49 | 49 | mapped = ServerError |
50 | 50 | if mapped is not None: |
51 | | - # Retagging is runtime-equivalent to allocating `mapped` directly, but keeps the return type `Self`. |
52 | | - instance.__class__ = mapped |
53 | | - return instance |
| 51 | + # `cls` is `ApifyApiError` here, so every mapped subclass is a `type[Self]`. |
| 52 | + target = cast('type[Self]', mapped) |
| 53 | + return super().__new__(target) |
54 | 54 |
|
55 | 55 | def __init__(self, response: HttpResponse, attempt: int, *, method: str = 'GET') -> None: |
56 | 56 | """Initialize the API error from a failed response. |
|
0 commit comments