Skip to content

Commit 290f643

Browse files
chore: improve pyproject-api maintenance path (#266)
Summary: - Add unit tests covering edge cases in BackendFailed exception formatting (e.g., empty err/out fields, multi-line tracebacks) and tighten type annotations on internal helpers where Any is used but a narrower type is inferable. - Keep the change narrow so it is straightforward to review. Notes: - I kept this scoped to the relevant implementation and tests. --------- Co-authored-by: Noa Levi <275430404+lphuc2250gma@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 46fde87 commit 290f643

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎src/pyproject_api/_frontend.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def __init__(self, result: dict[str, Any], out: str, err: str) -> None:
150150
#: standard error collected while running the command
151151
self.err = err
152152
#: exit code of the command
153-
self.code: int = result.get("code", -2)
153+
self.code: int | None = result.get("code", -2)
154154
#: the type of exception thrown
155155
self.exc_type: str = result.get("exc_type", "missing Exception type")
156156
#: the string representation of the exception thrown

‎tests/test_frontend.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,25 @@ def test_backend_build_editable_bad(tmp_path: Path, demo_pkg_inline: Path, monke
338338
assert not exc.args
339339
assert exc.exc_type == "TypeError"
340340
assert exc.exc_msg == "'build_editable' on 'build' returned 1 but expected type <class 'str'>"
341+
342+
343+
def test_backend_failed_str_repr() -> None:
344+
exc = BackendFailed({"code": 1, "exc_type": "TypeError", "exc_msg": "bad"}, "out", "err")
345+
assert str(exc) == "packaging backend failed (code=1), with TypeError: bad\nerrout"
346+
assert repr(exc) == ("BackendFailed(result=dict(code=1, exc_type='TypeError',exc_msg='bad'), out='out', err='err')")
347+
348+
exc = BackendFailed({"code": None, "exc_type": "ValueError", "exc_msg": "missing"}, "", "")
349+
assert str(exc) == "packaging backend failed, with ValueError: missing"
350+
assert exc.code is None
351+
352+
exc = BackendFailed({}, "", "")
353+
assert exc.code == -2
354+
assert exc.exc_type == "missing Exception type"
355+
assert exc.exc_msg == "missing Exception message"
356+
assert str(exc) == "packaging backend failed (code=-2), with missing Exception type: missing Exception message"
357+
358+
exc = BackendFailed({"code": 2, "exc_type": "RuntimeError", "exc_msg": "line1\nline2"}, "", "")
359+
assert str(exc) == "packaging backend failed (code=2), with RuntimeError: line1\nline2"
360+
361+
exc = BackendFailed({"code": 3, "exc_type": "Error", "exc_msg": "msg"}, "out\n", "err\n")
362+
assert str(exc) == "packaging backend failed (code=3), with Error: msg\nerr\nout"

0 commit comments

Comments
 (0)