From ae5d00f7a139b9ce2be8c33b043aad42196d62bc Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 20 Apr 2022 14:00:38 +0100 Subject: [PATCH] Fix deep_update for Python 3.10 This is only used by the Elasticsearch backend, so we were missing test runs for it over all Python versions. --- wagtail/core/tests/test_utils.py | 36 ++++++++++++++++++++++++++++++++ wagtail/utils/utils.py | 4 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/wagtail/core/tests/test_utils.py b/wagtail/core/tests/test_utils.py index 4782c31162..f07b6e1e06 100644 --- a/wagtail/core/tests/test_utils.py +++ b/wagtail/core/tests/test_utils.py @@ -10,6 +10,7 @@ from wagtail.core.utils import ( accepts_kwarg, camelcase_to_underscore, cautious_slugify, find_available_slug, get_content_languages, get_dummy_request, get_supported_content_language_variant, multigetattr, safe_snake_case, string_to_ascii) +from wagtail.utils.utils import deep_update class TestCamelCaseToUnderscore(TestCase): @@ -374,3 +375,38 @@ class TestGetDummyRequest(TestCase): request = get_dummy_request(site=site) self.assertEqual(request.get_host(), 'other.example.com:8888') + + +class TestDeepUpdate(TestCase): + def test_deep_update(self): + val = { + "captain": "picard", + "beverage": { + "type": "coffee", + "temperature": "hot", + }, + } + + deep_update( + val, + { + "beverage": { + "type": "tea", + "variant": "earl grey", + }, + "starship": "enterprise", + }, + ) + + self.assertEqual( + val, + { + "captain": "picard", + "beverage": { + "type": "tea", + "variant": "earl grey", + "temperature": "hot", + }, + "starship": "enterprise", + }, + ) diff --git a/wagtail/utils/utils.py b/wagtail/utils/utils.py index 2ecd07ff8b..62be69ca5f 100644 --- a/wagtail/utils/utils.py +++ b/wagtail/utils/utils.py @@ -1,4 +1,4 @@ -import collections +from collections.abc import Mapping def deep_update(source, overrides): @@ -7,7 +7,7 @@ def deep_update(source, overrides): Modify ``source`` in place. """ for key, value in overrides.items(): - if isinstance(value, collections.Mapping) and value: + if isinstance(value, Mapping) and value: returned = deep_update(source.get(key, {}), value) source[key] = returned else: