Skip to content

Commit

Permalink
Use the standard lib tomllib or default to tomli
Browse files Browse the repository at this point in the history
Following JupyterLab
  • Loading branch information
mwouts committed Apr 27, 2024
1 parent a9cedc6 commit 681378e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Jupytext ChangeLog

**Changed**
- Temporary text notebooks for the `--pipe` or `--check` commands are now created in the notebook directory ([#1206](https://github.com/mwouts/jupytext/issues/1206))
- Jupytext uses the standard library `tomllib` in Python 3.11, or `tomli` in Python 3.10 or older, to match JupyterLab's dependencies ([#1195](https://github.com/mwouts/jupytext/issues/1195))

**Fixed**
- Jupytext is now tested with `pandoc>=3.0`. Please note that switching to `pandoc>=3.0` will add cell ids to your `pandoc:md` notebooks ([#1006](https://github.com/mwouts/jupytext/issues/1006))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies = [
"markdown-it-py>=1.0",
"packaging",
"pyyaml",
"toml",
"tomli;python_version<\"3.11\"",
]
dynamic = ["version"]

Expand Down
14 changes: 8 additions & 6 deletions src/jupytext/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"""Find and read Jupytext configuration files"""
import json
import os

try:
import tomllib
except ImportError:
import tomli as tomllib

import warnings

import yaml
Expand Down Expand Up @@ -334,10 +340,8 @@ def find_jupytext_configuration_file(path, search_parent_dirs=True):

pyproject_path = os.path.join(path, PYPROJECT_FILE)
if os.path.isfile(pyproject_path):
import toml

with open(pyproject_path) as stream:
doc = toml.loads(stream.read())
doc = tomllib.loads(stream.read())
if doc.get("tool", {}).get("jupytext") is not None:
return pyproject_path

Expand Down Expand Up @@ -366,9 +370,7 @@ def parse_jupytext_configuration_file(jupytext_config_file, stream=None):

try:
if jupytext_config_file.endswith((".toml", "jupytext")):
import toml

doc = toml.loads(stream)
doc = tomllib.loads(stream)
if jupytext_config_file.endswith(PYPROJECT_FILE):
return doc["tool"]["jupytext"]
else:
Expand Down
12 changes: 8 additions & 4 deletions src/jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"""
import itertools
import os

try:
import tomllib
except ImportError:
import tomli as tomllib

from collections import namedtuple
from datetime import timedelta

Expand Down Expand Up @@ -512,12 +518,10 @@ def get_config_file(self, directory):

pyproject_path = directory + "/" + PYPROJECT_FILE
if self.file_exists(pyproject_path):
import toml

model = self.get(pyproject_path, type="file")
try:
doc = toml.loads(model["content"])
except toml.decoder.TomlDecodeError as e:
doc = tomllib.loads(model["content"])
except tomllib.TOMLDecodeError as e:
self.log.warning(f"Cannot load {pyproject_path}: {e}")
else:
if doc.get("tool", {}).get("jupytext") is not None:
Expand Down

0 comments on commit 681378e

Please sign in to comment.