MAINT: Replace try/except/pass with contextlib.suppress (SIM105)#3767
MAINT: Replace try/except/pass with contextlib.suppress (SIM105)#3767jonasboos wants to merge 3 commits into
Conversation
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>
There was a problem hiding this comment.
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/passblocks intowith contextlib.suppress(...):across core modules and one test. - Added
import contextlibto affected modules to support the new suppression style. - Removed
SIM105from[tool.ruff.lint].ignoreinpyproject.tomlnow 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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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>
|
Thanks for the review feedback! I've addressed the import ordering issue — Regarding the third comment about |
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 |
There was a problem hiding this comment.
While we are at it, I suggest tightening the suppressed exceptions here.
| if entry_type_b == b"n": | ||
| self.xref[generation][num] = offset | ||
| try: | ||
| with contextlib.suppress(Exception): |
There was a problem hiding this comment.
While we are at it, I suggest tightening the suppressed exceptions here.
| except Exception: | ||
| pass | ||
| try: | ||
| with contextlib.suppress(Exception): |
There was a problem hiding this comment.
While we are at it, I suggest tightening the suppressed exceptions here.
| } | ||
| ) | ||
| try: | ||
| with contextlib.suppress(AttributeError): # pragma: no cover |
There was a problem hiding this comment.
Keeping the pragma seems wrong, as this will not report coverage issues with the body.
| self._id_translated = {} | ||
| elif isinstance(reader, PdfReader): | ||
| try: | ||
| with contextlib.suppress(Exception): |
There was a problem hiding this comment.
While we are at it, I suggest tightening the suppressed exceptions here.
| pass | ||
| elif isinstance(reader, IndirectObject): | ||
| try: | ||
| with contextlib.suppress(Exception): |
There was a problem hiding this comment.
While we are at it, I suggest tightening the suppressed exceptions here.
| assert exc.value.args[0] == "PDF starts with 'foo', but '%PDF-' expected" | ||
| caplog.clear() | ||
| try: | ||
| with contextlib.suppress(Exception): |
There was a problem hiding this comment.
While we are at it, I suggest tightening the suppressed exceptions here.
Description
Replaces 20
try/except/passpatterns withcontextlib.suppress()as suggested by ruff's SIM105 rule. This is a readability improvement that simplifies error suppression in non-critical paths.Changes
try: ... except <Exception>: passblocks withwith contextlib.suppress(<Exception>): ...exceptblocks as inline comments on thewithstatement (e.g.,# keep previous size,# pragma: no cover,# Occurs when called on PdfReader,# delete an already existing page)SIM105from the ruff ignore list inpyproject.tomlsince all violations are now fixedimport contextlibto affected modulesFiles 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 listContributes to #3327.