Skip to content

Commit 8591657

Browse files
DEP: prefer stdlib tomllib over tomli when available (#577)
1 parent 9e40b4a commit 8591657

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

grayskull/strategy/py_base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from urllib.parse import urlparse
1818

1919
import requests
20-
import tomli
2120
from colorama import Fore, Style
2221
from packaging.specifiers import SpecifierSet
2322
from packaging.utils import canonicalize_version
@@ -38,6 +37,11 @@
3837
sha256_checksum,
3938
)
4039

40+
if sys.version_info >= (3, 11):
41+
import tomllib
42+
else:
43+
import tomli as tomllib
44+
4145
log = logging.getLogger(__name__)
4246
RE_DEPS_NAME = re.compile(r"^\s*([\.a-zA-Z0-9_-]+)", re.MULTILINE)
4347
PIN_PKG_COMPILER = {"numpy": "<{ pin_compatible('numpy') }}"}
@@ -631,8 +635,8 @@ def get_entry_points_from_sdist(sdist_metadata: dict) -> list:
631635
all_parts[-1] = f"'{all_parts[-1]}'"
632636
all_lines.append("=".join(all_parts))
633637
try:
634-
all_entry_points = tomli.loads("\n".join(all_lines))
635-
except tomli.TOMLDecoderError:
638+
all_entry_points = tomllib.loads("\n".join(all_lines))
639+
except tomllib.TOMLDecoderError:
636640
return []
637641

638642
if all_entry_points.get("console_scripts") or all_entry_points.get("gui_scripts"):

grayskull/strategy/py_toml.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
import sys
12
from collections import defaultdict
23
from functools import singledispatch
34
from pathlib import Path
45
from typing import Tuple, Union
56

6-
import tomli
7-
87
from grayskull.strategy.parse_poetry_version import encode_poetry_version
98
from grayskull.utils import nested_dict
109

10+
if sys.version_info >= (3, 11):
11+
import tomllib
12+
else:
13+
import tomli as tomllib
14+
1115

1216
class InvalidPoetryDependency(BaseException):
1317
pass
@@ -207,7 +211,7 @@ def add_pep725_metadata(metadata: dict, toml_metadata: dict):
207211

208212
def get_all_toml_info(path_toml: Union[Path, str]) -> dict:
209213
with open(path_toml, "rb") as f:
210-
toml_metadata = tomli.load(f)
214+
toml_metadata = tomllib.load(f)
211215
toml_metadata = defaultdict(dict, toml_metadata)
212216
metadata = nested_dict()
213217
toml_project = toml_metadata.get("project", {}) or {}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies = [
2828
"setuptools >=30.3.0",
2929
"semver >=3.0.0,==3.0.*",
3030
"stdlib-list",
31-
"tomli",
31+
"tomli; python_version < '3.11'",
3232
"tomli-w",
3333
]
3434

0 commit comments

Comments
 (0)