Skip to content

Commit

Permalink
Fix ValidationError false positive on nested inputs (#943)
Browse files Browse the repository at this point in the history
Reverts parts of PR 909.
  • Loading branch information
intgr authored Apr 28, 2022
1 parent 6226381 commit c836c3a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
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'],
])

0 comments on commit c836c3a

Please sign in to comment.