From bb91bcd76dedcd7f276ab52104e14fd721d3824f Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Mon, 8 Aug 2022 19:30:08 +0200 Subject: [PATCH] Rework SubConfig so it doesn't share mutable state (#2916) --- mkdocs/config/config_options.py | 16 +++++++--------- mkdocs/tests/config/config_tests.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/mkdocs/config/config_options.py b/mkdocs/config/config_options.py index aea40dd0..8bec5b42 100644 --- a/mkdocs/config/config_options.py +++ b/mkdocs/config/config_options.py @@ -49,19 +49,17 @@ class BaseConfigOption: """ -class SubConfig(BaseConfigOption, Config): +class SubConfig(BaseConfigOption): def __init__(self, *config_options): - BaseConfigOption.__init__(self) - Config.__init__(self, config_options) + super().__init__() self.default = {} - - def validate(self, value): - self.load_dict(value) - return self.run_validation(value) + self.config_options = config_options def run_validation(self, value): - Config.validate(self) - return self + config = Config(self.config_options) + config.load_dict(value) + config.validate() + return config class ConfigItems(BaseConfigOption): diff --git a/mkdocs/tests/config/config_tests.py b/mkdocs/tests/config/config_tests.py index eceef26e..908e6da6 100644 --- a/mkdocs/tests/config/config_tests.py +++ b/mkdocs/tests/config/config_tests.py @@ -283,6 +283,26 @@ class ConfigTests(unittest.TestCase): self.assertEqual(len(errors), 1) self.assertEqual(warnings, []) + SUBCONFIG_TEST_SCHEMA = { + "items": mkdocs.config.config_options.ConfigItems( + ("value", mkdocs.config.config_options.Type(str)), + ), + }.items() + + def test_subconfig_with_multiple_items(self): + # This had a bug where subsequent items would get merged into the same dict. + conf = config.Config(schema=self.SUBCONFIG_TEST_SCHEMA) + conf.load_dict( + { + 'items': [ + {'value': 'a'}, + {'value': 'b'}, + ] + } + ) + conf.validate() + self.assertEqual(conf['items'], [{'value': 'a'}, {'value': 'b'}]) + def test_multiple_markdown_config_instances(self): # This had a bug where an extension config would persist to separate # config instances that didn't specify extensions.