diff --git a/_docs/render.py b/_docs/render.py index c14bd4dc..349238a9 100644 --- a/_docs/render.py +++ b/_docs/render.py @@ -154,9 +154,9 @@ def example_contribution( if format == "yaml": return yaml.safe_dump(output, sort_keys=False) if format == "toml": - import pytomlpp as toml + import tomli_w - return toml.dumps(output) + return tomli_w.dumps(output) if format == "json": return json.dumps(output) raise ValueError("Invalid format: {format}. Must be 'yaml', 'toml' or 'json'.") diff --git a/pyproject.toml b/pyproject.toml index 05bc54ab..17e8d7c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,8 @@ dependencies = [ "build", "psygnal>=0.3.0", "pydantic<2", - "pytomlpp", + "tomli-w", + "tomli; python_version < '3.11'", "rich", "typer", ] diff --git a/src/npe2/_inspection/_setuputils.py b/src/npe2/_inspection/_setuputils.py index 678b8ec2..467f72a5 100644 --- a/src/npe2/_inspection/_setuputils.py +++ b/src/npe2/_inspection/_setuputils.py @@ -86,18 +86,6 @@ def get_package_dir_info(path: Union[Path, str]) -> PackageInfo: EntryPoint(name.strip(), value.strip(), group) ) - # # check for pyproject.toml - # pyproject_toml = path / "pyproject.toml" - # if pyproject_toml.exists(): - # info.pyproject_toml = pyproject_toml - # with open(pyproject_toml, "r") as f: - # data = pytomlpp.load(f) - # if project := data.get("project"): - # info.package_name = project.get("name", "") - # for group, ep in project.get('entry-points', {}).items(): - # for name, value in ep.items(): - # info.entry_points.append(EntryPoint(name, value, group)) - return info diff --git a/src/npe2/_setuptools_plugin.py b/src/npe2/_setuptools_plugin.py index 9093748a..99ff9174 100644 --- a/src/npe2/_setuptools_plugin.py +++ b/src/npe2/_setuptools_plugin.py @@ -36,9 +36,12 @@ def trace(*k: object) -> None: def _lazy_tomli_load(data: str) -> dict[str, Any]: - from pytomlpp import loads + try: + import tomllib + except ImportError: + import tomli as tomllib # type: ignore [no-redef] - return loads(data) + return tomllib.loads(data) def _read_dist_name_from_setup_cfg() -> str | None: diff --git a/src/npe2/manifest/_bases.py b/src/npe2/manifest/_bases.py index f7ddca06..630b75bf 100644 --- a/src/npe2/manifest/_bases.py +++ b/src/npe2/manifest/_bases.py @@ -3,7 +3,6 @@ from pathlib import Path from typing import Callable, Dict, Optional, Union -import pytomlpp as toml import yaml from pydantic import BaseModel, PrivateAttr @@ -30,10 +29,12 @@ def toml(self, pyproject=False, **kwargs) -> str: **kwargs passed to `BaseModel.json()` """ + import tomli_w + d = self._serialized_data(**kwargs) if pyproject: d = {"tool": {"napari": d}} - return toml.dumps(d) + return tomli_w.dumps(d) def yaml(self, **kwargs) -> str: """Generate serialized `yaml` string for this model. @@ -74,13 +75,18 @@ def from_file(cls, path: Union[Path, str]): if path.suffix.lower() == ".json": loader = json.load elif path.suffix.lower() == ".toml": - loader = toml.load + try: + import tomllib + except ImportError: + import tomli as tomllib # type: ignore [no-redef] + + loader = tomllib.load elif path.suffix.lower() in (".yaml", ".yml"): loader = yaml.safe_load else: raise ValueError(f"unrecognized file extension: {path}") # pragma: no cover - with open(path, encoding="utf-8") as f: + with open(path, mode="rb") as f: data = loader(f) or {} if path.name == "pyproject.toml":