Skip to content

MAINT: Move try block statements to else blocks (TRY300)#3769

Open
jonasboos wants to merge 1 commit into
py-pdf:mainfrom
jonasboos:maint-try300-else-block
Open

MAINT: Move try block statements to else blocks (TRY300)#3769
jonasboos wants to merge 1 commit into
py-pdf:mainfrom
jonasboos:maint-try300-else-block

Conversation

@jonasboos
Copy link
Copy Markdown
Contributor

Description

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

  • pypdf/_codecs/_codecs.py: Moved return code from try block to else block
  • pypdf/_reader.py: Moved return None from try block to else block after xref rebuild
  • pypdf/generic/_utils.py: Moved return retval from try block to else block
  • pyproject.toml: Removed TRY300 from the ruff ignore list since all violations are now fixed

Contributes to #3327.

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>
Copilot AI review requested due to automatic review settings May 10, 2026 20:35
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/except blocks to use else for the post-success return.
  • Removed TRY300 from the global ruff ignore list in pyproject.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
Copy link
Copy Markdown

codecov Bot commented May 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.62%. Comparing base (4c64630) to head (0a9a96c).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread pypdf/generic/_utils.py
@@ -185,9 +185,10 @@ def create_string_object(
retval = TextStringObject(decode_pdfdocencoding(string))
retval._original_bytes = string
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to move this and the following line into the else block as well? They should not raise an UnicodeDecodeError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants