-
Notifications
You must be signed in to change notification settings - Fork 285
Fix Python 3.10 #2230
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
Fix Python 3.10 #2230
Conversation
Some python 3.12+ code has been merged, breaking compatibility with 3.10. Signed-off-by: Oliver Walsh <[email protected]>
Reviewer's GuideAdds Python 3.10 compatibility by introducing a StrEnum fallback, ensuring temp file handling works on 3.10, and extending CI to run unit and e2e test matrices across both default and Python 3.10 environments on all platforms. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Summary of ChangesHello @olliewalsh, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses compatibility issues with Python 3.10 by backporting certain features and adjusting code that relied on Python 3.11+ specific functionalities. The changes ensure that the application and its test suite can run seamlessly on Python 3.10, broadening the supported Python environment. This involves creating a compatibility layer for Highlights
Ignored Files
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- In the new temporary file handling (e.g.,
test_accel_imageandbuild_containerfile), you're manually managingNamedTemporaryFileand callingclose()both in the main path and infinally; consider using a singlewith NamedTemporaryFile(...) as tfile:plusos.unlink(tfile.name)after the context (or only closing infinally) to avoid double-closing and make the lifetime clearer. - The GitHub Actions matrices for adding Python 3.10 are repeated with nearly identical
variant/uv-optionsstructures across multiple jobs; consider extracting these into a reusable pattern (e.g., YAML anchors or a composite action) to reduce duplication and keep Python version support easier to maintain. - On the Windows E2E job you switched to
uv run tox -e e2e -- -vbut dropped the explicit tox/dev-dependency installation; double-check that tox is resolvable viauv runfrom your project metadata or reintroduce a minimal step to ensure tox is available without relying on global state.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the new temporary file handling (e.g., `test_accel_image` and `build_containerfile`), you're manually managing `NamedTemporaryFile` and calling `close()` both in the main path and in `finally`; consider using a single `with NamedTemporaryFile(...) as tfile:` plus `os.unlink(tfile.name)` after the context (or only closing in `finally`) to avoid double-closing and make the lifetime clearer.
- The GitHub Actions matrices for adding Python 3.10 are repeated with nearly identical `variant`/`uv-options` structures across multiple jobs; consider extracting these into a reusable pattern (e.g., YAML anchors or a composite action) to reduce duplication and keep Python version support easier to maintain.
- On the Windows E2E job you switched to `uv run tox -e e2e -- -v` but dropped the explicit tox/dev-dependency installation; double-check that tox is resolvable via `uv run` from your project metadata or reintroduce a minimal step to ensure tox is available without relying on global state.
## Individual Comments
### Comment 1
<location> `ramalama/engine.py:251-258` </location>
<code_context>
Return the ID of the built image.
"""
- with NamedTemporaryFile(delete_on_close=False) as tfile:
+ tfile = NamedTemporaryFile(delete=False)
+ try:
tfile.write(content.encode("utf-8"))
tfile.close()
return self.build(tfile.name, context, tag=tag)
+ finally:
+ tfile.close()
+ os.unlink(tfile.name)
</code_context>
<issue_to_address>
**issue (bug_risk):** Handle potential errors when unlinking an already-removed temporary file and avoid double-closing
If `self.build(...)` (or another process) removes the temp file, `os.unlink(tfile.name)` will raise `FileNotFoundError` and can hide the original error. Also, `tfile.close()` is called both in the `try` block and in `finally`, which is redundant. A safer pattern is:
```python
tfile = NamedTemporaryFile(delete=False)
try:
tfile.write(content.encode("utf-8"))
finally:
tfile.close()
try:
return self.build(tfile.name, context, tag=tag)
finally:
with contextlib.suppress(FileNotFoundError):
os.unlink(tfile.name)
```
This keeps cleanup robust, avoids double-closing, and ensures the temp file is removed even on error without masking exceptions.
</issue_to_address>
### Comment 2
<location> `ramalama/compat.py:9` </location>
<code_context>
+except ImportError:
+ from enum import Enum
+
+ class StrEnum(str, Enum): # type: ignore[no-redef]
+ """StrEnum class for Python 3.10."""
+
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Backported StrEnum does not fully match stdlib StrEnum semantics (notably str() behavior)
The stdlib `enum.StrEnum` has different `str()` semantics than a plain `Enum`: `str(Member)` returns the value, not `Class.Member`. With this backport, `str(MyStrEnum.FOO)` will still be `'MyStrEnum.FOO'`, which can cause behavioral differences between 3.10 and 3.11+ if code relies on the real `StrEnum`. Consider overriding `__str__` (and possibly `__repr__`) to match:
```python
class StrEnum(str, Enum):
def __str__(self) -> str: # type: ignore[override]
return str(self.value)
```
</issue_to_address>
### Comment 3
<location> `test/unit/test_common.py:158` </location>
<code_context>
+def test_accel_image(accel_env: str, env_override, config_override: str, expected_result, monkeypatch: pytest.MonkeyPatch):
</code_context>
<issue_to_address>
**suggestion (testing):** Add tests for the new Python 3.10 `StrEnum` compatibility layer
The PR adds `ramalama.compat.StrEnum` for Python 3.10, but there are no tests for it. Please add a small test module (e.g. `test_compat.py`) that imports `StrEnum`, defines a sample enum subclass, and verifies that members are instances of both `str` and `Enum`, that equality and `__str__` behave correctly, and that it imports correctly on both 3.10 and 3.11+ so CI actually validates the compat layer.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request successfully adds compatibility for Python 3.10 by introducing shims for features from newer Python versions, such as StrEnum and contextlib.chdir. The changes to temporary file handling correctly address potential resource leaks. I have a couple of suggestions to refine the new temporary file logic to make it more idiomatic and less redundant, which will improve code clarity and maintainability.
Signed-off-by: Oliver Walsh <[email protected]>
Signed-off-by: Oliver Walsh <[email protected]>
Signed-off-by: Oliver Walsh <[email protected]>
4487a83 to
2d2c5f0
Compare
|
LGTM |
We currently depend on some python 3.11+ features. Add unit/e2e test coverage for Python 3.10 and workaround the issues.
Summary by Sourcery
Add Python 3.10 compatibility and extend CI coverage to run unit and end-to-end tests across both default and Python 3.10 environments.
New Features:
Bug Fixes:
Enhancements:
CI: