#3 Don't use tomli, use tomllib from the Python 3.11+ standard library
Merged 9 months ago by lbalhar. Opened 9 months ago by churchyard.
rpms/ churchyard/python-hatch-jupyter-builder tomllib  into  rawhide

file added
+204
@@ -0,0 +1,204 @@ 

+ From 989608006455bc1300c6083cf6cbd3324ffbc218 Mon Sep 17 00:00:00 2001

+ From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= <miro@hroncok.cz>

+ Date: Mon, 6 Mar 2023 10:33:40 +0100

+ Subject: [PATCH 1/2] Use tomllib from the Python 3.11+ standard library

+ 

+ ---

+  hatch_jupyter_builder/migrate/_migrate.py          | 10 +++++++---

+  hatch_jupyter_builder/migrate/cli.py               |  2 +-

+  hatch_jupyter_builder/migrate/jupyter_packaging.py |  8 ++++++--

+  pyproject.toml                                     |  2 +-

+  tests/test_migration.py                            | 14 +++++++++-----

+  5 files changed, 24 insertions(+), 12 deletions(-)

+ 

+ diff --git a/hatch_jupyter_builder/migrate/_migrate.py b/hatch_jupyter_builder/migrate/_migrate.py

+ index 00d450e..87b709a 100644

+ --- a/hatch_jupyter_builder/migrate/_migrate.py

+ +++ b/hatch_jupyter_builder/migrate/_migrate.py

+ @@ -6,10 +6,14 @@

+  import sys

+  from pathlib import Path

+  

+ -import tomli

+  import tomli_w

+  from packaging import version

+  

+ +try:

+ +    import tomllib

+ +except ImportError:

+ +    import tomli as tomllib

+ +

+  logger = logging.getLogger(__name__)

+  logging.basicConfig()

+  

+ @@ -31,7 +35,7 @@

+  # Read pyproject before migration to get old build requirements.

+  pyproject = Path("pyproject.toml")

+  if pyproject.exists():

+ -    data = tomli.loads(pyproject.read_text("utf-8"))

+ +    data = tomllib.loads(pyproject.read_text("utf-8"))

+      requires = data["build-system"]["requires"]

+      # Install the old build reqs into this venv.

+      subprocess.run([sys.executable, "-m", "pip", "install", *requires])

+ @@ -104,7 +108,7 @@

+  # Migrate and remove unused config.

+  # Read in the project.toml after auto migration.

+  logger.info("Migrating static data")

+ -data = tomli.loads(pyproject.read_text("utf-8"))

+ +data = tomllib.loads(pyproject.read_text("utf-8"))

+  tool_table = data.setdefault("tool", {})

+  

+  # Handle license file.

+ diff --git a/hatch_jupyter_builder/migrate/cli.py b/hatch_jupyter_builder/migrate/cli.py

+ index f9c1b64..34ab802 100644

+ --- a/hatch_jupyter_builder/migrate/cli.py

+ +++ b/hatch_jupyter_builder/migrate/cli.py

+ @@ -27,7 +27,7 @@ def main(td, target_dir):

+      runner([python, "-m", "pip", "install", "build"])

+      runner([python, "-m", "pip", "install", "packaging"])

+      runner([python, "-m", "pip", "install", "tomli_w"])

+ -    runner([python, "-m", "pip", "install", "tomli"])

+ +    runner([python, "-m", "pip", "install", "tomli;python_version<'3.11'"])

+      runner([python, "-m", "pip", "install", "hatch"])

+      runner([python, "-m", "build", target_dir, "--sdist"])

+  

+ diff --git a/hatch_jupyter_builder/migrate/jupyter_packaging.py b/hatch_jupyter_builder/migrate/jupyter_packaging.py

+ index f03931a..d286d34 100644

+ --- a/hatch_jupyter_builder/migrate/jupyter_packaging.py

+ +++ b/hatch_jupyter_builder/migrate/jupyter_packaging.py

+ @@ -3,9 +3,13 @@

+  import sys

+  from pathlib import Path

+  

