Skip to content

Commit

Permalink
[Fix] Allow StrPromise to be used in exceptions (#297)
Browse files Browse the repository at this point in the history
Co-authored-by: Xavier Francisco <[email protected]>
  • Loading branch information
XF-FW and Qu4tro authored Jan 4, 2023
1 parent 3b8738f commit 728da9c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion rest_framework-stubs/exceptions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from typing import Any
from typing_extensions import TypeAlias

from django.http import HttpRequest, JsonResponse
from django_stubs_ext import StrOrPromise
from rest_framework.renderers import BaseRenderer
from rest_framework.request import Request

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

_Detail: TypeAlias = str | list[Any] | dict[str, Any]
_Detail: TypeAlias = StrOrPromise | list[Any] | dict[str, Any]

class APIException(Exception):
status_code: int
Expand Down
6 changes: 3 additions & 3 deletions scripts/typecheck_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"Dict entry",
'"FieldValues"',
'base class "Field" defined the type as "bool"',
'Invalid index type "int" for "Union[str, List[Any], Dict[str, Any]]"; expected type "str"',
'Invalid index type "int" for "Union[str, _StrPromise, List[Any], Dict[str, Any]]"; expected type "str"',
'Item "str" of "Union[str, Any]" has no attribute "code"',
'Argument "default" to "CharField" has incompatible type',
'"MultipleChoiceField" has no attribute "partial"',
Expand Down Expand Up @@ -152,7 +152,7 @@
'Cannot assign multiple types to name "composed_perm" without an explicit "Type[...]" annotation',
],
"test_relations.py": [
'Invalid index type "int" for "Union[str, List[Any], Dict[str, Any]]"; expected type "str"',
'Invalid index type "int" for "Union[str, _StrPromise, List[Any], Dict[str, Any]]"; expected type "str"',
'Argument "queryset" to "HyperlinkedRelatedField" has incompatible type',
'Incompatible return value type (got "None", expected "HttpResponseBase',
'Argument 2 to "re_path" has incompatible type "Callable[[], None]"; expected "Callable[..., HttpResponseBase]"', # noqa: E501
Expand Down Expand Up @@ -217,7 +217,7 @@
'Argument 1 to "to_internal_value" of "Field" has incompatible type "object"',
],
"test_validation_error.py": [
'Argument "detail" to "ValidationError" has incompatible type "Tuple[str, str]"; expected "Optional[Union[str, List[Any], Dict[str, Any]]]"', # noqa: E501
'Argument "detail" to "ValidationError" has incompatible type "Tuple[str, str]"; expected "Optional[Union[Union[str, _StrPromise], List[Any], Dict[str, Any]]]"', # noqa: E501
],
"test_validators.py": [
'Argument "queryset" to "BaseUniqueForValidator" has incompatible type "object";'
Expand Down
9 changes: 9 additions & 0 deletions tests/typecheck/test_exceptions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@
status_code = 200
default_detail = {"ok": "everything"}
default_code = "ok"
- case: test_exception_declaration_lazystr
main: |
from django.utils.translation import gettext_lazy as _
from rest_framework import exceptions
class MyException(exceptions.APIException):
status_code = 200
default_detail = _("Está tudo bem")
default_code = "ok"

0 comments on commit 728da9c

Please sign in to comment.