From 01219bddfeea16f8d6dd6d65d0d84581cf183a3f Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Thu, 4 Apr 2024 13:16:28 -0700 Subject: [PATCH] Allow arbitrary Python object in YAML config I'm not convinced this is a good idea, but MkDocs does it so we have to as well... --- mike/mkdocs_utils.py | 2 +- test/unit/test_mkdocs_utils.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/mike/mkdocs_utils.py b/mike/mkdocs_utils.py index 171ec2a..5051607 100644 --- a/mike/mkdocs_utils.py +++ b/mike/mkdocs_utils.py @@ -28,7 +28,7 @@ def representer(dumper, data): return data.node -class RoundTripLoader(yaml.SafeLoader): +class RoundTripLoader(yaml.Loader): pass diff --git a/test/unit/test_mkdocs_utils.py b/test/unit/test_mkdocs_utils.py index 09e12d2..321a595 100644 --- a/test/unit/test_mkdocs_utils.py +++ b/test/unit/test_mkdocs_utils.py @@ -184,6 +184,22 @@ def test_round_trip(self): '- baz:\n option: !ENV [variable, default]\n') self.assertEqual(self.out.getvalue(), expected) + def test_python_tag(self): + cfg = 'plugins:\n - foo:\n option: !!python/none' + with mock.patch('builtins.open', + mock_open_files({'mkdocs.yml': cfg})), \ + mock.patch('mike.mkdocs_utils.NamedTemporaryFile', + return_value=self.out), \ + mock.patch('os.remove') as mremove: + with mkdocs_utils.inject_plugin('mkdocs.yml') as f: + self.assertEqual(f, self.out.name) + newcfg = yaml.safe_load(self.out.getvalue()) + mremove.assert_called_once() + + self.assertEqual(newcfg, {'plugins': [ + 'mike', {'foo': {'option': None}}, + ]}) + def test_inherit(self): main_cfg = 'INHERIT: mkdocs-base.yml\nplugins:\n foo: {}\n' base_cfg = 'plugins:\n bar: {}\n'