mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-20 02:41:16 +08:00
Fix ValidationError false positive on nested inputs (#943)
Reverts parts of PR 909.
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
import sys
|
import sys
|
||||||
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
|
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
|
||||||
|
|
||||||
from django.forms.utils import ErrorDict
|
|
||||||
|
|
||||||
if sys.version_info < (3, 8):
|
if sys.version_info < (3, 8):
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
else:
|
else:
|
||||||
@@ -32,18 +30,6 @@ class FieldError(Exception): ...
|
|||||||
|
|
||||||
NON_FIELD_ERRORS: Literal["__all__"] = ...
|
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):
|
class ValidationError(Exception):
|
||||||
error_dict: Dict[str, List[ValidationError]] = ...
|
error_dict: Dict[str, List[ValidationError]] = ...
|
||||||
error_list: List[ValidationError] = ...
|
error_list: List[ValidationError] = ...
|
||||||
@@ -52,7 +38,8 @@ class ValidationError(Exception):
|
|||||||
params: Optional[Dict[str, Any]] = ...
|
params: Optional[Dict[str, Any]] = ...
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
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] = ...,
|
code: Optional[str] = ...,
|
||||||
params: Optional[Dict[str, Any]] = ...,
|
params: Optional[Dict[str, Any]] = ...,
|
||||||
) -> None: ...
|
) -> None: ...
|
||||||
|
|||||||
22
tests/typecheck/core/test_exceptions.yml
Normal file
22
tests/typecheck/core/test_exceptions.yml
Normal 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'],
|
||||||
|
])
|
||||||
Reference in New Issue
Block a user