+ -import tomli

+  import tomli_w

+  

+ +try:

+ +    import tomllib

+ +except ImportError:

+ +    import tomli as tomllib

+ +

+  __this_shim = sys.modules.pop("jupyter_packaging")

+  __current_directory = sys.path.pop(0)

+  

+ @@ -17,7 +21,7 @@

+  

+  def _write_config(path, data):

+      pyproject = Path("pyproject.toml")

+ -    top = tomli.loads(pyproject.read_text(encoding="utf-8"))

+ +    top = tomllib.loads(pyproject.read_text(encoding="utf-8"))

+      current = top

+      parts = path.split(".")

+      for part in parts[:-1]:

+ diff --git a/pyproject.toml b/pyproject.toml

+ index d342540..6b810ce 100644

+ --- a/pyproject.toml

+ +++ b/pyproject.toml

+ @@ -31,7 +31,7 @@ Issues = "https://github.com/jupyterlab/hatch-jupyter-builder/issues"

+  Source = "https://github.com/jupyterlab/hatch-jupyter-builder"

+  

+  [project.optional-dependencies]

+ -test = ["pytest", "pytest-mock", "hatch", "pytest-cov", "tomli", "twine"]

+ +test = ["pytest", "pytest-mock", "hatch", "pytest-cov", "tomli;python_version<'3.11'", "twine"]

+  docs = ["pydata-sphinx-theme", "myst-parser", "hatch_jupyter_builder", "sphinxcontrib-spelling", "sphinx-autodoc-typehints"]

+  

+  [project.scripts]

+ diff --git a/tests/test_migration.py b/tests/test_migration.py

+ index 915dacc..4f79c5f 100644

+ --- a/tests/test_migration.py

+ +++ b/tests/test_migration.py

+ @@ -7,7 +7,11 @@

+  from pathlib import Path

+  

+  import pytest

+ -import tomli

+ +

+ +try:

+ +    import tomllib

+ +except ImportError:

+ +    import tomli as tomllib

+  

+  from hatch_jupyter_builder.compare_migrated.cli import main

+  

+ @@ -32,8 +36,8 @@ def test_npm_builder_migration():

+          subprocess.check_call([python, "-m", "hatch_jupyter_builder.migrate", target1])

+          source_toml = source.joinpath("pyproject.toml").read_text(encoding="utf-8")

+          target_toml = target1.joinpath("pyproject.toml").read_text(encoding="utf-8")

+ -        source_data = tomli.loads(source_toml)

+ -        target_data = tomli.loads(target_toml)

+ +        source_data = tomllib.loads(source_toml)

+ +        target_data = tomllib.loads(target_toml)

+  

+          # The hatchling and hatch_jupyter_builder versions might differ.

+          source_data["build-system"]["requires"] = target_data["build-system"]["requires"]

+ @@ -84,8 +88,8 @@ def test_create_cmdclass_migration():

+          subprocess.check_call([python, "-m", "hatch_jupyter_builder.migrate", target1])

+          source_toml = source.joinpath("pyproject.toml").read_text(encoding="utf-8")

+          target_toml = target1.joinpath("pyproject.toml").read_text(encoding="utf-8")

+ -        source_data = tomli.loads(source_toml)

+ -        target_data = tomli.loads(target_toml)

+ +        source_data = tomllib.loads(source_toml)

+ +        target_data = tomllib.loads(target_toml)

+  

+          # The hatchling and hatch_jupyter_builder versions might differ.

+          source_data["build-system"]["requires"] = target_data["build-system"]["requires"]

+ 

+ From 4eac392f91a4b501b2b410bdd05bd345f21e1039 Mon Sep 17 00:00:00 2001

+ From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= <miro@hroncok.cz>

+ Date: Thu, 9 Mar 2023 12:41:34 +0100

+ Subject: [PATCH 2/2] Please the type checker

+ 

+ ---

+  hatch_jupyter_builder/migrate/_migrate.py          | 4 ++--

+  hatch_jupyter_builder/migrate/jupyter_packaging.py | 4 ++--

