From 6dc250790774acffa18420365eb61a1c876cb876 Mon Sep 17 00:00:00 2001 From: Justin Mayer Date: Sun, 11 Oct 2026 21:20:25 +0200 Subject: [PATCH] fix: Clarify errors for missing or empty metadata Report whether required metadata is missing, or present but empty, instead of the vague "could not find information about" message, which was misleading when a field such as `:title:` was present but empty. --- pelican/contents.py | 9 ++++++--- pelican/tests/test_contents.py | 13 +++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pelican/contents.py b/pelican/contents.py index 9254701b..ee357161 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -170,13 +170,16 @@ class Content: return self.source_path or repr(self) def _has_valid_mandatory_properties(self) -> bool: - """Test mandatory properties are set.""" + """Test mandatory properties are set and not empty.""" for prop in self.mandatory_properties: - if not hasattr(self, prop) or getattr(self, prop) in (None, ""): + if not hasattr(self, prop): logger.error( - "Skipping %s: could not find information about '%s'", self, prop + "Skipping %s: required metadata '%s' is missing", self, prop ) return False + if getattr(self, prop) in (None, ""): + logger.error("Skipping %s: required metadata '%s' is empty", self, prop) + return False return True def _has_valid_slug(self) -> bool: diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py index 9dfa53f0..0ea55154 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -81,17 +81,22 @@ class TestPage(TestBase): self.assertEqual(page.content, TEST_CONTENT) def test_mandatory_properties(self): - # If the title is not set, must throw an exception. + # A missing title must make the content invalid. page = Page("content") self.assertFalse(page._has_valid_mandatory_properties()) - # An empty or `None` title must be treated the same as a missing title. + self.assertLogCountEqual( + count=1, + msg="Skipping .*: required metadata 'title' is missing", + level=logging.ERROR, + ) + # So must an empty or `None` title, with a message saying it is empty. for title in ("", None): with self.subTest(title=title): page = Page("content", metadata={"title": title}) self.assertFalse(page._has_valid_mandatory_properties()) self.assertLogCountEqual( - count=3, - msg="Skipping .*: could not find information about 'title'", + count=2, + msg="Skipping .*: required metadata 'title' is empty", level=logging.ERROR, ) page = Page("content", metadata={"title": "foobar"})