From 7eaac72e7fdb82d52bc19c62b76218c9a9775f27 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Thu, 28 Apr 2022 18:50:09 +0300 Subject: [PATCH] Fix ValidationError false positive on nested inputs Reverts parts of PR 909. --- django-stubs/core/exceptions.pyi | 17 ++--------------- tests/typecheck/core/test_exceptions.yml | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 tests/typecheck/core/test_exceptions.yml diff --git a/django-stubs/core/exceptions.pyi b/django-stubs/core/exceptions.pyi index 025e32a90..e8f72e066 100644 --- a/django-stubs/core/exceptions.pyi +++ b/django-stubs/core/exceptions.pyi @@ -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: ... diff --git a/tests/typecheck/core/test_exceptions.yml b/tests/typecheck/core/test_exceptions.yml new file mode 100644 index 000000000..755961a70 --- /dev/null +++ b/tests/typecheck/core/test_exceptions.yml @@ -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'], + ])