From 9ef4a18dbe71f538a9ef8c39111ae2f0b62eb90b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Wed, 12 Feb 2020 14:48:49 +0100 Subject: [PATCH] Changed django.forms.ValidationError imports to django.core.exceptions.ValidationError. Co-Authored-By: Mariusz Felisiak --- django/contrib/admin/forms.py | 4 +- django/contrib/auth/forms.py | 17 +-- django/contrib/flatpages/forms.py | 7 +- .../gis/db/backends/postgis/pgraster.py | 2 +- django/contrib/gis/forms/fields.py | 9 +- django/contrib/postgres/forms/jsonb.py | 3 +- docs/ref/forms/fields.txt | 5 +- docs/ref/forms/validation.txt | 6 +- docs/topics/auth/customizing.txt | 3 +- docs/topics/auth/default.txt | 4 +- docs/topics/forms/formsets.txt | 5 +- docs/topics/i18n/translation.txt | 5 +- tests/admin_inlines/admin.py | 3 +- tests/admin_views/admin.py | 2 +- .../custom_has_permission_admin.py | 4 +- tests/admin_views/forms.py | 4 +- tests/auth_tests/test_forms.py | 12 +- .../field_tests/test_booleanfield.py | 3 +- .../forms_tests/field_tests/test_charfield.py | 2 +- .../field_tests/test_choicefield.py | 3 +- .../field_tests/test_combofield.py | 3 +- .../forms_tests/field_tests/test_datefield.py | 5 +- .../field_tests/test_datetimefield.py | 3 +- .../field_tests/test_decimalfield.py | 3 +- .../field_tests/test_emailfield.py | 3 +- .../forms_tests/field_tests/test_filefield.py | 3 +- .../field_tests/test_filepathfield.py | 3 +- .../field_tests/test_floatfield.py | 3 +- .../field_tests/test_genericipaddressfield.py | 3 +- .../field_tests/test_imagefield.py | 5 +- .../field_tests/test_integerfield.py | 3 +- .../field_tests/test_multiplechoicefield.py | 3 +- .../field_tests/test_multivaluefield.py | 2 +- .../field_tests/test_regexfield.py | 3 +- .../field_tests/test_splitdatetimefield.py | 3 +- .../forms_tests/field_tests/test_timefield.py | 3 +- .../field_tests/test_typedchoicefield.py | 3 +- .../test_typedmultiplechoicefield.py | 3 +- .../forms_tests/field_tests/test_urlfield.py | 3 +- .../forms_tests/field_tests/test_uuidfield.py | 3 +- .../forms_tests/tests/test_error_messages.py | 3 +- tests/forms_tests/tests/test_forms.py | 2 +- tests/forms_tests/tests/test_formsets.py | 3 +- tests/forms_tests/tests/test_input_formats.py | 105 +++++++++--------- tests/gis_tests/test_geoforms.py | 8 +- tests/model_forms/test_modelchoicefield.py | 2 +- tests/model_forms/tests.py | 5 +- tests/test_client/views.py | 3 +- 48 files changed, 164 insertions(+), 133 deletions(-) diff --git a/django/contrib/admin/forms.py b/django/contrib/admin/forms.py index e973c61972..ee275095e3 100644 --- a/django/contrib/admin/forms.py +++ b/django/contrib/admin/forms.py @@ -1,5 +1,5 @@ -from django import forms from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm +from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ @@ -19,7 +19,7 @@ class AdminAuthenticationForm(AuthenticationForm): def confirm_login_allowed(self, user): super().confirm_login_allowed(user) if not user.is_staff: - raise forms.ValidationError( + raise ValidationError( self.error_messages['invalid_login'], code='invalid_login', params={'username': self.username_field.verbose_name} diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py index e5e6a86c1e..4f33992028 100644 --- a/django/contrib/auth/forms.py +++ b/django/contrib/auth/forms.py @@ -10,6 +10,7 @@ from django.contrib.auth.hashers import ( from django.contrib.auth.models import User from django.contrib.auth.tokens import default_token_generator from django.contrib.sites.shortcuts import get_current_site +from django.core.exceptions import ValidationError from django.core.mail import EmailMultiAlternatives from django.template import loader from django.utils.encoding import force_bytes @@ -113,7 +114,7 @@ class UserCreationForm(forms.ModelForm): password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: - raise forms.ValidationError( + raise ValidationError( self.error_messages['password_mismatch'], code='password_mismatch', ) @@ -127,7 +128,7 @@ class UserCreationForm(forms.ModelForm): if password: try: password_validation.validate_password(password, self.instance) - except forms.ValidationError as error: + except ValidationError as error: self.add_error('password2', error) def save(self, commit=True): @@ -226,12 +227,12 @@ class AuthenticationForm(forms.Form): allow login by active users, and reject login by inactive users. If the given user cannot log in, this method should raise a - ``forms.ValidationError``. + ``ValidationError``. If the given user may log in, this method should return None. """ if not user.is_active: - raise forms.ValidationError( + raise ValidationError( self.error_messages['inactive'], code='inactive', ) @@ -240,7 +241,7 @@ class AuthenticationForm(forms.Form): return self.user_cache def get_invalid_login_error(self): - return forms.ValidationError( + return ValidationError( self.error_messages['invalid_login'], code='invalid_login', params={'username': self.username_field.verbose_name}, @@ -354,7 +355,7 @@ class SetPasswordForm(forms.Form): password2 = self.cleaned_data.get('new_password2') if password1 and password2: if password1 != password2: - raise forms.ValidationError( + raise ValidationError( self.error_messages['password_mismatch'], code='password_mismatch', ) @@ -392,7 +393,7 @@ class PasswordChangeForm(SetPasswordForm): """ old_password = self.cleaned_data["old_password"] if not self.user.check_password(old_password): - raise forms.ValidationError( + raise ValidationError( self.error_messages['password_incorrect'], code='password_incorrect', ) @@ -429,7 +430,7 @@ class AdminPasswordChangeForm(forms.Form): password2 = self.cleaned_data.get('password2') if password1 and password2: if password1 != password2: - raise forms.ValidationError( + raise ValidationError( self.error_messages['password_mismatch'], code='password_mismatch', ) diff --git a/django/contrib/flatpages/forms.py b/django/contrib/flatpages/forms.py index 95d9899920..f5ee76443a 100644 --- a/django/contrib/flatpages/forms.py +++ b/django/contrib/flatpages/forms.py @@ -1,6 +1,7 @@ from django import forms from django.conf import settings from django.contrib.flatpages.models import FlatPage +from django.core.exceptions import ValidationError from django.utils.translation import gettext, gettext_lazy as _ @@ -38,12 +39,12 @@ class FlatpageForm(forms.ModelForm): def clean_url(self): url = self.cleaned_data['url'] if not url.startswith('/'): - raise forms.ValidationError( + raise ValidationError( gettext("URL is missing a leading slash."), code='missing_leading_slash', ) if self._trailing_slash_required() and not url.endswith('/'): - raise forms.ValidationError( + raise ValidationError( gettext("URL is missing a trailing slash."), code='missing_trailing_slash', ) @@ -60,7 +61,7 @@ class FlatpageForm(forms.ModelForm): if sites and same_url.filter(sites__in=sites).exists(): for site in sites: if same_url.filter(sites=site).exists(): - raise forms.ValidationError( + raise ValidationError( _('Flatpage with url %(url)s already exists for site %(site)s'), code='duplicate_url', params={'url': url, 'site': site}, diff --git a/django/contrib/gis/db/backends/postgis/pgraster.py b/django/contrib/gis/db/backends/postgis/pgraster.py index 085c130183..bfdeb6faf3 100644 --- a/django/contrib/gis/db/backends/postgis/pgraster.py +++ b/django/contrib/gis/db/backends/postgis/pgraster.py @@ -1,6 +1,6 @@ import struct -from django.forms import ValidationError +from django.core.exceptions import ValidationError from .const import ( BANDTYPE_FLAG_HASNODATA, BANDTYPE_PIXTYPE_MASK, GDAL_TO_POSTGIS, diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py index 269981048d..3371c06456 100644 --- a/django/contrib/gis/forms/fields.py +++ b/django/contrib/gis/forms/fields.py @@ -1,6 +1,7 @@ from django import forms from django.contrib.gis.gdal import GDALException from django.contrib.gis.geos import GEOSException, GEOSGeometry +from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ from .widgets import OpenLayersWidget @@ -47,7 +48,7 @@ class GeometryField(forms.Field): except (GEOSException, ValueError, TypeError): value = None if value is None: - raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom') + raise ValidationError(self.error_messages['invalid_geom'], code='invalid_geom') # Try to set the srid if not value.srid: @@ -71,14 +72,14 @@ class GeometryField(forms.Field): # Ensuring that the geometry is of the correct type (indicated # using the OGC string label). if str(geom.geom_type).upper() != self.geom_type and not self.geom_type == 'GEOMETRY': - raise forms.ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type') + raise ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type') # Transforming the geometry if the SRID was set. if self.srid and self.srid != -1 and self.srid != geom.srid: try: geom.transform(self.srid) except GEOSException: - raise forms.ValidationError( + raise ValidationError( self.error_messages['transform_error'], code='transform_error') return geom @@ -89,7 +90,7 @@ class GeometryField(forms.Field): try: data = self.to_python(data) initial = self.to_python(initial) - except forms.ValidationError: + except ValidationError: return True # Only do a geographic comparison if both values are available diff --git a/django/contrib/postgres/forms/jsonb.py b/django/contrib/postgres/forms/jsonb.py index 2865498d68..196d2b9096 100644 --- a/django/contrib/postgres/forms/jsonb.py +++ b/django/contrib/postgres/forms/jsonb.py @@ -1,6 +1,7 @@ import json from django import forms +from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ __all__ = ['JSONField'] @@ -30,7 +31,7 @@ class JSONField(forms.CharField): try: converted = json.loads(value) except json.JSONDecodeError: - raise forms.ValidationError( + raise ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index 92c1af63d7..3d228e88ad 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -18,8 +18,9 @@ other hooks. Although the primary way you'll use ``Field`` classes is in ``Form`` classes, you can also instantiate them and use them directly to get a better idea of how they work. Each ``Field`` instance has a ``clean()`` method, which takes -a single argument and either raises a ``django.forms.ValidationError`` -exception or returns the clean value:: +a single argument and either raises a +``django.core.exceptions.ValidationError`` exception or returns the clean +value:: >>> from django import forms >>> f = forms.EmailField() diff --git a/docs/ref/forms/validation.txt b/docs/ref/forms/validation.txt index d12232afc7..c3fa968bdb 100644 --- a/docs/ref/forms/validation.txt +++ b/docs/ref/forms/validation.txt @@ -305,6 +305,7 @@ don't want to put it into the general ``MultiEmailField`` class. Instead, we write a cleaning method that operates on the ``recipients`` field, like so:: from django import forms + from django.core.exceptions import ValidationError class ContactForm(forms.Form): # Everything as before. @@ -313,7 +314,7 @@ write a cleaning method that operates on the ``recipients`` field, like so:: def clean_recipients(self): data = self.cleaned_data['recipients'] if "fred@example.com" not in data: - raise forms.ValidationError("You have forgotten about Fred!") + raise ValidationError("You have forgotten about Fred!") # Always return a value to use as the new cleaned data, even if # this method didn't change it. @@ -346,6 +347,7 @@ an error, you can raise a ``ValidationError`` from the ``clean()`` method. For example:: from django import forms + from django.core.exceptions import ValidationError class ContactForm(forms.Form): # Everything as before. @@ -359,7 +361,7 @@ example:: if cc_myself and subject: # Only do something if both fields are valid so far. if "help" not in subject: - raise forms.ValidationError( + raise ValidationError( "Did not send for 'help' in the subject despite " "CC'ing yourself." ) diff --git a/docs/topics/auth/customizing.txt b/docs/topics/auth/customizing.txt index 2bf8d611c2..c8a9a39158 100644 --- a/docs/topics/auth/customizing.txt +++ b/docs/topics/auth/customizing.txt @@ -1100,6 +1100,7 @@ code would be required in the app's ``admin.py`` file:: from django.contrib.auth.models import Group from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.forms import ReadOnlyPasswordHashField + from django.core.exceptions import ValidationError from customauth.models import MyUser @@ -1119,7 +1120,7 @@ code would be required in the app's ``admin.py`` file:: password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: - raise forms.ValidationError("Passwords don't match") + raise ValidationError("Passwords don't match") return password2 def save(self, commit=True): diff --git a/docs/topics/auth/default.txt b/docs/topics/auth/default.txt index 9cebdf4a3e..dab12ca592 100644 --- a/docs/topics/auth/default.txt +++ b/docs/topics/auth/default.txt @@ -1508,12 +1508,12 @@ provides several built-in forms located in :mod:`django.contrib.auth.forms`: class PickyAuthenticationForm(AuthenticationForm): def confirm_login_allowed(self, user): if not user.is_active: - raise forms.ValidationError( + raise ValidationError( _("This account is inactive."), code='inactive', ) if user.username.startswith('b'): - raise forms.ValidationError( + raise ValidationError( _("Sorry, accounts starting with 'b' aren't welcome here."), code='no_b_users', ) diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt index d6aafaa8b5..2f3e4403f6 100644 --- a/docs/topics/forms/formsets.txt +++ b/docs/topics/forms/formsets.txt @@ -220,7 +220,7 @@ this management data, an exception will be raised:: >>> formset.is_valid() Traceback (most recent call last): ... - django.forms.utils.ValidationError: ['ManagementForm data is missing or has been tampered with'] + django.core.exceptions.ValidationError: ['ManagementForm data is missing or has been tampered with'] It is used to keep track of how many form instances are being displayed. If you are adding new forms via JavaScript, you should increment the count fields @@ -261,6 +261,7 @@ Custom formset validation A formset has a ``clean`` method similar to the one on a ``Form`` class. This is where you define your own validation that works at the formset level:: + >>> from django.core.exceptions import ValidationError >>> from django.forms import BaseFormSet >>> from django.forms import formset_factory >>> from myapp.forms import ArticleForm @@ -277,7 +278,7 @@ is where you define your own validation that works at the formset level:: ... continue ... title = form.cleaned_data.get('title') ... if title in titles: - ... raise forms.ValidationError("Articles in a set must have distinct titles.") + ... raise ValidationError("Articles in a set must have distinct titles.") ... titles.append(title) >>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet) diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt index d038dcd2c5..5fdf8f0a4e 100644 --- a/docs/topics/i18n/translation.txt +++ b/docs/topics/i18n/translation.txt @@ -454,6 +454,7 @@ integer as the ``number`` argument. Then ``number`` will be looked up in the dictionary under that key during string interpolation. Here's example:: from django import forms + from django.core.exceptions import ValidationError from django.utils.translation import ngettext_lazy class MyForm(forms.Form): @@ -463,7 +464,7 @@ dictionary under that key during string interpolation. Here's example:: def clean(self): # ... if error: - raise forms.ValidationError(self.error_message % {'num': number}) + raise ValidationError(self.error_message % {'num': number}) If the string contains exactly one unnamed placeholder, you can interpolate directly with the ``number`` argument:: @@ -477,7 +478,7 @@ directly with the ``number`` argument:: def clean(self): # ... if error: - raise forms.ValidationError(self.error_message % number) + raise ValidationError(self.error_message % number) Formatting strings: ``format_lazy()`` diff --git a/tests/admin_inlines/admin.py b/tests/admin_inlines/admin.py index 72a32e4d17..5c968870e4 100644 --- a/tests/admin_inlines/admin.py +++ b/tests/admin_inlines/admin.py @@ -1,5 +1,6 @@ from django import forms from django.contrib import admin +from django.core.exceptions import ValidationError from django.db import models from .models import ( @@ -102,7 +103,7 @@ class TitleForm(forms.ModelForm): title1 = cleaned_data.get("title1") title2 = cleaned_data.get("title2") if title1 != title2: - raise forms.ValidationError("The two titles must be the same") + raise ValidationError("The two titles must be the same") return cleaned_data diff --git a/tests/admin_views/admin.py b/tests/admin_views/admin.py index beec6f80f4..44623e483f 100644 --- a/tests/admin_views/admin.py +++ b/tests/admin_views/admin.py @@ -223,7 +223,7 @@ class BasePersonModelFormSet(BaseModelFormSet): person = person_dict.get('id') alive = person_dict.get('alive') if person and alive and person.name == "Grace Hopper": - raise forms.ValidationError("Grace is not a Zombie") + raise ValidationError("Grace is not a Zombie") class PersonAdmin(admin.ModelAdmin): diff --git a/tests/admin_views/custom_has_permission_admin.py b/tests/admin_views/custom_has_permission_admin.py index 0c774ef573..354f6a2418 100644 --- a/tests/admin_views/custom_has_permission_admin.py +++ b/tests/admin_views/custom_has_permission_admin.py @@ -4,6 +4,7 @@ A custom AdminSite for AdminViewPermissionsTest.test_login_has_permission(). from django.contrib import admin from django.contrib.auth import get_permission_codename from django.contrib.auth.forms import AuthenticationForm +from django.core.exceptions import ValidationError from . import admin as base_admin, models @@ -12,9 +13,8 @@ PERMISSION_NAME = 'admin_views.%s' % get_permission_codename('change', models.Ar class PermissionAdminAuthenticationForm(AuthenticationForm): def confirm_login_allowed(self, user): - from django import forms if not user.is_active or not (user.is_staff or user.has_perm(PERMISSION_NAME)): - raise forms.ValidationError('permission denied') + raise ValidationError('permission denied') class HasPermissionAdmin(admin.AdminSite): diff --git a/tests/admin_views/forms.py b/tests/admin_views/forms.py index cceb543c41..b25e49246a 100644 --- a/tests/admin_views/forms.py +++ b/tests/admin_views/forms.py @@ -1,6 +1,6 @@ -from django import forms from django.contrib.admin.forms import AdminAuthenticationForm from django.contrib.admin.helpers import ActionForm +from django.core.exceptions import ValidationError class CustomAdminAuthenticationForm(AdminAuthenticationForm): @@ -11,7 +11,7 @@ class CustomAdminAuthenticationForm(AdminAuthenticationForm): def clean_username(self): username = self.cleaned_data.get('username') if username == 'customform': - raise forms.ValidationError('custom form error') + raise ValidationError('custom form error') return username diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py index 4e5f8e3094..88b4b32667 100644 --- a/tests/auth_tests/test_forms.py +++ b/tests/auth_tests/test_forms.py @@ -2,7 +2,6 @@ import datetime import re from unittest import mock -from django import forms from django.contrib.auth.forms import ( AdminPasswordChangeForm, AuthenticationForm, PasswordChangeForm, PasswordResetForm, ReadOnlyPasswordHashField, ReadOnlyPasswordHashWidget, @@ -12,6 +11,7 @@ from django.contrib.auth.models import User from django.contrib.auth.signals import user_login_failed from django.contrib.sites.models import Site from django.core import mail +from django.core.exceptions import ValidationError from django.core.mail import EmailMultiAlternatives from django.forms.fields import CharField, Field, IntegerField from django.test import SimpleTestCase, TestCase, override_settings @@ -372,13 +372,13 @@ class AuthenticationFormTest(TestDataMixin, TestCase): form = AuthenticationFormWithInactiveUsersOkay(None, data) self.assertTrue(form.is_valid()) - # If we want to disallow some logins according to custom logic, - # we should raise a django.forms.ValidationError in the form. + # Raise a ValidationError in the form to disallow some logins according + # to custom logic. class PickyAuthenticationForm(AuthenticationForm): def confirm_login_allowed(self, user): if user.username == "inactive": - raise forms.ValidationError("This user is disallowed.") - raise forms.ValidationError("Sorry, nobody's allowed in.") + raise ValidationError("This user is disallowed.") + raise ValidationError("Sorry, nobody's allowed in.") form = PickyAuthenticationForm(None, data) self.assertFalse(form.is_valid()) @@ -496,7 +496,7 @@ class AuthenticationFormTest(TestDataMixin, TestCase): def test_get_invalid_login_error(self): error = AuthenticationForm().get_invalid_login_error() - self.assertIsInstance(error, forms.ValidationError) + self.assertIsInstance(error, ValidationError) self.assertEqual( error.message, 'Please enter a correct %(username)s and password. Note that both ' diff --git a/tests/forms_tests/field_tests/test_booleanfield.py b/tests/forms_tests/field_tests/test_booleanfield.py index 7ea117a071..b0153e9e0b 100644 --- a/tests/forms_tests/field_tests/test_booleanfield.py +++ b/tests/forms_tests/field_tests/test_booleanfield.py @@ -1,6 +1,7 @@ import pickle -from django.forms import BooleanField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import BooleanField from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_charfield.py b/tests/forms_tests/field_tests/test_charfield.py index cbacf4d0d7..352761deec 100644 --- a/tests/forms_tests/field_tests/test_charfield.py +++ b/tests/forms_tests/field_tests/test_charfield.py @@ -1,6 +1,6 @@ +from django.core.exceptions import ValidationError from django.forms import ( CharField, HiddenInput, PasswordInput, Textarea, TextInput, - ValidationError, ) from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_choicefield.py b/tests/forms_tests/field_tests/test_choicefield.py index 465cfd83a8..cdb264b36c 100644 --- a/tests/forms_tests/field_tests/test_choicefield.py +++ b/tests/forms_tests/field_tests/test_choicefield.py @@ -1,4 +1,5 @@ -from django.forms import ChoiceField, Form, ValidationError +from django.core.exceptions import ValidationError +from django.forms import ChoiceField, Form from django.test import SimpleTestCase from . import FormFieldAssertionsMixin diff --git a/tests/forms_tests/field_tests/test_combofield.py b/tests/forms_tests/field_tests/test_combofield.py index b40e7ba885..481783fe2e 100644 --- a/tests/forms_tests/field_tests/test_combofield.py +++ b/tests/forms_tests/field_tests/test_combofield.py @@ -1,4 +1,5 @@ -from django.forms import CharField, ComboField, EmailField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import CharField, ComboField, EmailField from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_datefield.py b/tests/forms_tests/field_tests/test_datefield.py index 415e1643ee..c0e369cd0c 100644 --- a/tests/forms_tests/field_tests/test_datefield.py +++ b/tests/forms_tests/field_tests/test_datefield.py @@ -1,8 +1,7 @@ from datetime import date, datetime -from django.forms import ( - DateField, Form, HiddenInput, SelectDateWidget, ValidationError, -) +from django.core.exceptions import ValidationError +from django.forms import DateField, Form, HiddenInput, SelectDateWidget from django.test import SimpleTestCase, override_settings from django.utils import translation diff --git a/tests/forms_tests/field_tests/test_datetimefield.py b/tests/forms_tests/field_tests/test_datetimefield.py index 50f1d8e557..f0e6ada3c5 100644 --- a/tests/forms_tests/field_tests/test_datetimefield.py +++ b/tests/forms_tests/field_tests/test_datetimefield.py @@ -1,6 +1,7 @@ from datetime import date, datetime -from django.forms import DateTimeField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import DateTimeField from django.test import SimpleTestCase from django.utils.timezone import get_fixed_timezone, utc diff --git a/tests/forms_tests/field_tests/test_decimalfield.py b/tests/forms_tests/field_tests/test_decimalfield.py index 44ebebcf75..d999dab3f3 100644 --- a/tests/forms_tests/field_tests/test_decimalfield.py +++ b/tests/forms_tests/field_tests/test_decimalfield.py @@ -1,6 +1,7 @@ import decimal -from django.forms import DecimalField, NumberInput, ValidationError, Widget +from django.core.exceptions import ValidationError +from django.forms import DecimalField, NumberInput, Widget from django.test import SimpleTestCase, override_settings from django.utils import formats, translation diff --git a/tests/forms_tests/field_tests/test_emailfield.py b/tests/forms_tests/field_tests/test_emailfield.py index 826524ae62..7bea420a54 100644 --- a/tests/forms_tests/field_tests/test_emailfield.py +++ b/tests/forms_tests/field_tests/test_emailfield.py @@ -1,4 +1,5 @@ -from django.forms import EmailField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import EmailField from django.test import SimpleTestCase from . import FormFieldAssertionsMixin diff --git a/tests/forms_tests/field_tests/test_filefield.py b/tests/forms_tests/field_tests/test_filefield.py index fc5c4b5c1e..261d9f4ca9 100644 --- a/tests/forms_tests/field_tests/test_filefield.py +++ b/tests/forms_tests/field_tests/test_filefield.py @@ -1,7 +1,8 @@ import pickle +from django.core.exceptions import ValidationError from django.core.files.uploadedfile import SimpleUploadedFile -from django.forms import FileField, ValidationError +from django.forms import FileField from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_filepathfield.py b/tests/forms_tests/field_tests/test_filepathfield.py index 2b9683ed84..bdd55c32a5 100644 --- a/tests/forms_tests/field_tests/test_filepathfield.py +++ b/tests/forms_tests/field_tests/test_filepathfield.py @@ -1,6 +1,7 @@ import os.path -from django.forms import FilePathField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import FilePathField from django.test import SimpleTestCase PATH = os.path.dirname(os.path.abspath(__file__)) diff --git a/tests/forms_tests/field_tests/test_floatfield.py b/tests/forms_tests/field_tests/test_floatfield.py index 531396868a..d97bbfc13e 100644 --- a/tests/forms_tests/field_tests/test_floatfield.py +++ b/tests/forms_tests/field_tests/test_floatfield.py @@ -1,4 +1,5 @@ -from django.forms import FloatField, NumberInput, ValidationError +from django.core.exceptions import ValidationError +from django.forms import FloatField, NumberInput from django.test import SimpleTestCase from django.test.utils import override_settings from django.utils import formats, translation diff --git a/tests/forms_tests/field_tests/test_genericipaddressfield.py b/tests/forms_tests/field_tests/test_genericipaddressfield.py index 97a83e38ae..92dbd71a28 100644 --- a/tests/forms_tests/field_tests/test_genericipaddressfield.py +++ b/tests/forms_tests/field_tests/test_genericipaddressfield.py @@ -1,4 +1,5 @@ -from django.forms import GenericIPAddressField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import GenericIPAddressField from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_imagefield.py b/tests/forms_tests/field_tests/test_imagefield.py index b134fedefc..1cb34bf058 100644 --- a/tests/forms_tests/field_tests/test_imagefield.py +++ b/tests/forms_tests/field_tests/test_imagefield.py @@ -1,12 +1,11 @@ import os import unittest +from django.core.exceptions import ValidationError from django.core.files.uploadedfile import ( SimpleUploadedFile, TemporaryUploadedFile, ) -from django.forms import ( - ClearableFileInput, FileInput, ImageField, ValidationError, Widget, -) +from django.forms import ClearableFileInput, FileInput, ImageField, Widget from django.test import SimpleTestCase from . import FormFieldAssertionsMixin diff --git a/tests/forms_tests/field_tests/test_integerfield.py b/tests/forms_tests/field_tests/test_integerfield.py index a0cde40910..0dde7ff488 100644 --- a/tests/forms_tests/field_tests/test_integerfield.py +++ b/tests/forms_tests/field_tests/test_integerfield.py @@ -1,4 +1,5 @@ -from django.forms import IntegerField, Textarea, ValidationError +from django.core.exceptions import ValidationError +from django.forms import IntegerField, Textarea from django.test import SimpleTestCase from . import FormFieldAssertionsMixin diff --git a/tests/forms_tests/field_tests/test_multiplechoicefield.py b/tests/forms_tests/field_tests/test_multiplechoicefield.py index 9ffe461687..6dbeee2685 100644 --- a/tests/forms_tests/field_tests/test_multiplechoicefield.py +++ b/tests/forms_tests/field_tests/test_multiplechoicefield.py @@ -1,4 +1,5 @@ -from django.forms import MultipleChoiceField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import MultipleChoiceField from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_multivaluefield.py b/tests/forms_tests/field_tests/test_multivaluefield.py index fb99451f6c..29287d0b01 100644 --- a/tests/forms_tests/field_tests/test_multivaluefield.py +++ b/tests/forms_tests/field_tests/test_multivaluefield.py @@ -1,9 +1,9 @@ from datetime import datetime +from django.core.exceptions import ValidationError from django.forms import ( CharField, Form, MultipleChoiceField, MultiValueField, MultiWidget, SelectMultiple, SplitDateTimeField, SplitDateTimeWidget, TextInput, - ValidationError, ) from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_regexfield.py b/tests/forms_tests/field_tests/test_regexfield.py index 9e1d2cefc1..51b6fe6fd5 100644 --- a/tests/forms_tests/field_tests/test_regexfield.py +++ b/tests/forms_tests/field_tests/test_regexfield.py @@ -1,6 +1,7 @@ import re -from django.forms import RegexField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import RegexField from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_splitdatetimefield.py b/tests/forms_tests/field_tests/test_splitdatetimefield.py index 940d03b8a9..716e49c343 100644 --- a/tests/forms_tests/field_tests/test_splitdatetimefield.py +++ b/tests/forms_tests/field_tests/test_splitdatetimefield.py @@ -1,6 +1,7 @@ import datetime -from django.forms import SplitDateTimeField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import SplitDateTimeField from django.forms.widgets import SplitDateTimeWidget from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_timefield.py b/tests/forms_tests/field_tests/test_timefield.py index bd8a67992f..a44b10fa07 100644 --- a/tests/forms_tests/field_tests/test_timefield.py +++ b/tests/forms_tests/field_tests/test_timefield.py @@ -1,6 +1,7 @@ import datetime -from django.forms import TimeField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import TimeField from django.test import SimpleTestCase from . import FormFieldAssertionsMixin diff --git a/tests/forms_tests/field_tests/test_typedchoicefield.py b/tests/forms_tests/field_tests/test_typedchoicefield.py index bf0fdb4d47..2c6cd265b5 100644 --- a/tests/forms_tests/field_tests/test_typedchoicefield.py +++ b/tests/forms_tests/field_tests/test_typedchoicefield.py @@ -1,6 +1,7 @@ import decimal -from django.forms import TypedChoiceField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import TypedChoiceField from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_typedmultiplechoicefield.py b/tests/forms_tests/field_tests/test_typedmultiplechoicefield.py index b33d26bdaa..1c97676a87 100644 --- a/tests/forms_tests/field_tests/test_typedmultiplechoicefield.py +++ b/tests/forms_tests/field_tests/test_typedmultiplechoicefield.py @@ -1,6 +1,7 @@ import decimal -from django.forms import TypedMultipleChoiceField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import TypedMultipleChoiceField from django.test import SimpleTestCase diff --git a/tests/forms_tests/field_tests/test_urlfield.py b/tests/forms_tests/field_tests/test_urlfield.py index 52f1080cdc..46cc38acb8 100644 --- a/tests/forms_tests/field_tests/test_urlfield.py +++ b/tests/forms_tests/field_tests/test_urlfield.py @@ -1,4 +1,5 @@ -from django.forms import URLField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import URLField from django.test import SimpleTestCase from . import FormFieldAssertionsMixin diff --git a/tests/forms_tests/field_tests/test_uuidfield.py b/tests/forms_tests/field_tests/test_uuidfield.py index 242b81647d..3f902643e0 100644 --- a/tests/forms_tests/field_tests/test_uuidfield.py +++ b/tests/forms_tests/field_tests/test_uuidfield.py @@ -1,6 +1,7 @@ import uuid -from django.forms import UUIDField, ValidationError +from django.core.exceptions import ValidationError +from django.forms import UUIDField from django.test import SimpleTestCase diff --git a/tests/forms_tests/tests/test_error_messages.py b/tests/forms_tests/tests/test_error_messages.py index f324c08096..d60acc1daa 100644 --- a/tests/forms_tests/tests/test_error_messages.py +++ b/tests/forms_tests/tests/test_error_messages.py @@ -1,10 +1,11 @@ +from django.core.exceptions import ValidationError from django.core.files.uploadedfile import SimpleUploadedFile from django.forms import ( BooleanField, CharField, ChoiceField, DateField, DateTimeField, DecimalField, EmailField, FileField, FloatField, Form, GenericIPAddressField, IntegerField, ModelChoiceField, ModelMultipleChoiceField, MultipleChoiceField, RegexField, - SplitDateTimeField, TimeField, URLField, ValidationError, utils, + SplitDateTimeField, TimeField, URLField, utils, ) from django.template import Context, Template from django.test import SimpleTestCase, TestCase, ignore_warnings diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 16ecf09814..26f8ecafea 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -3359,7 +3359,7 @@ Good luck picking a username that doesn't already exist.

self.assertIsInstance(e, list) self.assertIn('Foo', e) - self.assertIn('Foo', forms.ValidationError(e)) + self.assertIn('Foo', ValidationError(e)) self.assertEqual( e.as_text(), diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py index 0a4be19623..689f972a07 100644 --- a/tests/forms_tests/tests/test_formsets.py +++ b/tests/forms_tests/tests/test_formsets.py @@ -2,9 +2,10 @@ import datetime from collections import Counter from unittest import mock +from django.core.exceptions import ValidationError from django.forms import ( BaseForm, CharField, DateField, FileField, Form, IntegerField, - SplitDateTimeField, ValidationError, formsets, + SplitDateTimeField, formsets, ) from django.forms.formsets import BaseFormSet, all_valid, formset_factory from django.forms.utils import ErrorList diff --git a/tests/forms_tests/tests/test_input_formats.py b/tests/forms_tests/tests/test_input_formats.py index e7aabf74b3..81ba9eb943 100644 --- a/tests/forms_tests/tests/test_input_formats.py +++ b/tests/forms_tests/tests/test_input_formats.py @@ -1,6 +1,7 @@ from datetime import date, datetime, time from django import forms +from django.core.exceptions import ValidationError from django.test import SimpleTestCase, override_settings from django.utils.translation import activate, deactivate @@ -19,7 +20,7 @@ class LocalizedTimeTests(SimpleTestCase): "TimeFields can parse dates in the default format" f = forms.TimeField() # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM') # Parse a time in a valid format, get a parsed result @@ -46,7 +47,7 @@ class LocalizedTimeTests(SimpleTestCase): "Localized TimeFields act as unlocalized widgets" f = forms.TimeField(localize=True) # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM') # Parse a time in a valid format, get a parsed result @@ -69,9 +70,9 @@ class LocalizedTimeTests(SimpleTestCase): "TimeFields with manually specified input formats can accept those formats" f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"]) # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05') # Parse a time in a valid format, get a parsed result @@ -94,9 +95,9 @@ class LocalizedTimeTests(SimpleTestCase): "Localized TimeFields with manually specified input formats can accept those formats" f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True) # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05') # Parse a time in a valid format, get a parsed result @@ -122,7 +123,7 @@ class CustomTimeInputFormatsTests(SimpleTestCase): "TimeFields can parse dates in the default format" f = forms.TimeField() # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05') # Parse a time in a valid format, get a parsed result @@ -145,7 +146,7 @@ class CustomTimeInputFormatsTests(SimpleTestCase): "Localized TimeFields act as unlocalized widgets" f = forms.TimeField(localize=True) # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05') # Parse a time in a valid format, get a parsed result @@ -168,9 +169,9 @@ class CustomTimeInputFormatsTests(SimpleTestCase): "TimeFields with manually specified input formats can accept those formats" f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"]) # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05') # Parse a time in a valid format, get a parsed result @@ -193,9 +194,9 @@ class CustomTimeInputFormatsTests(SimpleTestCase): "Localized TimeFields with manually specified input formats can accept those formats" f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True) # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05') # Parse a time in a valid format, get a parsed result @@ -220,7 +221,7 @@ class SimpleTimeFormatTests(SimpleTestCase): "TimeFields can parse dates in the default format" f = forms.TimeField() # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM') # Parse a time in a valid format, get a parsed result @@ -243,7 +244,7 @@ class SimpleTimeFormatTests(SimpleTestCase): "Localized TimeFields in a non-localized environment act as unlocalized widgets" f = forms.TimeField() # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM') # Parse a time in a valid format, get a parsed result @@ -266,7 +267,7 @@ class SimpleTimeFormatTests(SimpleTestCase): "TimeFields with manually specified input formats can accept those formats" f = forms.TimeField(input_formats=["%I:%M:%S %p", "%I:%M %p"]) # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05') # Parse a time in a valid format, get a parsed result @@ -289,7 +290,7 @@ class SimpleTimeFormatTests(SimpleTestCase): "Localized TimeFields with manually specified input formats can accept those formats" f = forms.TimeField(input_formats=["%I:%M:%S %p", "%I:%M %p"], localize=True) # Parse a time in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05') # Parse a time in a valid format, get a parsed result @@ -321,7 +322,7 @@ class LocalizedDateTests(SimpleTestCase): "DateFields can parse dates in the default format" f = forms.DateField() # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21/12/2010') # ISO formats are accepted, even if not specified in formats.py @@ -347,7 +348,7 @@ class LocalizedDateTests(SimpleTestCase): "Localized DateFields act as unlocalized widgets" f = forms.DateField(localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21/12/2010') # Parse a date in a valid format, get a parsed result @@ -370,11 +371,11 @@ class LocalizedDateTests(SimpleTestCase): "DateFields with manually specified input formats can accept those formats" f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"]) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010-12-21') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21/12/2010') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21.12.2010') # Parse a date in a valid format, get a parsed result @@ -397,11 +398,11 @@ class LocalizedDateTests(SimpleTestCase): "Localized DateFields with manually specified input formats can accept those formats" f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010-12-21') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21/12/2010') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21.12.2010') # Parse a date in a valid format, get a parsed result @@ -427,7 +428,7 @@ class CustomDateInputFormatsTests(SimpleTestCase): "DateFields can parse dates in the default format" f = forms.DateField() # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010-12-21') # Parse a date in a valid format, get a parsed result @@ -450,7 +451,7 @@ class CustomDateInputFormatsTests(SimpleTestCase): "Localized DateFields act as unlocalized widgets" f = forms.DateField(localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010-12-21') # Parse a date in a valid format, get a parsed result @@ -473,9 +474,9 @@ class CustomDateInputFormatsTests(SimpleTestCase): "DateFields with manually specified input formats can accept those formats" f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"]) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21.12.2010') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010-12-21') # Parse a date in a valid format, get a parsed result @@ -498,9 +499,9 @@ class CustomDateInputFormatsTests(SimpleTestCase): "Localized DateFields with manually specified input formats can accept those formats" f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21.12.2010') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010-12-21') # Parse a date in a valid format, get a parsed result @@ -525,7 +526,7 @@ class SimpleDateFormatTests(SimpleTestCase): "DateFields can parse dates in the default format" f = forms.DateField() # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21.12.2010') # Parse a date in a valid format, get a parsed result @@ -548,7 +549,7 @@ class SimpleDateFormatTests(SimpleTestCase): "Localized DateFields in a non-localized environment act as unlocalized widgets" f = forms.DateField() # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('21.12.2010') # Parse a date in a valid format, get a parsed result @@ -571,7 +572,7 @@ class SimpleDateFormatTests(SimpleTestCase): "DateFields with manually specified input formats can accept those formats" f = forms.DateField(input_formats=["%d.%m.%Y", "%d-%m-%Y"]) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010-12-21') # Parse a date in a valid format, get a parsed result @@ -594,7 +595,7 @@ class SimpleDateFormatTests(SimpleTestCase): "Localized DateFields with manually specified input formats can accept those formats" f = forms.DateField(input_formats=["%d.%m.%Y", "%d-%m-%Y"], localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010-12-21') # Parse a date in a valid format, get a parsed result @@ -626,7 +627,7 @@ class LocalizedDateTimeTests(SimpleTestCase): "DateTimeFields can parse dates in the default format" f = forms.DateTimeField() # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM 21/12/2010') # ISO formats are accepted, even if not specified in formats.py @@ -652,7 +653,7 @@ class LocalizedDateTimeTests(SimpleTestCase): "Localized DateTimeFields act as unlocalized widgets" f = forms.DateTimeField(localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM 21/12/2010') # Parse a date in a valid format, get a parsed result @@ -675,11 +676,11 @@ class LocalizedDateTimeTests(SimpleTestCase): "DateTimeFields with manually specified input formats can accept those formats" f = forms.DateTimeField(input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"]) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010-12-21 13:30:05 13:30:05') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM 21/12/2010') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05 21.12.2010') # Parse a date in a valid format, get a parsed result @@ -702,11 +703,11 @@ class LocalizedDateTimeTests(SimpleTestCase): "Localized DateTimeFields with manually specified input formats can accept those formats" f = forms.DateTimeField(input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"], localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010/12/21 13:30:05') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('1:30:05 PM 21/12/2010') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05 21.12.2010') # Parse a date in a valid format, get a parsed result @@ -736,7 +737,7 @@ class CustomDateTimeInputFormatsTests(SimpleTestCase): "DateTimeFields can parse dates in the default format" f = forms.DateTimeField() # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010/12/21 13:30:05') # Parse a date in a valid format, get a parsed result @@ -759,7 +760,7 @@ class CustomDateTimeInputFormatsTests(SimpleTestCase): "Localized DateTimeFields act as unlocalized widgets" f = forms.DateTimeField(localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010/12/21 13:30:05') # Parse a date in a valid format, get a parsed result @@ -782,9 +783,9 @@ class CustomDateTimeInputFormatsTests(SimpleTestCase): "DateTimeFields with manually specified input formats can accept those formats" f = forms.DateTimeField(input_formats=["%m.%d.%Y %H:%M:%S", "%m-%d-%Y %H:%M"]) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05 21.12.2010') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010/12/21 13:30:05') # Parse a date in a valid format, get a parsed result @@ -807,9 +808,9 @@ class CustomDateTimeInputFormatsTests(SimpleTestCase): "Localized DateTimeFields with manually specified input formats can accept those formats" f = forms.DateTimeField(input_formats=["%m.%d.%Y %H:%M:%S", "%m-%d-%Y %H:%M"], localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05 21.12.2010') - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010/12/21 13:30:05') # Parse a date in a valid format, get a parsed result @@ -834,7 +835,7 @@ class SimpleDateTimeFormatTests(SimpleTestCase): "DateTimeFields can parse dates in the default format" f = forms.DateTimeField() # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05 21.12.2010') # Parse a date in a valid format, get a parsed result @@ -857,7 +858,7 @@ class SimpleDateTimeFormatTests(SimpleTestCase): "Localized DateTimeFields in a non-localized environment act as unlocalized widgets" f = forms.DateTimeField() # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('13:30:05 21.12.2010') # Parse a date in a valid format, get a parsed result @@ -880,7 +881,7 @@ class SimpleDateTimeFormatTests(SimpleTestCase): "DateTimeFields with manually specified input formats can accept those formats" f = forms.DateTimeField(input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"]) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010/12/21 13:30:05') # Parse a date in a valid format, get a parsed result @@ -903,7 +904,7 @@ class SimpleDateTimeFormatTests(SimpleTestCase): "Localized DateTimeFields with manually specified input formats can accept those formats" f = forms.DateTimeField(input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"], localize=True) # Parse a date in an unaccepted format; get an error - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): f.clean('2010/12/21 13:30:05') # Parse a date in a valid format, get a parsed result diff --git a/tests/gis_tests/test_geoforms.py b/tests/gis_tests/test_geoforms.py index 269ce03bde..c6fb2f71fb 100644 --- a/tests/gis_tests/test_geoforms.py +++ b/tests/gis_tests/test_geoforms.py @@ -3,7 +3,7 @@ import re from django.contrib.gis import forms from django.contrib.gis.forms import BaseGeometryWidget, OpenLayersWidget from django.contrib.gis.geos import GEOSGeometry -from django.forms import ValidationError +from django.core.exceptions import ValidationError from django.test import SimpleTestCase, override_settings from django.utils.html import escape @@ -39,7 +39,7 @@ class GeometryFieldTest(SimpleTestCase): "Testing GeometryField's handling of null (None) geometries." # Form fields, by default, are required (`required=True`) fld = forms.GeometryField() - with self.assertRaisesMessage(forms.ValidationError, "No geometry value provided."): + with self.assertRaisesMessage(ValidationError, "No geometry value provided."): fld.clean(None) # This will clean None as a geometry (See #10660). @@ -64,7 +64,7 @@ class GeometryFieldTest(SimpleTestCase): pnt_fld.to_python('LINESTRING(0 0, 1 1)') ) # but rejected by `clean` - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): pnt_fld.clean('LINESTRING(0 0, 1 1)') def test_to_python(self): @@ -92,7 +92,7 @@ class GeometryFieldTest(SimpleTestCase): # but raises a ValidationError for any other string for geo_input in bad_inputs: with self.subTest(geo_input=geo_input): - with self.assertRaises(forms.ValidationError): + with self.assertRaises(ValidationError): fld.to_python(geo_input) def test_to_python_different_map_srid(self): diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py index 1d4b9f9e70..8f41ce9c40 100644 --- a/tests/model_forms/test_modelchoicefield.py +++ b/tests/model_forms/test_modelchoicefield.py @@ -1,7 +1,7 @@ import datetime from django import forms -from django.core.validators import ValidationError +from django.core.exceptions import ValidationError from django.forms.models import ModelChoiceIterator from django.forms.widgets import CheckboxSelectMultiple from django.template import Context, Template diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 0caf6e9bad..00e68d1dae 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -5,10 +5,9 @@ from unittest import mock, skipUnless from django import forms from django.core.exceptions import ( - NON_FIELD_ERRORS, FieldError, ImproperlyConfigured, + NON_FIELD_ERRORS, FieldError, ImproperlyConfigured, ValidationError, ) from django.core.files.uploadedfile import SimpleUploadedFile -from django.core.validators import ValidationError from django.db import connection, models from django.db.models.query import EmptyQuerySet from django.forms.models import ( @@ -2619,7 +2618,7 @@ class CustomCleanTests(TestCase): def clean(self): if not self.cleaned_data['left'] == self.cleaned_data['right']: - raise forms.ValidationError('Left and right should be equal') + raise ValidationError('Left and right should be equal') return self.cleaned_data form = TripleFormWithCleanOverride({'left': 1, 'middle': 2, 'right': 1}) diff --git a/tests/test_client/views.py b/tests/test_client/views.py index c2aef76508..b56ea458a2 100644 --- a/tests/test_client/views.py +++ b/tests/test_client/views.py @@ -4,8 +4,9 @@ from xml.dom.minidom import parseString from django.contrib.auth.decorators import login_required, permission_required from django.core import mail +from django.core.exceptions import ValidationError from django.forms import fields -from django.forms.forms import Form, ValidationError +from django.forms.forms import Form from django.forms.formsets import BaseFormSet, formset_factory from django.http import ( HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed,