MAINT: Move try block statements to else blocks (TRY300)#3769
Conversation
Move return/yield statements from the end of try blocks to the corresponding else blocks, following ruff's TRY300 rule. This makes the control flow clearer by explicitly separating the code that may raise an exception from the code that only runs on success. Changes: - _codecs/_codecs.py: Move `return code` to else block - _reader.py: Move `return None` to else block after xref rebuild - generic/_utils.py: Move `return retval` to else block Also removes TRY300 from the ruff ignore list in pyproject.toml. Contributes to py-pdf#3327. Signed-off-by: Jonas Boos <jonasboos@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR aligns the codebase with ruff’s TRY300 rule by moving “success-path” return statements from the end of try blocks into corresponding else blocks, improving control-flow clarity and enabling removal of the global TRY300 ignore.
Changes:
- Refactored three
try/exceptblocks to useelsefor the post-successreturn. - Removed
TRY300from the global ruff ignore list inpyproject.toml.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pyproject.toml | Removes the global TRY300 ignore now that violations are addressed. |
| pypdf/generic/_utils.py | Moves return retval in create_string_object into a try/except/else structure. |
| pypdf/_reader.py | Moves return None after _rebuild_xref_table() into the else block. |
| pypdf/_codecs/_codecs.py | Moves return code into else in _next_code_decode() to satisfy TRY300. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3769 +/- ##
=======================================
Coverage 97.62% 97.62%
=======================================
Files 55 55
Lines 10221 10221
Branches 1876 1876
=======================================
Hits 9978 9978
Misses 138 138
Partials 105 105 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| @@ -185,9 +185,10 @@ def create_string_object( | |||
| retval = TextStringObject(decode_pdfdocencoding(string)) | |||
| retval._original_bytes = string | |||
There was a problem hiding this comment.
Wouldn't it make sense to move this and the following line into the else block as well? They should not raise an UnicodeDecodeError.
Description
Move return/yield statements from the end of
tryblocks to the correspondingelseblocks, following ruff's TRY300 rule. This makes the control flow clearer by explicitly separating the code that may raise an exception from the code that only runs on success.Changes
pypdf/_codecs/_codecs.py: Movedreturn codefrom try block to else blockpypdf/_reader.py: Movedreturn Nonefrom try block to else block after xref rebuildpypdf/generic/_utils.py: Movedreturn retvalfrom try block to else blockpyproject.toml: Removed TRY300 from the ruff ignore list since all violations are now fixedContributes to #3327.