Skip to content

refactor: simplify code paths found in review - #1124

Merged
gaborbernat merged 6 commits into
pypa:mainfrom
henryiii:refactor/review-simplifications
Jul 2, 2026
Merged

gaborbernat merged 6 commits into
pypa:mainfrom
henryiii:refactor/review-simplifications

Conversation

@henryiii

@henryiii henryiii commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🤖 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):

  1. _builder.py _parse_build_system_table: requires and backend-path are already validated as lists of strings, so the redundant re-filtering comprehensions become plain list(...) copies.
  2. env.py _has_virtualenv: drop the local from packaging.requirements import Requirement — it is already imported at module top.
  3. env.py _has_dependency: use the module-level importlib_metadata alias instead of re-importing ._compat.importlib (same object).
  4. _builder.py _call_backend: replace the exists/isdir/makedirs branches (and their TOCTOU window) with a single os.makedirs(outdir, exist_ok=True) guarded against FileExistsError/NotADirectoryError, raising the same BuildException.
  5. _compat/tarfile.py: hoist the duplicated Python-version predicate into a single _HAS_DATA_FILTER bool and remove the now-dead TarFile subclass (its extraction_filter default was never used because safe_extractall always passes filter='data'). Only safe_extractall is exported; __main__.py now uses the plain tarfile.open helper it already imports.
  6. __main__.py _bootstrap_build_env: deduplicate the partial(...)/runner construction that was repeated in both branches into a shared builder_kwargs.
  7. __main__.py build_package_via_sdist: reuse _validate_sdist_archive(sdist) to derive the top-level directory instead of assuming sdist_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). The test_build_package_via_sdist_incomplete_sdist test was updated to expect this clearer error.

All existing tests pass (282 passed), mypy and pre-commit are clean.

Comment thread src/build/__main__.py Outdated
Comment on lines +79 to +82
class _BuilderKwargs(TypedDict, total=False):
"""Optional keyword arguments shared by both ``ProjectBuilder`` constructors."""

runner: SubprocessRunner

@layday layday Jul 1, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this is used twice (different functions), so you can't nicely use a partial.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gone with the simpler change, let me know if you'd rather teach these to accept None.

Comment thread src/build/_builder.py Resolved
Comment thread src/build/_builder.py Outdated
else:
try:
os.makedirs(outdir, exist_ok=True)
except (FileExistsError, NotADirectoryError):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@henryiii henryiii Jul 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test, let's see what happens on Windows.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileNotFoundError on Windows

henryiii added 4 commits July 2, 2026 10:24
- _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>
@henryiii
henryiii force-pushed the refactor/review-simplifications branch from 5649fee to 5db6425 Compare July 2, 2026 14:47
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
@henryiii
henryiii marked this pull request as ready for review July 2, 2026 21:40
@henryiii
henryiii requested a review from gaborbernat as a code owner July 2, 2026 21:40
Comment thread src/build/_builder.py Resolved
Co-authored-by: layday <layday@protonmail.com>
@gaborbernat
gaborbernat merged commit 43c337a into pypa:main Jul 2, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants