From cd8011633934496f5021660bb09ce6bd8f5d612a Mon Sep 17 00:00:00 2001 From: Thea Flowers Date: Thu, 9 May 2024 10:27:04 -0400 Subject: [PATCH] Fix crash when mkdocs_theme.yml is empty Fixes #3699 --- mkdocs/tests/theme_tests.py | 19 +++++++++++++++++++ mkdocs/theme.py | 3 +++ 2 files changed, 22 insertions(+) diff --git a/mkdocs/tests/theme_tests.py b/mkdocs/tests/theme_tests.py index f9989fb3..ddc526bf 100644 --- a/mkdocs/tests/theme_tests.py +++ b/mkdocs/tests/theme_tests.py @@ -104,3 +104,22 @@ class ThemeTests(unittest.TestCase): ], ) self.assertEqual(theme.static_templates, {'sitemap.xml', 'child.html', 'parent.html'}) + + def test_empty_config_file(self): + # Test for themes with *empty* mkdocs_theme.yml. + # See https://github.com/mkdocs/mkdocs/issues/3699 + m = mock.Mock( + # yaml.load returns "None" for an empty file + side_effect=[None] + ) + with mock.patch('yaml.load', m) as m: + theme = Theme(name='mkdocs') + # Should only have the default name and locale __vars set in + # Theme.__init__() + self.assertEqual( + dict(theme), + { + 'name': 'mkdocs', + 'locale': parse_locale('en'), + }, + ) diff --git a/mkdocs/theme.py b/mkdocs/theme.py index bf587201..b45f1079 100644 --- a/mkdocs/theme.py +++ b/mkdocs/theme.py @@ -138,6 +138,9 @@ class Theme(MutableMapping[str, Any]): f"Please upgrade to a current version of the theme." ) + if theme_config is None: + theme_config = {} + log.debug(f"Loaded theme configuration for '{name}' from '{file_path}': {theme_config}") if parent_theme := theme_config.pop('extends', None):