squidfunk / mkdocs-material

Documentation that simply works
https://squidfunk.github.io/mkdocs-material/
MIT License
20.22k stars 3.47k forks source link

Section nav error: TypeError: unsupported operand type(s) for +: 'int' and 'str' #2030

Closed JulioV closed 3 years ago

JulioV commented 3 years ago

I've found a bug and checked that ...

Description

I get the following error when activating section based navigation with mkdocs-material-6.1.4+insiders-1.9.0 in a minimal website.

ERROR   -  Exception in callback <bound method LiveReloadHandler.poll_tasks of <class 'livereload.handlers.LiveReloadHandler'>>
Traceback (most recent call last):
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/tornado/ioloop.py", line 907, in _run
    return self.callback()
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/livereload/handlers.py", line 69, in poll_tasks
    filepath, delay = cls.watcher.examine()
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/livereload/watcher.py", line 119, in examine
    func()
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/mkdocs/commands/serve.py", line 136, in builder
    build(config, live_server=live_server, dirty=dirty)
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/mkdocs/commands/build.py", line 285, in build
    _build_theme_template(template, env, files, config, nav)
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/mkdocs/commands/build.py", line 108, in _build_theme_template
    output = _build_template(template_name, template, files, config, nav)
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/mkdocs/commands/build.py", line 87, in _build_template
    output = template.render(context)
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/material/404.html", line 4, in top-level template code
    {% extends "main.html" %}
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/jinja2/environment.py", line 1005, in render
    return concat(self.root_render_func(self.new_context(vars)))
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/material/404.html", line 14, in root
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/material/main.html", line 14, in root
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/material/base.html", line 126, in root
    <main class="md-main" data-md-component="main">
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/material/base.html", line 360, in block_site_nav
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/material/partials/nav.html", line 59, in root
  File "/usr/local/Caskroom/miniconda/base/envs/rapids202008/lib/python3.7/site-packages/material/partials/nav-item.html", line 38, in root
    </nav>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Expected behavior

Load the website without errors and with section navigation

Actual behavior

The website is not rendered

Steps to reproduce the bug

  1. Install mkdocs-material-6.1.4+insiders-1.9.0
  2. Setup the mkdocs.yml described below
  3. Create a test file under /docs/test.md with this line # My Test
  4. mkdocs serve

Package versions

Project configuration

site_name: Test
theme:
  name: material
  features:
    - navigation.sections
nav:
  - Others:
    - Team: test.md

System information

squidfunk commented 3 years ago

Thanks for reporting! I'll look into it straight away.

squidfunk commented 3 years ago

Hmm, weird, works for me exactly as it should (see attached screenshot), Have you cloned Insiders from the master or downloaded the *.tar.gz package? Also, the line numbers (i.e. in nav-item.html) are somehow off for all files I checked, e.g. material/base.html only has 225 lines, so there's no line 360. If you didn't install from master, could you try that?

You can also message me on Gitter.


screenshot-127-0-0-1-8000-test-1604829518465

squidfunk commented 3 years ago

Okay, so I looked at the templates again and maybe have a fix for what you're seeing, albeit I cannot test it, as I cannot reproduce it. The older parts of the templates used + for string concatenation in Jinja templates for a long time, instead of ~ which should be used. The latter coerces all expressions to strings before concatenating, so as the error you're seeing says that there's a type mismatch for the + operator, I replaced all string concatenations with ~ in 86a4c47c0aee5bb42b3d7435c05d24936c32e397 which I also just merged into the master of Insiders.

Could you reinstall Insiders and try again?

pip install --force-reinstall git+https://${GH_TOKEN}@github.com/squidfunk/mkdocs-material-insiders.git
JulioV commented 3 years ago

I originally downloaded the repo as a zip file, not sure why this mattered. However, it's working now after the last commits to master, thanks!

squidfunk commented 3 years ago

Perfect! I’ll investigate the zip file issue, but great that it works for you now!

DougTrajano commented 3 years ago

Sorry to comment on an old issue, but I think that I identified the reason for that.

It seems that it is related to the Jinja2 version. mkdocs requires Jinja2>=2.10.3 on its requirements.txt, but when I defined Jinja2==2.11.3 in my project, it solved the issue.

