Skip to content

Commit 728da9c

Browse files
XF-FWQu4tro
andauthored
[Fix] Allow StrPromise to be used in exceptions (#297)
Co-authored-by: Xavier Francisco <[email protected]>
1 parent 3b8738f commit 728da9c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

rest_framework-stubs/exceptions.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ from typing import Any
44
from typing_extensions import TypeAlias
55

66
from django.http import HttpRequest, JsonResponse
7+
from django_stubs_ext import StrOrPromise
78
from rest_framework.renderers import BaseRenderer
89
from rest_framework.request import Request
910

@@ -15,7 +16,7 @@ class ErrorDetail(str):
1516
code: str | None
1617
def __new__(cls, string: str, code: str | None = ...): ...
1718

18-
_Detail: TypeAlias = str | list[Any] | dict[str, Any]
19+
_Detail: TypeAlias = StrOrPromise | list[Any] | dict[str, Any]
1920

2021
class APIException(Exception):
2122
status_code: int

scripts/typecheck_tests.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"Dict entry",
107107
'"FieldValues"',
108108
'base class "Field" defined the type as "bool"',
109-
'Invalid index type "int" for "Union[str, List[Any], Dict[str, Any]]"; expected type "str"',
109+
'Invalid index type "int" for "Union[str, _StrPromise, List[Any], Dict[str, Any]]"; expected type "str"',
110110
'Item "str" of "Union[str, Any]" has no attribute "code"',
111111
'Argument "default" to "CharField" has incompatible type',
112112
'"MultipleChoiceField" has no attribute "partial"',
@@ -152,7 +152,7 @@
152152
'Cannot assign multiple types to name "composed_perm" without an explicit "Type[...]" annotation',
153153
],
154154
"test_relations.py": [
155-
'Invalid index type "int" for "Union[str, List[Any], Dict[str, Any]]"; expected type "str"',
155+
'Invalid index type "int" for "Union[str, _StrPromise, List[Any], Dict[str, Any]]"; expected type "str"',
156156
'Argument "queryset" to "HyperlinkedRelatedField" has incompatible type',
157157
'Incompatible return value type (got "None", expected "HttpResponseBase',
158158
'Argument 2 to "re_path" has incompatible type "Callable[[], None]"; expected "Callable[..., HttpResponseBase]"', # noqa: E501
@@ -217,7 +217,7 @@
217217
'Argument 1 to "to_internal_value" of "Field" has incompatible type "object"',
218218
],
219219
"test_validation_error.py": [
220-
'Argument "detail" to "ValidationError" has incompatible type "Tuple[str, str]"; expected "Optional[Union[str, List[Any], Dict[str, Any]]]"', # noqa: E501
220+
'Argument "detail" to "ValidationError" has incompatible type "Tuple[str, str]"; expected "Optional[Union[Union[str, _StrPromise], List[Any], Dict[str, Any]]]"', # noqa: E501
221221
],
222222
"test_validators.py": [
223223
'Argument "queryset" to "BaseUniqueForValidator" has incompatible type "object";'

tests/typecheck/test_exceptions.yml

+9
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@
1515
status_code = 200
1616
default_detail = {"ok": "everything"}
1717
default_code = "ok"
18+
- case: test_exception_declaration_lazystr
19+
main: |
20+
from django.utils.translation import gettext_lazy as _
21+
from rest_framework import exceptions
22+
23+
class MyException(exceptions.APIException):
24+
status_code = 200
25+
default_detail = _("Está tudo bem")
26+
default_code = "ok"

0 commit comments

Comments
 (0)