Skip to content

mkdocs build fails in HtmlFormatter with 'NoneType' object has no attribute 'replace' #3076

Description

@TTFrSim

We encountered a bug when building our HTML documentation via mkdocs build.
It seems that the README.md could not be parsed anymore.
The README.md was not changed.
The only change was upgrading to the new pygements version 2.20.0 as part of our automated regression tests.
After pining the version to 2.19.2 the build is not failing anymore.

ERROR   -  Error reading page 'README.md': 'NoneType' object has no attribute 'replace'
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Scripts\mkdocs.exe\__main__.py", line 7, in <module>
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\click\core.py", line 1485, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\click\core.py", line 1406, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\click\core.py", line 1873, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\click\core.py", line 1269, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\click\core.py", line 824, in invoke
    return callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\mkdocs\__main__.py", line 288, in build_command
    build.build(cfg, dirty=not clean)
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\mkdocs\commands\build.py", line 310, in build
    _populate_page(file.page, config, files, dirty)
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\mkdocs\commands\build.py", line 167, in _populate_page
    page.render(config, files)
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\mkdocs\structure\pages.py", line 285, in render
    self.content = md.convert(self.markdown)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\markdown\core.py", line 367, in convert
    newRoot = treeprocessor.run(root)
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\pymdownx\highlight.py", line 527, in run
    code.highlight(
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\pymdownx\highlight.py", line 400, in highlight
    formatter = html_formatter(
                ^^^^^^^^^^^^^^^
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\pymdownx\highlight.py", line 179, in __init__
    HtmlFormatter.__init__(self, **options)
  File "C:\Users\<User>\AppData\Local\pypoetry\Cache\virtualenvs\<lib-env>-py3.12\Lib\site-packages\pygments\formatters\html.py", line 434, in __init__
    self.filename = html.escape(self._decodeifneeded(options.get('filename', '')))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\<User>\AppData\Local\Programs\Python\Python312\Lib\html\__init__.py", line 19, in escape
    s = s.replace("&", "&amp;") # Must be done first!
        ^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'

Activity

  1. styx0x6 commented on Mar 30, 2026

    @styx0x6
    Contributor

    Hi,

    html.escape have been added for hardening purpose in class attributes when initiated. It uses replace built-in function inside itself to do its business. This function only take strings as argument.

    It seems that sometimes not all attributes may be initiated with a value, works like that up to 2.19.2. But since the hardening, built-in replace doesn't accept None object, only string.

    Maybe the best solution is to force-cast escaped values with str(), just to render the None object or any other as readable.

  2. styx0x6 commented on Mar 30, 2026

    @styx0x6
    Contributor

    My proposal to fix this issue: #3078

  3. added a commit that references this issue on Mar 30, 2026
  4. TTFrSim commented on Mar 31, 2026

    @TTFrSim
    Author

    Thanks for working on a fix... this fast 🚀

    When the artifact is ready, I also can test it on my side and give Feedback.

  5. added a commit that references this issue on Mar 31, 2026
  6. added this to the milestone on Mar 31, 2026
  7. woodruffw commented on Apr 1, 2026

    @woodruffw

    Just for searchability/cross-referencing: this appears to affect Zensical as well (which is similar to MkDocs).

  8. birkenfeld commented on Apr 1, 2026

    @birkenfeld
    Member

    Fix will be released shortly, however, for the record, this is a bug in (appears to be) pymdownx since AFAICS we never guaranteed that None would work as an option value. So please also report there.

  9. added a commit that references this issue on Apr 2, 2026
    7d7ab7f
  10. added a commit that references this issue on Apr 24, 2026
  11. added a commit that references this issue on Apr 30, 2026
  12. added a commit that references this issue on May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions