Rework SubConfig so it doesn't share mutable state (#2916)

This commit is contained in:
Oleh Prypin
2022-08-08 19:30:08 +02:00
committed by GitHub
parent 98672b85f6
commit bb91bcd76d
2 changed files with 27 additions and 9 deletions

View File

@@ -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):

View File

@@ -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.