Skip to content

Commit

Permalink
Use tomllib from the Python 3.11+ standard library (#108)
Browse files Browse the repository at this point in the history
* Use tomllib from the Python 3.11+ standard library

* Please the type checker
  • Loading branch information
hroncok authored Mar 12, 2024
1 parent 1eb7774 commit 209154e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
10 changes: 7 additions & 3 deletions hatch_jupyter_builder/migrate/_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import sys
from pathlib import Path

import tomli
import tomli_w # type:ignore[import-not-found]
from packaging import version

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib

logger = logging.getLogger(__name__)
logging.basicConfig()

Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion hatch_jupyter_builder/migrate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down
8 changes: 6 additions & 2 deletions hatch_jupyter_builder/migrate/jupyter_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
from pathlib import Path
from typing import Any

import tomli
import tomli_w # type:ignore[import-not-found]

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib

__this_shim = sys.modules.pop("jupyter_packaging")
__current_directory = sys.path.pop(0)

Expand All @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test = [
"pytest",
"pytest-cov",
"pytest-mock",
"tomli",
"tomli;python_version<'3.11'",
"twine",
]
[project.urls]
Expand Down
14 changes: 9 additions & 5 deletions tests/test_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from pathlib import Path

import pytest
import tomli

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib

from hatch_jupyter_builder.compare_migrated.cli import main

Expand All @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit 209154e

Please sign in to comment.