Skip to content

Commit 1e3c39d

Browse files
Merge pull request #1357 from tiran/fix/pyproject-tomllib-read
fix(deps): read pyproject.toml with tomllib instead of tomlkit
2 parents 3d0038c + a2c053d commit 1e3c39d

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎src/fromager/dependencies.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import os
66
import pathlib
77
import tempfile
8+
import tomllib
89
import typing
910
import zipfile
1011

1112
import pyproject_hooks
12-
import tomlkit
1313
from packaging.metadata import Metadata
1414
from packaging.requirements import Requirement
1515
from packaging.utils import NormalizedName, canonicalize_name
@@ -492,7 +492,9 @@ def get_pyproject_contents(sdist_root_dir: pathlib.Path) -> dict[str, typing.Any
492492
pyproject_toml_filename = sdist_root_dir / "pyproject.toml"
493493
if not os.path.exists(pyproject_toml_filename):
494494
return {}
495-
return tomlkit.loads(pyproject_toml_filename.read_text())
495+
# Read-only parse: use stdlib tomllib, not tomlkit. tomlkit rejects some
496+
# spec-valid files (out-of-order tables), e.g. #1356.
497+
return tomllib.loads(pyproject_toml_filename.read_text())
496498

497499

498500
# From pypa/build/src/build/__main__.py

‎tests/test_dependencies.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,33 @@ def test_get_build_backend(
129129
assert expected_results == actual
130130

131131

132+
def test_get_pyproject_contents_out_of_order_tables(tmp_path: pathlib.Path) -> None:
133+
# Regression test for #1356: a spec-valid pyproject.toml that re-opens a
134+
# super-table (created via dotted keys) out of order. tomlkit rejects this,
135+
# stdlib tomllib accepts it.
136+
tmp_path.joinpath("pyproject.toml").write_text(
137+
textwrap.dedent("""\
138+
[build-system]
139+
requires = ["setuptools", "wheel"]
140+
build-backend = "setuptools.build_meta"
141+
142+
[tool.ruff]
143+
lint.extend-select = ["UP"]
144+
lint.ignore = ["E402"]
145+
146+
[[tool.mypy.overrides]]
147+
module = ["pkg.mod"]
148+
149+
[tool.ruff.lint.per-file-ignores]
150+
"__init__.py" = ["F401"]
151+
""")
152+
)
153+
154+
contents = dependencies.get_pyproject_contents(tmp_path)
155+
156+
assert contents["build-system"]["requires"] == ["setuptools", "wheel"]
157+
158+
132159
def _clean_build_artifacts(f: typing.Callable[..., None]) -> typing.Callable[..., None]:
133160
@functools.wraps(f)
134161
def _with_cleanup(*args: typing.Any, **kwds: typing.Any) -> None:

0 commit comments

Comments
 (0)