Blob Blame History Raw
From c331d2c3bee122c14d7bb62737d7e5108d4d2ac1 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 fbcfedd..a0c5535 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  # type:ignore[import-not-found]
 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], check=False)
@@ -105,7 +109,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 a7b5253..81ce524 100644
--- a/hatch_jupyter_builder/migrate/cli.py
+++ b/hatch_jupyter_builder/migrate/cli.py
@@ -27,7 +27,7 @@ def main(td: str, target_dir: str) -> None:
     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 b0c8ab6..ed3a037 100644
--- a/hatch_jupyter_builder/migrate/jupyter_packaging.py
+++ b/hatch_jupyter_builder/migrate/jupyter_packaging.py
@@ -6,9 +6,13 @@
 from pathlib import Path
 from typing import Any
 
-import tomli
 import tomli_w  # type:ignore[import-not-found]
 
+try:
+    import tomllib
+except ImportError:
+    import tomli as tomllib
+
 __this_shim = sys.modules.pop("jupyter_packaging")
 __current_directory = sys.path.pop(0)
 
@@ -20,7 +24,7 @@
 
 def _write_config(path: Any, data: Any) -> None:
     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 2adf22d..6a0550c 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -45,7 +45,7 @@ test = [
   "pytest",
   "pytest-cov",
   "pytest-mock",
-  "tomli",
+  "tomli;python_version<'3.11'",
   "twine",
 ]
 [project.urls]
diff --git a/tests/test_migration.py b/tests/test_migration.py
index f373b13..ed7822c 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"]
@@ -87,8 +91,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 35749190389d045f74d17d17efdcdbf7f60ca733 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 a0c5535..383dca8 100644
--- a/hatch_jupyter_builder/migrate/_migrate.py
+++ b/hatch_jupyter_builder/migrate/_migrate.py
@@ -9,9 +9,9 @@
 import tomli_w  # type:ignore[import-not-found]
 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 ed3a037..9f4bd85 100644
--- a/hatch_jupyter_builder/migrate/jupyter_packaging.py
+++ b/hatch_jupyter_builder/migrate/jupyter_packaging.py
@@ -8,9 +8,9 @@
 
 import tomli_w  # type:ignore[import-not-found]
 
-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 ed7822c..9c8b0b0 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