Skip to content
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

Fix ValidationError false positive on nested inputs #943

Merged
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
17 changes: 2 additions & 15 deletions django-stubs/core/exceptions.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import sys
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union

from django.forms.utils import ErrorDict

if sys.version_info < (3, 8):
from typing_extensions import Literal
else:
Expand Down Expand Up @@ -32,18 +30,6 @@ class FieldError(Exception): ...

NON_FIELD_ERRORS: Literal["__all__"] = ...

_MsgTypeBase = Union[str, ValidationError]
# Yeah, it's really ugly, but __init__ checks with isinstance()
_MsgType = Union[
_MsgTypeBase,
Dict[str, _MsgTypeBase],
List[_MsgTypeBase],
Dict[str, str],
Dict[str, ValidationError],
List[str],
List[ValidationError],
]

class ValidationError(Exception):
error_dict: Dict[str, List[ValidationError]] = ...
error_list: List[ValidationError] = ...
Expand All @@ -52,7 +38,8 @@ class ValidationError(Exception):
params: Optional[Dict[str, Any]] = ...
def __init__(
self,
message: _MsgType,
# Accepts arbitrarily nested data structure, mypy doesn't allow describing it accurately.
message: Union[str, ValidationError, Dict[str, Any], List[Any]],
code: Optional[str] = ...,
params: Optional[Dict[str, Any]] = ...,
) -> None: ...
Expand Down
22 changes: 22 additions & 0 deletions tests/typecheck/core/test_exceptions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- case: ValidationError_nested_message
main: |
from django.core.exceptions import ValidationError


ValidationError({
'list': [
'list error 1',
'list error 2'
],
'plain_str': 'message',
'plain_error': ValidationError('message'),
'list_error': [
ValidationError('list error 1', code='test'),
ValidationError('list error 2', code='test'),
]
})
ValidationError([
'message 1',
ValidationError('message 2'),
['nested 1', 'nested 2'],
])