Skip to content

Commit 1459c57

Browse files
authored
Add error message specifying that we can't parse pyproject.toml (#734)
1 parent 95977b4 commit 1459c57

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ repos:
1414
- id: check-case-conflict
1515
- id: check-json
1616
- id: check-toml
17+
exclude: tests/fixtures/project_duplicate_dependency/pyproject.toml
1718
- id: check-yaml
1819
- id: pretty-format-json
1920
args:

src/poetry/core/pyproject/toml.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,21 @@ def data(self) -> dict[str, Any]:
2828
if not self.path.exists():
2929
self._data = {}
3030
else:
31-
with self.path.open("rb") as f:
32-
self._data = tomllib.load(f)
31+
try:
32+
with self.path.open("rb") as f:
33+
self._data = tomllib.load(f)
34+
except tomllib.TOMLDecodeError as e:
35+
from poetry.core.pyproject.exceptions import PyProjectException
36+
37+
msg = (
38+
f"{self._path.as_posix()} is not a valid TOML file.\n"
39+
f"{e.__class__.__name__}: {e}"
40+
)
41+
42+
if str(e).startswith("Cannot overwrite a value"):
43+
msg += "\nThis is often caused by a duplicate entry."
44+
45+
raise PyProjectException(msg) from e
3346

3447
return self._data
3548

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "minimal"
3+
version = "1"
4+
5+
[tool.poetry.dependencies]
6+
python = "^3.8"
7+
python = "^3.8"

tests/pyproject/test_pyproject_toml.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ def test_pyproject_toml_non_existent(pyproject_toml: Path) -> None:
7272
assert pyproject.data == {}
7373
assert build_system.requires == ["poetry-core"]
7474
assert build_system.build_backend == "poetry.core.masonry.api"
75+
76+
77+
def test_unparseable_pyproject_toml() -> None:
78+
pyproject_toml = (
79+
Path(__file__).parent.parent
80+
/ "fixtures"
81+
/ "project_duplicate_dependency"
82+
/ "pyproject.toml"
83+
)
84+
85+
with pytest.raises(PyProjectException) as excval:
86+
_ = PyProjectTOML(pyproject_toml).build_system
87+
88+
assert (
89+
f"{pyproject_toml.as_posix()} is not a valid TOML file.\n"
90+
"TOMLDecodeError: Cannot overwrite a value (at line 7, column 16)\n"
91+
"This is often caused by a duplicate entry"
92+
) in str(excval.value)

0 commit comments

Comments
 (0)