squidfunk commented 3 years ago

Thanks! It might be worth raising this issue over at MkDocs, so they bump the Jinja version in their requirements, as Jinja is a transitive dependency for us. This would automatically install a new version when MkDocs is upgraded.

liuhualh commented 3 years ago

I have a similar problem on Ubuntu 20.04

The related software versions are:

mkdocs                                    1.2.1               
mkdocs-enumerate-headings-plugin          0.4.5               
mkdocs-git-authors-plugin                 0.4.1               
mkdocs-git-committers-plugin              0.2.2               
mkdocs-git-revision-date-localized-plugin 0.9.2               
mkdocs-git-revision-date-plugin           0.3.1               
mkdocs-material                           7.1.9               
mkdocs-material-extensions                1.0.1               
mkdocs-minify-plugin                      0.4.0               
mkdocs-plugin-progress                    1.2.0               
mkdocs-redirects                          1.0.3 
Jinja2                                    2.10.1

Distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:    20.04
Codename:   focal

When mkdocs build , print the following error information

INFO    -  Processing page "101_memo/site_mind.md"...
INFO    -  Contextualized template 404.html.
Traceback (most recent call last):
  File "/usr/local/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
  File "/usr/lib/python3/dist-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3/dist-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3/dist-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/mkdocs/__main__.py", line 183, in build_command
    build.build(config.load_config(**kwargs), dirty=not clean)
  File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/build.py", line 298, in build
    _build_theme_template(template, env, files, config, nav)
  File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/build.py", line 111, in _build_theme_template
    output = _build_template(template_name, template, files, config, nav)
  File "/usr/local/lib/python3.8/dist-packages/mkdocs/commands/build.py", line 90, in _build_template
    output = template.render(context)
  File "/usr/lib/python3/dist-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3/dist-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.8/dist-packages/material/404.html", line 4, in <module>
    {% extends "main.html" %}
  File "/usr/local/lib/python3.8/dist-packages/material/main.html", line 4, in <module>
    {% extends "base.html" %}
  File "/usr/local/lib/python3.8/dist-packages/material/base.html", line 128, in <module>
    {% block site_nav %}
  File "/usr/local/lib/python3.8/dist-packages/material/base.html", line 136, in <module>
    {% include "partials/nav.html" %}
  File "/usr/local/lib/python3.8/dist-packages/material/partials/nav.html", line 27, in <module>
    {% include "partials/nav-item.html" %}
  File "/usr/local/lib/python3.8/dist-packages/material/partials/nav-item.html", line 67, in <module>
    {{ render(nav_item, path, level) }}
  File "/usr/lib/python3/dist-packages/jinja2/runtime.py", line 583, in _invoke
    rv = self._func(*arguments)
  File "/usr/local/lib/python3.8/dist-packages/material/partials/nav-item.html", line 10, in <module>
    {% if "navigation.sections" in features and level == 1 + (
TypeError: unsupported operand type(s) for +: 'int' and 'str'

After I upgrade Jinja to latest (3.0.1)

Jinja2                                    3.0.1

and then everything is ok, thanks very much!

binwiederhier commented 2 years ago

For the Googlers, I ran into the same issue here: https://github.com/binwiederhier/ntfy/actions/runs/1550547757 and fixed it with an explicit jinja2 dependency, here: https://github.com/binwiederhier/ntfy/commit/ab3cc47e272aa0d21f8b3fd134265611916b6416#diff-4d7c51b1efe9043e44439a949dfd92e5827321b34082903477fd04876edb7552R4

Thanks @liuhualh for the hint and thanks @squidfunk for a freaking awesome piece of software (which has docs here: https://ntfy.sh/docs).

squidfunk commented 2 years ago

We added Jinja as an explicit dependency in https://github.com/squidfunk/mkdocs-material/commit/905c8b77c689e459b37b64564d81775d5ca60879, released as part of 7.3.3, see here:

https://github.com/squidfunk/mkdocs-material/blob/1316d8d184b63ddb9e308956b2599c7320709bf7/requirements.txt#L22