From 4c36414323d9035a93a2b7a54ff5281116950fe1 Mon Sep 17 00:00:00 2001 From: Federico Bond Date: Mon, 25 Jun 2018 09:30:58 +0200 Subject: [PATCH] Fixed #29517 -- Rephrased error message when passing incorrect kwarg to model constructor --- django/db/models/base.py | 2 +- tests/basic/tests.py | 2 +- tests/properties/tests.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index fe8e7d3b0a..8cdd8f3d01 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -481,7 +481,7 @@ class Model(metaclass=ModelBase): except (AttributeError, FieldDoesNotExist): pass for kwarg in kwargs: - raise TypeError("'%s' is an invalid keyword argument for this function" % kwarg) + raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, list(kwargs)[0])) super().__init__() post_init.send(sender=cls, instance=self) diff --git a/tests/basic/tests.py b/tests/basic/tests.py index 76d92a7591..d82fc54675 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -65,7 +65,7 @@ class ModelInstanceCreationTests(TestCase): self.assertEqual(a.headline, 'Fourth article') def test_cannot_create_instance_with_invalid_kwargs(self): - with self.assertRaisesMessage(TypeError, "'foo' is an invalid keyword argument for this function"): + with self.assertRaisesMessage(TypeError, "Article() got an unexpected keyword argument 'foo'"): Article( id=None, headline='Some headline', diff --git a/tests/properties/tests.py b/tests/properties/tests.py index a50a825953..9ba28c0d0d 100644 --- a/tests/properties/tests.py +++ b/tests/properties/tests.py @@ -18,7 +18,7 @@ class PropertyTests(TestCase): setattr(self.a, 'full_name', 'Paul McCartney') # And cannot be used to initialize the class. - with self.assertRaisesMessage(TypeError, "'full_name' is an invalid keyword argument"): + with self.assertRaisesMessage(TypeError, "Person() got an unexpected keyword argument 'full_name'"): Person(full_name='Paul McCartney') # But "full_name_2" has, and it can be used to initialize the class.