Repository navigation
fix(python-uv): compile the function's source to bytecode too #978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomaDev
wants to merge
2
commits into
aws:develop
Choose a base branch
from
tomaDev:fix/python-uv-compile-source-bytecode
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
| from pathlib import Path | ||
| from unittest import TestCase | ||
| from unittest.mock import patch | ||
|
|
||
| from aws_lambda_builders.workflows.python_uv.actions import PythonUvCompileSourceAction | ||
| from aws_lambda_builders.workflows.python_uv.exceptions import MissingUvError | ||
|
|
||
|
|
||
| class TestPythonUvCompileSourceAction(TestCase): | ||
| def setUp(self): | ||
| self.source_dir = tempfile.mkdtemp() | ||
| self.artifacts_dir = tempfile.mkdtemp() | ||
| self.addCleanup(shutil.rmtree, self.source_dir) | ||
| self.addCleanup(shutil.rmtree, self.artifacts_dir) | ||
|
|
||
| def _execute(self, runtime="python3.13", excludes=("venv", "*.egg-info")): | ||
| with ( | ||
| patch("aws_lambda_builders.workflows.python_uv.actions.SubprocessUv"), | ||
| patch("aws_lambda_builders.workflows.python_uv.actions.UvRunner") as runner_class, | ||
| ): | ||
| PythonUvCompileSourceAction(self.source_dir, self.artifacts_dir, runtime, excludes).execute() | ||
| return runner_class.return_value.compile_bytecode | ||
|
|
||
| def test_compiles_only_what_was_copied_from_the_source(self): | ||
| for root in (self.source_dir, self.artifacts_dir): | ||
| Path(root, "app.py").touch() | ||
| Path(root, "pkg").mkdir() | ||
| Path(root, "requirements.txt").touch() | ||
| # Excluded from the copy, so not part of the function's source, even where the | ||
| # artifacts directory has an entry of the same name | ||
| for root in (self.source_dir, self.artifacts_dir): | ||
| Path(root, "venv").mkdir() | ||
| Path(root, "app.egg-info").mkdir() | ||
| # Installed dependencies, already compiled by the dependency build | ||
| Path(self.artifacts_dir, "six.py").touch() | ||
| Path(self.artifacts_dir, "requests").mkdir() | ||
|
|
||
| compile_bytecode = self._execute() | ||
|
|
||
| compile_bytecode.assert_called_once_with( | ||
| "3.13", [os.path.join(self.artifacts_dir, "app.py"), os.path.join(self.artifacts_dir, "pkg")] | ||
| ) | ||
|
|
||
| def test_does_nothing_without_python_source(self): | ||
| Path(self.source_dir, "requirements.txt").touch() | ||
| Path(self.artifacts_dir, "requirements.txt").touch() | ||
|
|
||
| self._execute().assert_not_called() | ||
|
|
||
| def test_passes_no_python_version_without_a_runtime(self): | ||
| Path(self.source_dir, "app.py").touch() | ||
| Path(self.artifacts_dir, "app.py").touch() | ||
|
|
||
| self._execute(runtime=None).assert_called_once_with(None, [os.path.join(self.artifacts_dir, "app.py")]) | ||
|
|
||
| def test_does_nothing_without_a_source_directory(self): | ||
| shutil.rmtree(self.source_dir) | ||
| self.addCleanup(os.mkdir, self.source_dir) | ||
|
|
||
| self._execute().assert_not_called() | ||
|
|
||
| def test_warns_instead_of_failing_when_uv_is_missing(self): | ||
| Path(self.source_dir, "app.py").touch() | ||
| Path(self.artifacts_dir, "app.py").touch() | ||
|
|
||
| with ( | ||
| patch("aws_lambda_builders.workflows.python_uv.actions.SubprocessUv", side_effect=MissingUvError()), | ||
| self.assertLogs("aws_lambda_builders.workflows.python_uv.actions", level="WARNING") as logs, | ||
| ): | ||
| PythonUvCompileSourceAction(self.source_dir, self.artifacts_dir, "python3.13").execute() | ||
|
|
||
| self.assertIn("Skipping bytecode compilation of the source: uv executable not found", logs.output[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.