refactor: ontext managers for PDF - #22
Open
gustavo-szesz wants to merge 3 commits into
Open
Conversation
Owner
|
Thanks for the PR! 🎉 The lint check is failing because two files need formatting. Please run: ruff format src/ tests/This will reformat:
Then commit and push the changes. The CI should pass after that. |
NP-compete
reviewed
Mar 3, 2026
NP-compete
reviewed
Mar 3, 2026
| pdf_name = meta.title if meta and meta.title else "Untitled" | ||
| return sanitize_filename(pdf_name) | ||
| finally: | ||
| if hasattr(pdf_reader, "stream") and hasattr(pdf_reader.stream, "close"): |
Owner
There was a problem hiding this comment.
Closing pdf_reader.stream here will close the caller's BytesIO. They won't be able to reuse it after calling this function. Better to let the caller own the lifecycle.
NP-compete
reviewed
Mar 3, 2026
| return meta.title if meta and meta.title else "Untitled" | ||
| else: | ||
| pdf_reader = PyPDF2.PdfReader(str(pdf_path)) | ||
| try: |
Owner
There was a problem hiding this comment.
This else branch is unreachable. The type hint is str | Path, so the isinstance check above always passes. Safe to remove.
NP-compete
reviewed
Mar 3, 2026
| meta = pdf_reader.metadata | ||
| pdf_name = meta.title if meta and meta.title else "Untitled" | ||
| return sanitize_filename(pdf_name) | ||
| finally: |
Owner
There was a problem hiding this comment.
The hasattr(pdf_reader, "stream") pattern relies on PyPDF2 internals. The with open() approach you used on lines 86-89 is cleaner. Consider using that consistently.
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.
Description
Brief description of the changes in this PR.
Related Issue
Fixes #(issue number)
Type of Change
Checklist
Screenshots (if applicable)
Add screenshots to help explain your changes.
Additional Notes
Any additional information that reviewers should know.