mwouts / jupytext

Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
https://jupytext.readthedocs.io
MIT License
6.65k stars 386 forks source link

Opening markdown notebook with empty frontmatter in Jupyter Lab raises exception #1070

Closed tillahoffmann closed 1 year ago

tillahoffmann commented 1 year ago

Attempting to open a markdown file with empty frontmatter in Jupyter Lab raises an AttributeError. E.g., consider the simple document.

---

---

This file has empty frontmatter.

Loading the yaml frontmatter returns None, and it is not possible to open the notebook. Adding a dummy variable resolves the issue because the frontmatter is not None. If there is no frontmatter block, the Python representation presumably defaults to an empty dict which avoids the error.

[W <datetime> ServerApp] wrote error: "'NoneType' object has no attribute 'get'"
Traceback (most recent call last):
    File "/.../jupytext/contentsmanager.py", line 217, in get
    model["content"] = reads(
    File "/.../jupytext/jupytext.py", line 367, in reads
    format_name = read_format_from_metadata(text, ext) or fmt.get("format_name")
    File "/.../jupytext/formats.py", line 297, in read_format_from_metadata
    metadata = read_metadata(text, ext)
    File "/.../jupytext/formats.py", line 284, in read_metadata
    header.get("jupytext", {})
AttributeError: 'NoneType' object has no attribute 'get'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
    File "/.../tornado/web.py", line 1786, in _execute
    result = await result
    File "/.../jupyter_server/services/contents/handlers.py", line 122, in get
    self.contents_manager.get(
    File "/.../jupytext/contentsmanager.py", line 230, in get
    raise HTTPError(500, str(err))
tornado.web.HTTPError: HTTP 500: Internal Server Error ('NoneType' object has no attribute 'get')

This is on macOS 13.3.1 with Python 3.10 and the following dependencies.

jupyter-events==0.6.3
jupyter-lsp==2.1.0
jupyter_client==8.2.0
jupyter_core==5.3.0
jupyter_server==2.5.0
jupyter_server_terminals==0.4.4
jupyterlab==4.0.0
jupyterlab-pygments==0.2.2
jupyterlab-widgets==3.0.7
jupyterlab_server==2.22.1
jupytext==1.14.5
mwouts commented 1 year ago

Thanks @tillahoffmann for reporting this. It should be easy to add a test and a fix. I'll have a look soon.