Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion skillscope/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,13 @@ def _frontmatter(text: str) -> tuple[dict | None, str, str]:

import yaml # noqa: PLC0415 -- keeps the run path stdlib-only

# Any exception, not only YAMLError: PyYAML rejects some input with
# ValueError, AttributeError or KeyError (a tag whose value it cannot
# construct, such as `!!timestamp x`), and deep nesting raises
# RecursionError. `safe_load` of a string does nothing else that can fail.
try:
declared = yaml.safe_load(match.group("frontmatter"))
except yaml.YAMLError as exc:
except Exception as exc: # noqa: BLE001 -- whatever safe_load raises is the frontmatter's fault
return None, "", f"frontmatter is not valid YAML: {exc}"
if not isinstance(declared, dict):
return None, "", (
Expand Down
17 changes: 17 additions & 0 deletions tests/test_skillscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,23 @@ def test_frontmatter_that_is_not_yaml_is_reported_as_such(self) -> None:
self.declares("name: [demo-skill\ndescription: broken")
self.assertTrue(any("not valid YAML" in e for e in structure.errors()))

def test_yaml_the_parser_rejects_without_a_yaml_error_is_reported_too(self) -> None:
# Each of these is rejected with something other than a YAMLError:
# ValueError, AttributeError, KeyError, and RecursionError for nesting
# deeper than the interpreter allows. Each used to end the structural
# check with a traceback.
for name, frontmatter in (
("date", "name: demo-skill\ndescription: d\nupdated: 2001-13-45"),
("timestamp tag", "name: demo-skill\ndescription: d\nupdated: !!timestamp x"),
("bool tag", "name: demo-skill\ndescription: d\ndraft: !!bool x"),
("nesting", "name: demo-skill\ndescription: " + "[" * 2_000),
):
with self.subTest(name):
self.declares(frontmatter)
errors = structure.errors()
self.assertEqual(len(errors), 1, errors)
self.assertIn("not valid YAML", errors[0])

def test_frontmatter_that_is_not_a_mapping_is_reported(self) -> None:
self.declares("- demo-skill\n- a list, not a mapping")
self.assertTrue(any("must be a mapping" in e for e in structure.errors()))
Expand Down
Loading