Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Type error with YAML dates in frontmatter #49

Merged
merged 5 commits into from
Apr 12, 2023

Conversation

jgraichen
Copy link
Contributor

When valid YAML dates or times are added to the page frontmatter, the YAML parser will already convert them to datetime.date or datetime.datetime objects.

Example:

---
date: 2023-04-12
---

This resulted in #strptime raising a TypeError because datetime.date isn't a string. This commit changes the plugin code to always accept a native datetime.date or datetime.datetime, even if meta_time_format is not set, and handle them without string parsing.

Close #48

When valid YAML dates or times are added to the page frontmatter, the
YAML parser will already convert them to datetime.date or
datetime.datetime objects.

Example:

    ---
    date: 2023-04-12
    ---

This resulted in #strptime raising a TypeError because datetime.date
isn't a string. This commit changes the plugin code to always accept a
native datetime.date or datetime.datetime, even if meta_time_format is
not set, and handle them without string parsing.

Close liang2kl#48
timestamp = self._parse_time(page.meta["date"])
if not timestamp:
else:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If time is included in the page meta but is not a date time, and meta_time_format is not set, this will end up with timestamp = None.

For example:

---
time: 5 minutes
date: 2024-04-12
---

The time meta could be meant for another plugin.

Using is None would try with "date" if "time" is present but cannot be parsed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I understand. Thank you for the explanation.

@liang2kl
Copy link
Owner

By the way, could you please update the docs? The related stuff should be in the "Global Settings" section of index.md.

liang2kl
liang2kl previously approved these changes Apr 12, 2023
@jgraichen
Copy link
Contributor Author

I added an extra test about the fallback if time: is invalid and some more explicit return paths for the _parse_time method.

By the way, could you please update the docs? The related stuff should be in the "Global Settings" section of index.md.

I've added a note.

@liang2kl liang2kl merged commit 5a0b4ad into liang2kl:main Apr 12, 2023
@jgraichen jgraichen deleted the fix/yaml-datetime branch April 12, 2023 17:28
liang2kl added a commit that referenced this pull request Apr 12, 2023
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.

[BUG] TypeError: strptime() argument 1 must be str, not datetime.date
2 participants