Skip to content

fix(python-uv): compile the function's source to bytecode too - #978

Open
tomaDev wants to merge 2 commits into
aws:developfrom
tomaDev:fix/python-uv-compile-source-bytecode
Open

tomaDev wants to merge 2 commits into
aws:developfrom
tomaDev:fix/python-uv-compile-source-bytecode

Conversation

@tomaDev

@tomaDev tomaDev commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Issue #, if available: Fixes #977

Description of changes

With uv_compile_bytecode on, compileall ran inside the dependency install, before CopySourceAction, so the function's own source shipped without bytecode.

  • Add PythonUvCompileSourceAction after CopySourceAction. It compiles only the top-level entries that came from the source directory, so dependencies are not compiled twice.
  • It also runs for builds with no manifest, which now get source bytecode too. Two tests from fix(python-uv): don't crash when bytecode is on and there is no manifest #976 that expected a lone CopySourceAction there are updated.
  • As with dependencies, it never fails the build: a missing uv, target Python or source directory logs a warning and ships no source bytecode.
  • UvRunner.compile_bytecode(python_version, paths) is the shared entry point; _compile_bytecode now takes several paths.

Description of how you validated changes

  • python_uv unit, functional and integration tests pass (277), including the opt-in integration test, which now also checks a top-level module and a package from the source.
  • sam build with SAM CLI 1.174.0 and this branch on a requests + pydantic function: every .py has an unchecked-hash .pyc (200 of 200), also with --cached and with no requirements.txt. A source file with a syntax error logs a warning and the build passes.
  • After a zip round trip with 1980 timestamps, the source modules load from .pyc.
  • ruff check and black --check pass. Not tested on Windows.

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

🤖 Generated with Claude Code

With uv_compile_bytecode on, only the dependencies were compiled:
compileall ran inside the dependency install, before the source was
copied into the artifacts directory.

Add PythonUvCompileSourceAction after CopySourceAction. It compiles what
was copied from the source, so dependencies are not compiled twice, and
it also covers builds that have no manifest. A missing uv or source
directory skips it with a warning instead of failing the build.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@tomaDev
tomaDev requested a review from a team as a code owner November 20, 2026 11:10
@github-actions github-actions Bot added pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Nov 20, 2026
bnusunny
bnusunny previously approved these changes Nov 20, 2026

@bnusunny bnusunny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this fills the gap #926 left. The new PythonUvCompileSourceAction runs after CopySourceAction, and only when bytecode is enabled. It compiles just the top-level entries that CopySourceAction actually copied: the same EXCLUDED_FILES filter, with .py files and directories only. Dependencies aren't compiled a second time. It reuses the same compileall --invalidation-mode unchecked-hash call through UvRunner.compile_bytecode, and it warns instead of failing when uv, the target Python or the source directory is missing.

What I verified locally:

  • tests/unit/workflows/python_uv: 190 passed, 53 subtests.
  • With the new actions.append(...) in workflow.py disabled, 3 tests fail.
  • black --check and ruff check are clean.
  • I ran CopySourceAction and the new action against a real source tree, with the target Python stubbed to the local 3.12 interpreter:
    • The source tree had app.py, a pkg/, a .git/ with a .py inside, a data.txt, a file with a syntax error, and a dependency already installed in the artifacts directory.
    • compileall received only app.py, bad.py and pkg.
    • app.py and pkg/__init__.py got .pyc files with flags=1 (unchecked-hash).
    • .git and the pre-installed dependency weren't touched.
    • The syntax error logged a warning and everything else was still compiled.

One small nit is inline.

…test

The excluded entries only existed in the source directory, so the
artifacts existence check dropped them and the filter was never
exercised. Put them in the artifacts directory too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@bnusunny bnusunny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for pinning the filter. I re-checked on e666ced: 190 unit tests pass, and with the exclude filter replaced by if True the updated test now fails, as it should. Re-approving, since the push dismissed my earlier approval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

python_uv: uv_compile_bytecode compiles dependencies but not the function's own source

2 participants