From 681378e36dba4314e6c41d4ef47a6b12fc99d1e7 Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Sun, 7 Jan 2024 19:42:20 +0000 Subject: [PATCH] Use the standard lib tomllib or default to tomli Following JupyterLab --- CHANGELOG.md | 1 + pyproject.toml | 2 +- src/jupytext/config.py | 14 ++++++++------ src/jupytext/contentsmanager.py | 12 ++++++++---- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 993e4a189..bacae6b2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/pyproject.toml b/pyproject.toml index 6d61197d4..e6bd196d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,7 @@ dependencies = [ "markdown-it-py>=1.0", "packaging", "pyyaml", - "toml", + "tomli;python_version<\"3.11\"", ] dynamic = ["version"] diff --git a/src/jupytext/config.py b/src/jupytext/config.py index dd489900e..81bdfe38f 100644 --- a/src/jupytext/config.py +++ b/src/jupytext/config.py @@ -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 @@ -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 @@ -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: diff --git a/src/jupytext/contentsmanager.py b/src/jupytext/contentsmanager.py index 565e59ce6..80ee810bb 100644 --- a/src/jupytext/contentsmanager.py +++ b/src/jupytext/contentsmanager.py @@ -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 @@ -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: