-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Blog plugin failing with TypeError: can't compare offset-naive and offset-aware datetimes
#7705
Comments
Hello @perpil, If you're willing to modify the the Python sources please try this, before the line with the error: for post in self._resolve_posts(files, config):
print(post.file.src_uri)
print(" ", post.meta["date"])
print(" ", post.config.date.created) this might show what the input was that caused the error. I agree with your solution from the reference there needs to be some normalization. |
Here's the output if I put the code you suggest prior to the line that causes the error:
Surprisingly, for post in self._resolve_posts(files, config):
print(post.file.src_uri)
print(" ", post.meta["date"])
print(" ", post.config.date.created)
self.blog.posts = sorted(
self._resolve_posts(files, config),
key = lambda post: (
post.config.pin,
post.config.date.created
),
reverse = True
) Putting:
|
Thanks for the follow up:
this is the only file with the
This needs to be imported via |
That was it! I had a date in the frontmatter that was set to: Here's a repro: 9.5.44+insiders.4.53.14-blog-plugin-breaks-with-timestamp-frontmatter.zip I've also confirmed that adding For now I've removed the timestamp from the frontmatter date on the offending file and am unblocked. |
Thanks for reporting. Could you PR the necessary change? If so, please send a PR for the community edition, as I believe all functionality that is impacted was already released. |
I can do a PR for this change if it's just hardcoding the timezone. A couple of quick questions before I do.
|
Ooph, good questions. So, we can only work with what we get from YAML. We're parsing the front matter here: mkdocs-material/src/plugins/blog/structure/options.py Lines 53 to 77 in 596aeea
I'm not sure what the best way forward is, but my educated guess (without diving too deep into this topic, as I'm currently fighting other battles) is that if we would support timezones, we should normalize them to UTC, i.e., |
Fixes squidfunk#7705 Normalize datetime values to UTC in blog plugin to handle offset-naive and offset-aware datetimes correctly. * Import `timezone` from `datetime` in `material/plugins/blog/structure/options.py`. * Modify `pre_validation` method to set `tzinfo=timezone.utc` for datetime values. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/squidfunk/mkdocs-material/issues/7705?shareId=XXXX-XXXX-XXXX-XXXX).
Fixes squidfunk#7705 + Move changes to src directory
Released as part of 9.5.45. |
@squidfunk I actually started getting this error with 9.5.45. It works fine with 9.5.38. Looks like the same bug which might be still not fixed. I can open a separate bug if needed. fails with 9.5.45pip list | findstr mkdocs-material
mkdocs-material 9.5.45
mkdocs-material-extensions 1.3.1
mkdocs build --strict
INFO - Cleaning site directory
INFO - Building documentation to directory: C:\some_directory...
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "D:\temp\venv-blog-dev\scripts\mkdocs.exe\__main__.py", line 7, in <module>
File "D:\temp\venv-blog-dev\Lib\site-packages\click\core.py", line 1157, in __call__
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\temp\venv-blog-dev\Lib\site-packages\click\core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "D:\temp\venv-blog-dev\Lib\site-packages\click\core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\temp\venv-blog-dev\Lib\site-packages\click\core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\temp\venv-blog-dev\Lib\site-packages\click\core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\temp\venv-blog-dev\Lib\site-packages\mkdocs\__main__.py", line 288, in build_command
build.build(cfg, dirty=not clean)
File "D:\temp\venv-blog-dev\Lib\site-packages\mkdocs\commands\build.py", line 292, in build
files = config.plugins.on_files(files, config=config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\temp\venv-blog-dev\Lib\site-packages\mkdocs\plugins.py", line 593, in on_files
return self.run_event('files', files, config=config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\temp\venv-blog-dev\Lib\site-packages\mkdocs\plugins.py", line 566, in run_event
result = method(item, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^
File "D:\temp\venv-blog-dev\Lib\site-packages\material\plugins\blog\plugin.py", line 133, in on_files
self.blog.posts = sorted(
^^^^^^^
TypeError: can't compare offset-naive and offset-aware datetimes works fine with the previous version I had - 9.5.38pip install mkdocs-material==9.5.38
pip list | findstr mkdocs-material
mkdocs-material 9.5.38
mkdocs-material-extensions 1.3.1
mkdocs build --strict
INFO - Cleaning site directory
INFO - Building documentation to directory: C:\some_directory
INFO - Documentation built in 4.47 seconds |
@mvelikikh thanks for reporting. Could you please create a new bug report with a minimal reproduction, as it's related but another error in another location. Maybe @perpil can take a look, as he's the one that fixed the other issue |
The error seems to be at the same place, off by one line but I guess this is the difference between the Insiders version. We discussed the change to if isinstance(value, date):
config[key_name][key] = datetime.combine(value, time()).replace(tzinfo=timezone.utc) However, the PR doesn't if isinstance(value, datetime):
continue I guess that's the issue, haven't run any tests. |
PRs appreciated! |
@kamilkrzyskow and @squidfunk, thanks for the replies. I have constructed a minimally reproduced example, and opened a new issue: #7725 It seems that the error is happening only when both of the date created formats are present - it works if I keep only one format:
|
Ah! I thought that by making the change here it would be a more central location, but now I'm realizing the date I was testing with had a Z at the end of it making it timezone aware and the times it is choking on don't have any timezone info. I'll attempt to change it only if it is missing the timezone i.e. if datetime.tzinfo is None:
datetime.replace(tzinfo=timezone.utc) |
Context
I have been successfully building my blog using the blog plugin. I'm not sure whether it was a recent insiders update, daylight savings or some filesystem change but I have not changed my mkdocs.yml file lately. When I attempt to build now or serve, I get this error:
Bug description
The blog plugin appears to choke on the creation dates of some of the files on my filesystem. I'm able to workaround it by modifying line 134 in the blog plugin (
material/plugins/blog/plugin.py
) from:to (I also add
import pytz
at the top of the file)Related links
Reproduction
I was not able to repro this, I tried several things but could not isolate it. The following is the
markdown_extensions
andplugins
sections from the mkdocs.yml where it occurs.And here is the output of my docs/blog directory:
❯ stat -f "%Sc %z %N" *
May 31 15:38:53 2024 4778 2023-year-in-review.md
Oct 28 14:44:33 2024 8815 a-better-readme.md
May 31 15:44:25 2024 7776 bifurcating-lambda-logs.md
May 31 15:44:19 2024 6025 cloudwatch-insights-tricks.md
Aug 14 16:04:14 2024 15969 coldstart-zero-part-duex.md
May 31 15:44:06 2024 9717 coldstart-zero.md
May 31 15:44:01 2024 4142 done-before-the-bass-drops.md
Jun 27 21:06:41 2024 9605 dynamodb-secretmanager.md
Oct 17 12:58:46 2024 8387 edge-metrics.md
May 31 15:44:38 2024 6188 lambda-env-variable-coldstarts.md
May 31 15:44:43 2024 5648 lambda-request-timeline.md
May 31 15:44:52 2024 15597 logging-for-scale.md
Oct 13 17:09:17 2024 988 migrating-to-cdk.md
May 31 15:44:58 2024 2162 mission.md
May 31 15:45:03 2024 23022 optimizing-lambda-coldstarts.md
May 31 15:45:07 2024 8994 stripped-down-coldstarts.md
May 31 15:45:11 2024 5769 the-global-query.md
May 31 15:45:16 2024 4372 totally-async-eventbridge.md
May 31 15:45:21 2024 7874 why-im-spicy-about-coldstarts.md
Steps to reproduce
I tried creating a minimal blog repro both with and without the
git-revision-date-localized
plugin but was unable to get this issue to reoccur, it only occurs in my main site directory.I'm using
mkdocs-material 9.5.44+insiders.4.53.14
Browser
Firefox
Before submitting
The text was updated successfully, but these errors were encountered: