fix: close ZipFile in _prefetch_metadata with context manager#10800
Merged
radoering merged 3 commits intopython-poetry:mainfrom Apr 3, 2026
Merged
fix: close ZipFile in _prefetch_metadata with context manager#10800radoering merged 3 commits intopython-poetry:mainfrom
radoering merged 3 commits intopython-poetry:mainfrom
Conversation
Reviewer's GuideRefactors LazyWheelOverHTTP._prefetch_metadata to open ZipFile with a context manager so it is always closed, and adds a regression test to assert the ZipFile is closed even when metadata discovery fails with an error. Sequence diagram for _prefetch_metadata with ZipFile context managersequenceDiagram
participant caller as LazyWheelOverHTTP_caller
participant wheel as LazyWheelOverHTTP
participant zlib as ZipFile
caller->>wheel: _prefetch_metadata(name)
activate wheel
wheel->>zlib: __enter__()
activate zlib
loop iterate_zip_entries
wheel->>zlib: infolist()
zlib-->>wheel: list[ZipInfo]
wheel->>wheel: search for metadata filename
end
alt metadata_found
wheel->>wheel: determine start and end offsets
wheel->>wheel: _ensure_downloaded(start, end)
wheel-->>caller: return metadata_path
else metadata_not_found
wheel->>wheel: raise UnsupportedWheelError
end
zlib-->>wheel: __exit__()
deactivate zlib
deactivate wheel
Class diagram for LazyWheelOverHTTP and supporting types in _prefetch_metadataclassDiagram
class LazyWheelOverHTTP {
+str name
+Pattern _metadata_regex
+str _prefetch_metadata(str name)
+_ensure_downloaded(int start, int end)
}
class ZipFile {
+infolist() list~ZipInfo~
+start_dir int
+close() void
}
class ZipInfo {
+str filename
+int header_offset
}
class UnsupportedWheelError {
+UnsupportedWheelError(str message)
}
LazyWheelOverHTTP ..> ZipFile : uses
LazyWheelOverHTTP ..> ZipInfo : iterates
LazyWheelOverHTTP ..> UnsupportedWheelError : raises
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
test_prefetch_metadata_closes_zipfile_on_error, consider assertingpytest.raises(UnsupportedWheelError, ...)instead of catching a genericExceptionto keep the test focused on the expected failure mode. - The new test heavily mocks
ZipFile; you might simplify and make it more robust by constructing a small in-memory wheel/zip that lacks a matching.dist-info/METADATAentry instead of patchingZipFileand its methods.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `test_prefetch_metadata_closes_zipfile_on_error`, consider asserting `pytest.raises(UnsupportedWheelError, ...)` instead of catching a generic `Exception` to keep the test focused on the expected failure mode.
- The new test heavily mocks `ZipFile`; you might simplify and make it more robust by constructing a small in-memory wheel/zip that lacks a matching `.dist-info/METADATA` entry instead of patching `ZipFile` and its methods.
## Individual Comments
### Comment 1
<location path="tests/inspection/test_lazy_wheel.py" line_range="496-497" />
<code_context>
+ mock_zf.__exit__ = MagicMock(return_value=False)
+
+ with (
+ patch("poetry.inspection.lazy_wheel.ZipFile", return_value=mock_zf),
+ pytest.raises(Exception, match=r"no .* found for"),
+ ):
+ lazy._prefetch_metadata("test-pkg")
</code_context>
<issue_to_address>
**suggestion (testing):** Assert the specific `UnsupportedWheelError` instead of a generic `Exception`
This test is asserting that `_prefetch_metadata` raises `UnsupportedWheelError` when no matching `.dist-info/METADATA` is found, but `pytest.raises(Exception, ...)` is too broad and would still pass if a different exception were raised. Please import and assert the specific exception:
```python
from poetry.inspection.lazy_wheel import LazyWheelOverHTTP, UnsupportedWheelError
...
with (
patch("poetry.inspection.lazy_wheel.ZipFile", return_value=mock_zf),
pytest.raises(UnsupportedWheelError, match=r"no .* found for"),
):
lazy._prefetch_metadata("test-pkg")
```
This keeps the test aligned with the intended contract and will fail if the exception type changes unexpectedly.
Suggested implementation:
```python
from poetry.inspection.lazy_wheel import LazyWheelOverHTTP, UnsupportedWheelError
```
```python
with (
patch("poetry.inspection.lazy_wheel.ZipFile", return_value=mock_zf),
pytest.raises(UnsupportedWheelError, match=r"no .* found for"),
):
lazy._prefetch_metadata("test-pkg")
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
ZipFile opened in _prefetch_metadata was never closed. If an UnsupportedWheelError was raised, the handle leaked. Changed bare ZipFile() assignment to a with statement for proper cleanup. Co-authored-by: Copilot <[email protected]>
17bda58 to
6628f9d
Compare
radoering
previously approved these changes
Apr 3, 2026
radoering
approved these changes
Apr 3, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
ZipFile opened in _prefetch_metadata was never closed. If an UnsupportedWheelError was raised, the handle leaked. Changed bare ZipFile() assignment to a with statement for proper cleanup.
github and whitespace do not make a pretty diff, nothing has changed in the main body of what happens
Summary by Sourcery
Ensure wheel metadata prefetching closes ZipFile handles even when errors occur.
Bug Fixes:
Tests: