diff options
| author | Adam Johnson <me@adamj.eu> | 2025-05-11 23:07:47 +0200 |
|---|---|---|
| committer | nessita <124304+nessita@users.noreply.github.com> | 2025-05-13 21:42:19 -0300 |
| commit | 4647e2b8663cbd22a07af70bf0f8540946763851 (patch) | |
| tree | 13f94549c5f70b31ebdb3e031242941ca2338e63 /tests | |
| parent | 0f94ecd49d1af92d47bfa690ca39d4d71b5091cb (diff) | |
Refs #36383 -- Extended DeconstructibleSerializer to support non-identifier keyword arguments.
In Python, keyword arguments must normally be valid identifiers (i.e.,
variable names that follow Python's naming rules). However, Python dicts
can have keys that aren't valid identifiers, like "foo-bar" or "123foo".
This commit ensures that keyword arguments that are nt valid
identifiers, are properly handled when deconstructing an object.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_writer.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 6afee23da2..fc3d3bc909 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -41,6 +41,13 @@ class DeconstructibleInstances: return ("DeconstructibleInstances", [], {}) +@deconstructible +class DeconstructibleArbitrary: + def __init__(self, *args, **kwargs): + self.args = args + self.kwargs = kwargs + + class Money(decimal.Decimal): def deconstruct(self): return ( @@ -1143,6 +1150,24 @@ class WriterTests(SimpleTestCase): "models.CharField(default=migrations.test_writer.DeconstructibleInstances)", ) + def test_serialize_non_identifier_keyword_args(self): + instance = DeconstructibleArbitrary( + **{"kebab-case": 1, "my_list": [1, 2, 3], "123foo": {"456bar": set()}}, + regular="kebab-case", + **{"simple": 1, "complex": 3.1416}, + ) + string, imports = MigrationWriter.serialize(instance) + self.assertEqual( + string, + "migrations.test_writer.DeconstructibleArbitrary(complex=3.1416, " + "my_list=[1, 2, 3], regular='kebab-case', simple=1, " + "**{'123foo': {'456bar': set()}, 'kebab-case': 1})", + ) + self.assertEqual(imports, {"import migrations.test_writer"}) + result = self.serialize_round_trip(instance) + self.assertEqual(result.args, instance.args) + self.assertEqual(result.kwargs, instance.kwargs) + def test_register_serializer(self): class ComplexSerializer(BaseSerializer): def serialize(self): |
