From e2a5497e7b0032b074d856923dc4cc2a4b238a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Tue, 12 Nov 2024 11:48:59 +0100 Subject: [PATCH] DEP: prefer stdlib tomllib over tomli when available --- grayskull/strategy/py_base.py | 10 +++++++--- grayskull/strategy/py_toml.py | 10 +++++++--- pyproject.toml | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/grayskull/strategy/py_base.py b/grayskull/strategy/py_base.py index dcd9f1cac..2a8744bf1 100644 --- a/grayskull/strategy/py_base.py +++ b/grayskull/strategy/py_base.py @@ -16,7 +16,6 @@ from urllib.parse import urlparse import requests -import tomli from colorama import Fore, Style from packaging.specifiers import SpecifierSet from packaging.utils import canonicalize_version @@ -37,6 +36,11 @@ sha256_checksum, ) +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib + log = logging.getLogger(__name__) RE_DEPS_NAME = re.compile(r"^\s*([\.a-zA-Z0-9_-]+)", re.MULTILINE) PIN_PKG_COMPILER = {"numpy": "<{ pin_compatible('numpy') }}"} @@ -630,8 +634,8 @@ def get_entry_points_from_sdist(sdist_metadata: dict) -> list: all_parts[-1] = f"'{all_parts[-1]}'" all_lines.append("=".join(all_parts)) try: - all_entry_points = tomli.loads("\n".join(all_lines)) - except tomli.TOMLDecoderError: + all_entry_points = tomllib.loads("\n".join(all_lines)) + except tomllib.TOMLDecoderError: return [] if all_entry_points.get("console_scripts") or all_entry_points.get("gui_scripts"): diff --git a/grayskull/strategy/py_toml.py b/grayskull/strategy/py_toml.py index 8681dd203..36fd6e006 100644 --- a/grayskull/strategy/py_toml.py +++ b/grayskull/strategy/py_toml.py @@ -1,13 +1,17 @@ +import sys from collections import defaultdict from functools import singledispatch from pathlib import Path from typing import Tuple, Union -import tomli - from grayskull.strategy.parse_poetry_version import encode_poetry_version from grayskull.utils import nested_dict +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib + class InvalidPoetryDependency(BaseException): pass @@ -207,7 +211,7 @@ def add_pep725_metadata(metadata: dict, toml_metadata: dict): def get_all_toml_info(path_toml: Union[Path, str]) -> dict: with open(path_toml, "rb") as f: - toml_metadata = tomli.load(f) + toml_metadata = tomllib.load(f) toml_metadata = defaultdict(dict, toml_metadata) metadata = nested_dict() toml_project = toml_metadata.get("project", {}) or {} diff --git a/pyproject.toml b/pyproject.toml index 3bd2ded7e..a60696274 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,7 @@ dependencies = [ "setuptools >=30.3.0", "semver >=3.0.0,==3.0.*", "stdlib-list", - "tomli", + "tomli; python_version < '3.11'", "tomli-w", ]