Fix ValidationError false positive on nested inputs (#943)

Reverts parts of PR 909.
This commit is contained in:
Marti Raudsepp
2022-04-28 19:19:18 +03:00
committed by GitHub
parent 6226381484
commit c836c3ac3a
2 changed files with 24 additions and 15 deletions

View File

@@ -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:
@@ -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] = ...
@@ -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: ...

View File

@@ -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'],
])