From 0a9a96cbd0af77c4c3e221c5cfdfbca71b1794a0 Mon Sep 17 00:00:00 2001 From: Jonas Boos Date: Sun, 10 May 2026 20:35:08 +0000 Subject: [PATCH] MAINT: Move try block statements to else blocks (TRY300) 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 #3327. Signed-off-by: Jonas Boos --- pypdf/_codecs/_codecs.py | 4 ++-- pypdf/_reader.py | 3 ++- pypdf/generic/_utils.py | 3 ++- pyproject.toml | 1 - 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pypdf/_codecs/_codecs.py b/pypdf/_codecs/_codecs.py index 93f95e5aad..c723de35df 100644 --- a/pypdf/_codecs/_codecs.py +++ b/pypdf/_codecs/_codecs.py @@ -172,10 +172,10 @@ def _next_code_decode(self, data: bytes) -> int: # Reduce data to get rid of the overhead, # which increases performance on large streams significantly. self._next_data = self._next_data & 0xFFFFF - - return code except IndexError: return self.EOD_MARKER + else: + return code # The following method has been converted to Python from PDFsharp: # https://github.com/empira/PDFsharp/blob/5fbf6ed14740bc4e16786816882d32e43af3ff5d/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Filters/LzwDecode.cs diff --git a/pypdf/_reader.py b/pypdf/_reader.py index db9279f405..08a7457742 100644 --- a/pypdf/_reader.py +++ b/pypdf/_reader.py @@ -1044,9 +1044,10 @@ def _read_xref_other_error( logger_warning("Invalid parent xref., rebuild xref", __name__) try: self._rebuild_xref_table(stream) - return None except Exception: raise PdfReadError("Cannot rebuild xref") + else: + return None raise PdfReadError("Could not find xref table at specified location") def _sanitize_pdf15_xref_stream_index_pairs( diff --git a/pypdf/generic/_utils.py b/pypdf/generic/_utils.py index 1b81fd23b0..45e738f616 100644 --- a/pypdf/generic/_utils.py +++ b/pypdf/generic/_utils.py @@ -185,9 +185,10 @@ def create_string_object( retval = TextStringObject(decode_pdfdocencoding(string)) retval._original_bytes = string retval.autodetect_pdfdocencoding = True - return retval except UnicodeDecodeError: return ByteStringObject(string) + else: + return retval else: raise TypeError("create_string_object should have str or unicode arg") diff --git a/pyproject.toml b/pyproject.toml index d121340864..e7d11de180 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -204,7 +204,6 @@ ignore = [ "TRY003", # Avoid specifying long messages outside the exception class "TRY004", # Prefer `TypeError` exception for invalid type "TRY201", # Use `raise` without specifying exception name - "TRY300", # Consider moving this statement to an `else` block "TRY301", # Abstract `raise` to an inner function "UP006", # Non-PEP 585 annotation. As long as we are not on Python 3.11+ "UP007", # Non-PEP 604 annotation. As long as we are not on Python 3.11+