From d7d80040c1e3a282435155f9974e1eafa8da66e5 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sun, 9 Jul 2023 13:47:21 -0400 Subject: [PATCH] Made UniqueConstraint raise TypeError for type violations. --- django/db/models/constraints.py | 8 ++++---- tests/constraints/tests.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/django/db/models/constraints.py b/django/db/models/constraints.py index e5136f89f5..56d547e6b0 100644 --- a/django/db/models/constraints.py +++ b/django/db/models/constraints.py @@ -217,15 +217,15 @@ class UniqueConstraint(BaseConstraint): "Use django.contrib.postgres.indexes.OpClass() instead." ) if not isinstance(deferrable, (NoneType, Deferrable)): - raise ValueError( + raise TypeError( "UniqueConstraint.deferrable must be a Deferrable instance." ) if not isinstance(include, (NoneType, list, tuple)): - raise ValueError("UniqueConstraint.include must be a list or tuple.") + raise TypeError("UniqueConstraint.include must be a list or tuple.") if not isinstance(opclasses, (list, tuple)): - raise ValueError("UniqueConstraint.opclasses must be a list or tuple.") + raise TypeError("UniqueConstraint.opclasses must be a list or tuple.") if not isinstance(nulls_distinct, (NoneType, bool)): - raise ValueError("UniqueConstraint.nulls_distinct must be a bool.") + raise TypeError("UniqueConstraint.nulls_distinct must be a bool.") if opclasses and len(fields) != len(opclasses): raise ValueError( "UniqueConstraint.fields and UniqueConstraint.opclasses must " diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py index 5fde168af4..7e3d20e40c 100644 --- a/tests/constraints/tests.py +++ b/tests/constraints/tests.py @@ -1046,7 +1046,7 @@ class UniqueConstraintTests(TestCase): def test_invalid_defer_argument(self): message = "UniqueConstraint.deferrable must be a Deferrable instance." - with self.assertRaisesMessage(ValueError, message): + with self.assertRaisesMessage(TypeError, message): models.UniqueConstraint( fields=["name"], name="name_invalid", @@ -1064,7 +1064,7 @@ class UniqueConstraintTests(TestCase): def test_invalid_include_argument(self): msg = "UniqueConstraint.include must be a list or tuple." - with self.assertRaisesMessage(ValueError, msg): + with self.assertRaisesMessage(TypeError, msg): models.UniqueConstraint( name="uniq_include", fields=["field"], @@ -1073,7 +1073,7 @@ class UniqueConstraintTests(TestCase): def test_invalid_opclasses_argument(self): msg = "UniqueConstraint.opclasses must be a list or tuple." - with self.assertRaisesMessage(ValueError, msg): + with self.assertRaisesMessage(TypeError, msg): models.UniqueConstraint( name="uniq_opclasses", fields=["field"], @@ -1082,7 +1082,7 @@ class UniqueConstraintTests(TestCase): def test_invalid_nulls_distinct_argument(self): msg = "UniqueConstraint.nulls_distinct must be a bool." - with self.assertRaisesMessage(ValueError, msg): + with self.assertRaisesMessage(TypeError, msg): models.UniqueConstraint( name="uniq_opclasses", fields=["field"], nulls_distinct="NULLS DISTINCT" )