Skip to content

[Fix] Allow StrPromise to be used in exceptions #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -216,7 +216,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"