+  tests/test_migration.py                            | 4 ++--

+  3 files changed, 6 insertions(+), 6 deletions(-)

+ 

+ diff --git a/hatch_jupyter_builder/migrate/_migrate.py b/hatch_jupyter_builder/migrate/_migrate.py

+ index 87b709a..c2ff87c 100644

+ --- a/hatch_jupyter_builder/migrate/_migrate.py

+ +++ b/hatch_jupyter_builder/migrate/_migrate.py

+ @@ -9,9 +9,9 @@

+  import tomli_w

+  from packaging import version

+  

+ -try:

+ +if sys.version_info >= (3, 11):

+      import tomllib

+ -except ImportError:

+ +else:

+      import tomli as tomllib

+  

+  logger = logging.getLogger(__name__)

+ diff --git a/hatch_jupyter_builder/migrate/jupyter_packaging.py b/hatch_jupyter_builder/migrate/jupyter_packaging.py

+ index d286d34..d9897b1 100644

+ --- a/hatch_jupyter_builder/migrate/jupyter_packaging.py

+ +++ b/hatch_jupyter_builder/migrate/jupyter_packaging.py

+ @@ -5,9 +5,9 @@

+  

+  import tomli_w

+  

+ -try:

+ +if sys.version_info >= (3, 11):

+      import tomllib

+ -except ImportError:

+ +else:

+      import tomli as tomllib

+  

+  __this_shim = sys.modules.pop("jupyter_packaging")

+ diff --git a/tests/test_migration.py b/tests/test_migration.py

+ index 4f79c5f..8095a36 100644

+ --- a/tests/test_migration.py

+ +++ b/tests/test_migration.py

+ @@ -8,9 +8,9 @@

+  

+  import pytest

+  

+ -try:

+ +if sys.version_info >= (3, 11):

+      import tomllib

+ -except ImportError:

+ +else:

+      import tomli as tomllib

+  

+  from hatch_jupyter_builder.compare_migrated.cli import main

@@ -1,17 +1,20 @@ 

  Name:           python-hatch-jupyter-builder

  Version:        0.8.3

- Release:        3%{?dist}

+ Release:        4%{?dist}

  Summary:        A hatch plugin to help build Jupyter packages

  License:        BSD-3-Clause

  URL:            https://pypi.org/project/hatch-jupyter-builder/

  Source:         %{pypi_source hatch_jupyter_builder}

  

+ # Use tomllib from the Python 3.11+ standard library

+ Patch:          https://github.com/jupyterlab/hatch-jupyter-builder/pull/108.patch

+ 

  BuildArch:      noarch

  BuildRequires:  python3-devel

  # Test deps, upstream contains pre-commit, pytest-cov etc.

  BuildRequires:  python3-pytest

  BuildRequires:  python3-pytest-mock

- BuildRequires:  python3-tomli

+ BuildRequires:  (python3-tomli if python3 < 3.11)

  

  %global _description %{expand:

  This provides a build hook plugin for Hatch that adds
@@ -54,6 +57,10 @@ 

  

  

  %changelog

+ * Wed Sep 13 2023 Miro Hrončok <mhroncok@redhat.com> - 0.8.3-4

+ - Avoid an unneeded dependency on python3-tomli

+ - Use tomllib from the Python 3.11+ standard library instead

+ 

  * Fri Jul 21 2023 Fedora Release Engineering <releng@fedoraproject.org> - 0.8.3-3

  - Rebuilt for https://fedoraproject.org/wiki/Fedora_39_Mass_Rebuild

  

This package is currently pulling tomli to ELN. I don't believe hatch-jupyter-builder belongs to RHEL 10 either -- the dependency on the Jupyter stack needs to be solved elsewhere, but my upstream PR exists, so why not backport it? It makes the dependency chain a tiny bit saner.

cc @yselkowitz who might want to know about this

Build succeeded.
https://fedora.softwarefactory-project.io/zuul/buildset/2c5b69951f0e4cdda6232470307488c1

Pull-Request has been merged by lbalhar

9 months ago