Skip to content

Commit

Permalink
DEP: prefer stdlib tomllib over tomli when available (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros authored Nov 13, 2024
1 parent 9e40b4a commit 8591657
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 7 additions & 3 deletions grayskull/strategy/py_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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
Expand All @@ -38,6 +37,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') }}"}
Expand Down Expand Up @@ -631,8 +635,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"):
Expand Down
10 changes: 7 additions & 3 deletions grayskull/strategy/py_toml.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {}
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down

0 comments on commit 8591657

Please sign in to comment.