Repository: mkdocs/mkdocs

Issue:
Config.validate keeps 'mdx_configs' values across different instances
```python
import mkdocs.config

# 1. OK
conf = mkdocs.config.Config(mkdocs.config.DEFAULT_SCHEMA)
conf.load_dict({'site_name': 'foo'})
conf.validate()
assert conf['mdx_configs'].get('toc') == None

# 2. OK
conf = mkdocs.config.Config(mkdocs.config.DEFAULT_SCHEMA)
conf.load_dict({'site_name': 'foo', 'markdown_extensions': [{"toc": {"permalink": "aaa"}}]})
conf.validate()
assert conf['mdx_configs'].get('toc') == {'permalink': 'aaa'}

# 3. Identical to 1 but not OK.
conf = mkdocs.config.Config(mkdocs.config.DEFAULT_SCHEMA)
conf.load_dict({'site_name': 'foo'})
conf.validate()
assert conf['mdx_configs'].get('toc') == None
# fails, actually is {'permalink': 'aaa'}
```

Discussion:
waylan: Thank you for the work on this. As I discuss in #2289, this only puts a bandaid on the immediate problem. But perhaps we should address the global issue instead. I'll wait to see what we decide on that before moving forward with this. In any event, if we decide not to go with a global solution, this looks good and can be merged if/when the decision is made.

Patch:
diff --git a/mkdocs/config/config_options.py b/mkdocs/config/config_options.py
index 5e654295..aea40dd0 100644
--- a/mkdocs/config/config_options.py
+++ b/mkdocs/config/config_options.py
@@ -623,7 +623,6 @@ class MarkdownExtensions(OptionallyRequired):
         super().__init__(**kwargs)
         self.builtins = builtins or []
         self.configkey = configkey
-        self.configdata = {}
 
     def validate_ext_cfg(self, ext, cfg):
         if not isinstance(ext, str):
@@ -635,6 +634,7 @@ class MarkdownExtensions(OptionallyRequired):
         self.configdata[ext] = cfg
 
     def run_validation(self, value):
+        self.configdata = {}
         if not isinstance(value, (list, tuple, dict)):
             raise ValidationError('Invalid Markdown Extensions configuration')
         extensions = []


Test patch:
diff --git a/mkdocs/tests/config/config_tests.py b/mkdocs/tests/config/config_tests.py
index fe24fd3a..eceef26e 100644
--- a/mkdocs/tests/config/config_tests.py
+++ b/mkdocs/tests/config/config_tests.py
@@ -283,18 +283,24 @@ class ConfigTests(unittest.TestCase):
             self.assertEqual(len(errors), 1)
             self.assertEqual(warnings, [])
 
-    def testConfigInstancesUnique(self):
-        conf = mkdocs.config.Config(mkdocs.config.defaults.get_schema())
-        conf.load_dict({'site_name': 'foo'})
-        conf.validate()
-        self.assertIsNone(conf['mdx_configs'].get('toc'))
+    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.
+        schema = config.defaults.get_schema()
 
-        conf = mkdocs.config.Config(mkdocs.config.defaults.get_schema())
-        conf.load_dict({'site_name': 'foo', 'markdown_extensions': [{"toc": {"permalink": "aaa"}}]})
+        conf = config.Config(schema=schema)
+        conf.load_dict(
+            {
+                'site_name': 'Example',
+                'markdown_extensions': [{'toc': {'permalink': '##'}}],
+            }
+        )
         conf.validate()
-        self.assertEqual(conf['mdx_configs'].get('toc'), {'permalink': 'aaa'})
+        self.assertEqual(conf['mdx_configs'].get('toc'), {'permalink': '##'})
 
-        conf = mkdocs.config.Config(mkdocs.config.defaults.get_schema())
-        conf.load_dict({'site_name': 'foo'})
+        conf = config.Config(schema=schema)
+        conf.load_dict(
+            {'site_name': 'Example'},
+        )
         conf.validate()
         self.assertIsNone(conf['mdx_configs'].get('toc'))