Skip to content

Commit 9020754

Browse files
authored
Make tomli a dependency for <3.11 (#244)
1 parent 1d89f3c commit 9020754

File tree

4 files changed

+20
-60
lines changed

4 files changed

+20
-60
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies = [
1111
"packaging",
1212
"ruamel.yaml",
1313
"typing_extensions; python_version < '3.8'",
14+
"tomli; python_version < '3.11'",
1415
]
1516
requires-python = ">=3.7"
1617

unidep/_dependencies_parsing.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,10 @@
3636
from typing_extensions import Literal
3737

3838

39-
try: # pragma: no cover
40-
if sys.version_info >= (3, 11):
41-
import tomllib
42-
else:
43-
import tomli as tomllib
44-
HAS_TOML = True
45-
except ImportError: # pragma: no cover
46-
HAS_TOML = False
39+
if sys.version_info >= (3, 11):
40+
import tomllib
41+
else: # pragma: no cover
42+
import tomli as tomllib
4743

4844

4945
def find_requirements_files(
@@ -166,15 +162,6 @@ def _parse_overwrite_pins(overwrite_pins: list[str]) -> dict[str, str | None]:
166162
@functools.lru_cache
167163
def _load(p: Path, yaml: YAML) -> dict[str, Any]:
168164
if p.suffix == ".toml":
169-
if not HAS_TOML: # pragma: no cover
170-
msg = (
171-
"❌ No toml support found in your Python installation."
172-
" If you are using unidep from `pyproject.toml` and this"
173-
" error occurs during installation, make sure you add"
174-
'\n\n[build-system]\nrequires = [..., "unidep[toml]"]\n\n'
175-
" Otherwise, please install it with `pip install tomli`."
176-
)
177-
raise ImportError(msg)
178165
with p.open("rb") as f:
179166
pyproject = tomllib.load(f)
180167
project_dependencies = pyproject.get("project", {}).get("dependencies", [])

unidep/_setuptools_integration.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@
2424
warn,
2525
)
2626

27-
try: # pragma: no cover
28-
if sys.version_info >= (3, 11):
29-
import tomllib
30-
else:
31-
import tomli as tomllib
32-
HAS_TOML = True
33-
except ImportError: # pragma: no cover
34-
HAS_TOML = False
27+
if sys.version_info >= (3, 11):
28+
import tomllib
29+
else: # pragma: no cover
30+
import tomli as tomllib
3531

3632

3733
if TYPE_CHECKING:
@@ -195,9 +191,6 @@ def visit_Call(self, node: ast.Call) -> None: # noqa: N802
195191

196192

197193
def _package_name_from_pyproject_toml(file_path: Path) -> str:
198-
if not HAS_TOML: # pragma: no cover
199-
msg = "toml is required to parse pyproject.toml files."
200-
raise ImportError(msg)
201194
with file_path.open("rb") as f:
202195
data = tomllib.load(f)
203196
with contextlib.suppress(KeyError):

unidep/utils.py

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@
2323
validate_selector,
2424
)
2525

26-
try: # pragma: no cover
27-
if sys.version_info >= (3, 11):
28-
import tomllib
29-
else:
30-
import tomli as tomllib
31-
HAS_TOML = True
32-
except ImportError: # pragma: no cover
33-
HAS_TOML = False
26+
if sys.version_info >= (3, 11):
27+
import tomllib
28+
else: # pragma: no cover
29+
import tomli as tomllib
3430

3531

3632
def add_comment_to_file(
@@ -88,15 +84,9 @@ def is_pip_installable(folder: str | Path) -> bool: # pragma: no cover
8884

8985
pyproject_path = path / "pyproject.toml"
9086
if pyproject_path.exists():
91-
if HAS_TOML:
92-
with pyproject_path.open("rb") as file:
93-
pyproject_data = tomllib.load(file)
94-
return "build-system" in pyproject_data
95-
else:
96-
with pyproject_path.open("r") as file:
97-
for line in file:
98-
if line.strip().startswith("[build-system]"):
99-
return True
87+
with pyproject_path.open("rb") as file:
88+
pyproject_data = tomllib.load(file)
89+
return "build-system" in pyproject_data
10090
return False
10191

10292

@@ -245,21 +235,10 @@ def extract_matching_platforms(comment: str) -> list[Platform]:
245235

246236

247237
def unidep_configured_in_toml(path: Path) -> bool:
248-
"""Check if dependencies are specified in pyproject.toml.
249-
250-
If a TOML parser is not available it finds `[tool.unidep]` in `pyproject.toml`.
251-
"""
252-
if HAS_TOML:
253-
with path.open("rb") as f:
254-
data = tomllib.load(f)
255-
return bool(data.get("tool", {}).get("unidep", {}))
256-
# TODO[Bas]: will fail if defining dict in # noqa: TD004, TD003, FIX002
257-
# pyproject.toml directly e.g., it contains:
258-
# `tool = {unidep = {dependencies = ...}}`
259-
return any( # pragma: no cover
260-
line.lstrip().startswith("[tool.unidep")
261-
for line in path.read_text().splitlines()
262-
)
238+
"""Check if dependencies are specified in pyproject.toml."""
239+
with path.open("rb") as f:
240+
data = tomllib.load(f)
241+
return bool(data.get("tool", {}).get("unidep", {}))
263242

264243

265244
def split_path_and_extras(input_str: str | Path) -> tuple[Path, list[str]]:

0 commit comments

Comments
 (0)