Skip to content

Don't crash on front matter with non-string mapping keys - #133

Open
eeshsaxena wants to merge 1 commit into
eyeseast:mainfrom
eeshsaxena:fix/non-string-metadata-keys
Open

Don't crash on front matter with non-string mapping keys#133
eeshsaxena wants to merge 1 commit into
eyeseast:mainfrom
eeshsaxena:fix/non-string-metadata-keys

Conversation

@eeshsaxena

Copy link
Copy Markdown

frontmatter.loads crashes on a document whose front matter has a non-string key:

>>> import frontmatter
>>> frontmatter.loads("---\n1: one\n---\nbody")
TypeError: keywords must be strings

Integers, booleans and dates are all valid YAML (and TOML) mapping keys, but loads builds the post with Post(content, handler, **metadata), and splatting a dict with a non-string key into keyword arguments raises TypeError: keywords must be strings, even though the front matter itself parsed fine.

The fix assigns the parsed metadata dict to the Post directly instead of splatting it, so non-string keys are preserved and round-trip through dumps/loads. String-key documents are unaffected. Added a test.

loads did Post(content, handler, **metadata), which raises
'keywords must be strings' when the parsed metadata has non-string keys.
Integers, booleans and dates are all valid YAML/TOML mapping keys, so a
document like '---\n1: one\n---' blew up even though the front matter is valid.
Assign the metadata dict to the Post directly instead of splatting it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant