Repository navigation
refactor: simplify code paths found in review - #1124
Conversation
| class _BuilderKwargs(TypedDict, total=False): | ||
| """Optional keyword arguments shared by both ``ProjectBuilder`` constructors.""" | ||
|
|
||
| runner: SubprocessRunner |
There was a problem hiding this comment.
Replicating the builder's parameters in TypedDict format looks like code smell to me. What's the benefit here? The type checker should be able to map partial args to the builder's signature, and if it doesn't, that's just unfortunate.
There was a problem hiding this comment.
type: commands[0]> mypy
src/build/__main__.py:228: error: Argument 2 to "ProjectBuilder" has incompatible type "**dict[str, SubprocessRunner]"; expected "str" [arg-type]
Found 1 error in 1 file (checked 24 source files)
type: exit 1 (1.40 seconds) /Users/henryfs/git/pypa/build/.claude/worktrees/agent-a94ebc74b954f1e58> mypy pid=74554
.pkg: _exit> python /Users/henryfs/.local/share/uv/tools/tox/lib/python3.14/site-packages/pyproject_api/_backend.py True flit_core.buildapi
type: FAIL code 1 (2.70=setup[1.31]+cmd[1.40] seconds)
evaluation failed :( (2.74 seconds)
There was a problem hiding this comment.
And this is used twice (different functions), so you can't nicely use a partial.
There was a problem hiding this comment.
We could allow None to select the default runner in Builder/.from_isolated_env. Or we could pass pyproject_hooks.default_subprocess_runner here (which would be a smaller change).
There was a problem hiding this comment.
I've gone with the simpler change, let me know if you'd rather teach these to accept None.
| else: | ||
| try: | ||
| os.makedirs(outdir, exist_ok=True) | ||
| except (FileExistsError, NotADirectoryError): |
There was a problem hiding this comment.
These are not exactly equivalent. An intermediate folder could also turn out to be a file, in which case build path doesn't exist at all and the error message would be misleading.
Does os.makedirs ever raise NotADirectoryError?
There was a problem hiding this comment.
On linux/unix, yes, if the intermediate path component is a file. On windows, I'm not sure it's guaranteed. OSError would be safer but might over-catch.
Fine to revert this, usually the rule is it's best to not check file paths, and instead catch errors, but the alternative is really basically doing that too, so it's not better.
There was a problem hiding this comment.
Ahh, I can't read a diff, the os.makedirs was there before, it's white. The rule is applicable here then, since it's possible the file system gets modified between the check and the run, causing the exception to be missed, and surfacing the exception from os.makedirs instead.
There was a problem hiding this comment.
I've added a test, let's see what happens on Windows.
There was a problem hiding this comment.
(The old code did leak an exception when there was a file further up the path instead of a directory - I didn't customize that error message, since it's sort of correct, though I can, assuming it works the same way on Windows)
There was a problem hiding this comment.
FileNotFoundError on Windows
- _builder.py: replace redundant re-filtering of already-validated requires/backend-path with list() - _builder.py: collapse the _call_backend exists/isdir/makedirs dance into a single makedirs(exist_ok=True) guarded against FileExistsError/NotADirectoryError - env.py: drop redundant local Requirement import in _has_virtualenv - env.py: use the module-level importlib_metadata alias in _has_dependency instead of re-importing ._compat.importlib - _compat/tarfile.py: hoist the duplicated version predicate into _HAS_DATA_FILTER and remove the now-dead TarFile subclass, exporting only safe_extractall - __main__.py: switch sdist extraction to the plain tarfile.open helper and deduplicate the ProjectBuilder runner-kwargs construction - __main__.py: reuse _validate_sdist_archive in build_package_via_sdist so a nonconforming backend-produced sdist is rejected clearly instead of failing later Assisted-by: ClaudeCode:claude-opus-4.8
Assisted-by: ClaudeCode:claude-opus-4.8
Assisted-by: ClaudeCode:claude-opus-4.8
Assisted-by: ClaudeCode:claude-opus-4.8 Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
5649fee to
5db6425
Compare
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Co-authored-by: layday <layday@protonmail.com>
🤖 AI text below 🤖
Part of the code review in #1116.
Behavior-preserving simplifications spotted in review (except item 7, a small behavior improvement noted below):
_builder.py_parse_build_system_table:requiresandbackend-pathare already validated as lists of strings, so the redundant re-filtering comprehensions become plainlist(...)copies.env.py_has_virtualenv: drop the localfrom packaging.requirements import Requirement— it is already imported at module top.env.py_has_dependency: use the module-levelimportlib_metadataalias instead of re-importing._compat.importlib(same object)._builder.py_call_backend: replace the exists/isdir/makedirs branches (and their TOCTOU window) with a singleos.makedirs(outdir, exist_ok=True)guarded againstFileExistsError/NotADirectoryError, raising the sameBuildException._compat/tarfile.py: hoist the duplicated Python-version predicate into a single_HAS_DATA_FILTERbool and remove the now-deadTarFilesubclass (itsextraction_filterdefault was never used becausesafe_extractallalways passesfilter='data'). Onlysafe_extractallis exported;__main__.pynow uses the plaintarfile.openhelper it already imports.__main__.py_bootstrap_build_env: deduplicate thepartial(...)/runner construction that was repeated in both branches into a sharedbuilder_kwargs.__main__.pybuild_package_via_sdist: reuse_validate_sdist_archive(sdist)to derive the top-level directory instead of assumingsdist_name.removesuffix('.tar.gz'). Slight behavior improvement: a nonconforming backend-produced sdist (e.g. missing PKG-INFO) is now rejected clearly up front instead of failing later with a confusing "Source ... is not a directory" (or a downstream backend error). Thetest_build_package_via_sdist_incomplete_sdisttest was updated to expect this clearer error.All existing tests pass (
282 passed), mypy and pre-commit are clean.