Skip to content

MAINT: Replace try/except/pass with contextlib.suppress (SIM105)#3767

Open
jonasboos wants to merge 3 commits into
py-pdf:mainfrom
jonasboos:maint-sim105-contextlib-suppress
Open

MAINT: Replace try/except/pass with contextlib.suppress (SIM105)#3767
jonasboos wants to merge 3 commits into
py-pdf:mainfrom
jonasboos:maint-sim105-contextlib-suppress

Conversation

@jonasboos
Copy link
Copy Markdown
Contributor

Description

Replaces 20 try/except/pass patterns with contextlib.suppress() as suggested by ruff's SIM105 rule. This is a readability improvement that simplifies error suppression in non-critical paths.

Changes

  • Replaced all try: ... except <Exception>: pass blocks with with contextlib.suppress(<Exception>): ...
  • Preserved meaningful comments from original except blocks as inline comments on the with statement (e.g., # keep previous size, # pragma: no cover, # Occurs when called on PdfReader, # delete an already existing page)
  • Removed SIM105 from the ruff ignore list in pyproject.toml since all violations are now fixed
  • Added import contextlib to affected modules

Files changed

  • pypdf/_doc_common.py — 1 replacement (AttributeError)
  • pypdf/_page.py — 3 replacements (AttributeError, KeyError, ValueError)
  • pypdf/_reader.py — 2 replacements (Exception)
  • pypdf/_text_extraction/_text_extractor.py — 1 replacement (Exception)
  • pypdf/_writer.py — 5 replacements (KeyError, AttributeError, Exception, KeyError+AttributeError)
  • pypdf/generic/_data_structures.py — 1 replacement (KeyError)
  • pypdf/generic/_viewerpref.py — 2 replacements (AttributeError, KeyError)
  • tests/test_reader.py — 1 replacement (Exception)
  • pyproject.toml — removed SIM105 from ignore list

Contributes to #3327.

Replace 20 try/except/pass patterns with contextlib.suppress() as
suggested by ruff's SIM105 rule. This is a readability improvement
that simplifies error suppression in non-critical paths.

Comments from the original except blocks have been preserved where
they provided useful context (e.g., 'keep previous size', 'pragma: no
cover', 'Occurs when called on PdfReader').

Also removes SIM105 from the ruff ignore list in pyproject.toml
since all violations are now fixed.

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:18
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 addresses Ruff’s SIM105 recommendation by replacing try/except/pass blocks with contextlib.suppress(...) and then removing the global SIM105 ignore from the Ruff configuration, improving readability while keeping the same “non-critical error suppression” behavior.

Changes:

  • Refactored multiple try/except/pass blocks into with contextlib.suppress(...): across core modules and one test.
  • Added import contextlib to affected modules to support the new suppression style.
  • Removed SIM105 from [tool.ruff.lint].ignore in pyproject.toml now that violations are resolved.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_reader.py Replaces a try/except/pass with contextlib.suppress in a malformed-header test.
pyproject.toml Removes SIM105 from Ruff’s global ignore list.
pypdf/generic/_viewerpref.py Uses contextlib.suppress for optional attribute/key deletion paths.
pypdf/generic/_data_structures.py Uses contextlib.suppress(KeyError) when copying optional fields.
pypdf/_writer.py Multiple suppress refactors; adds contextlib import; includes a touched hunk with an existing-but-noticed error message.
pypdf/_text_extraction/_text_extractor.py Uses contextlib.suppress to keep previous font size on parse failures.
pypdf/_reader.py Uses contextlib.suppress in xref parsing; adds contextlib import.
pypdf/_page.py Uses contextlib.suppress for a few non-critical exception paths; adds contextlib import.
pypdf/_doc_common.py Uses contextlib.suppress(AttributeError) when copying an indirect reference; adds contextlib import.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pypdf/_writer.py
Comment thread pypdf/_reader.py
Comment thread pypdf/_writer.py
@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.64%. Comparing base (4c64630) to head (14ad76e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3767      +/-   ##
==========================================
+ Coverage   97.62%   97.64%   +0.02%     
==========================================
  Files          55       55              
  Lines       10221    10192      -29     
  Branches     1876     1876              
==========================================
- Hits         9978     9952      -26     
+ Misses        138      135       -3     
  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.

Move  from after the conditional typing imports block
to the stdlib imports section, following isort conventions.

Signed-off-by: Jonas Boos <jonasboos@users.noreply.github.com>
@jonasboos
Copy link
Copy Markdown
Contributor Author

Thanks for the review feedback! I've addressed the import ordering issue — import contextlib is now in the stdlib import section rather than after the conditional typing imports.

Regarding the third comment about reset_translation: the with contextlib.suppress(Exception) and raise Exception(...) patterns were pre-existing code, not introduced by this PR. The scope of this PR is specifically the SIM105 rule (replacing try/except/pass with contextlib.suppress). Narrowing the exception types or improving error messages would be a separate improvement.

Signed-off-by: Jonas Boos <jonasboos@users.noreply.github.com>

self._space_width = self.font.space_width / 2 # Actually the width of _half_ a space...
try:
with contextlib.suppress(Exception): # keep previous size
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.

While we are at it, I suggest tightening the suppressed exceptions here.

Comment thread pypdf/_reader.py
if entry_type_b == b"n":
self.xref[generation][num] = offset
try:
with contextlib.suppress(Exception):
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.

While we are at it, I suggest tightening the suppressed exceptions here.

Comment thread pypdf/_reader.py
except Exception:
pass
try:
with contextlib.suppress(Exception):
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.

While we are at it, I suggest tightening the suppressed exceptions here.

Comment thread pypdf/_writer.py
}
)
try:
with contextlib.suppress(AttributeError): # pragma: no cover
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.

Keeping the pragma seems wrong, as this will not report coverage issues with the body.

Comment thread pypdf/_writer.py
self._id_translated = {}
elif isinstance(reader, PdfReader):
try:
with contextlib.suppress(Exception):
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.

While we are at it, I suggest tightening the suppressed exceptions here.

Comment thread pypdf/_writer.py
pass
elif isinstance(reader, IndirectObject):
try:
with contextlib.suppress(Exception):
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.

While we are at it, I suggest tightening the suppressed exceptions here.

Comment thread tests/test_reader.py
assert exc.value.args[0] == "PDF starts with 'foo', but '%PDF-' expected"
caplog.clear()
try:
with contextlib.suppress(Exception):
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.

While we are at it, I suggest tightening the suppressed exceptions here.

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