Skip to content

Commit

Permalink
Add error message specifying that we can't parse pyproject.toml (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
mscheifer committed May 31, 2024
1 parent 95977b4 commit 1459c57
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repos:
- id: check-case-conflict
- id: check-json
- id: check-toml
exclude: tests/fixtures/project_duplicate_dependency/pyproject.toml
- id: check-yaml
- id: pretty-format-json
args:
Expand Down
17 changes: 15 additions & 2 deletions src/poetry/core/pyproject/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,21 @@ def data(self) -> dict[str, Any]:
if not self.path.exists():
self._data = {}
else:
with self.path.open("rb") as f:
self._data = tomllib.load(f)
try:
with self.path.open("rb") as f:
self._data = tomllib.load(f)
except tomllib.TOMLDecodeError as e:
from poetry.core.pyproject.exceptions import PyProjectException

msg = (
f"{self._path.as_posix()} is not a valid TOML file.\n"
f"{e.__class__.__name__}: {e}"
)

if str(e).startswith("Cannot overwrite a value"):
msg += "\nThis is often caused by a duplicate entry."

raise PyProjectException(msg) from e

return self._data

Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/project_duplicate_dependency/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
name = "minimal"
version = "1"

[tool.poetry.dependencies]
python = "^3.8"
python = "^3.8"
18 changes: 18 additions & 0 deletions tests/pyproject/test_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,21 @@ def test_pyproject_toml_non_existent(pyproject_toml: Path) -> None:
assert pyproject.data == {}
assert build_system.requires == ["poetry-core"]
assert build_system.build_backend == "poetry.core.masonry.api"


def test_unparseable_pyproject_toml() -> None:
pyproject_toml = (
Path(__file__).parent.parent
/ "fixtures"
/ "project_duplicate_dependency"
/ "pyproject.toml"
)

with pytest.raises(PyProjectException) as excval:
_ = PyProjectTOML(pyproject_toml).build_system

assert (
f"{pyproject_toml.as_posix()} is not a valid TOML file.\n"
"TOMLDecodeError: Cannot overwrite a value (at line 7, column 16)\n"
"This is often caused by a duplicate entry"
) in str(excval.value)

0 comments on commit 1459c57

Please sign in to comment.