Repository navigation
BUG: Always define PdfWriter._reader - #3960
Merged
stefan6419846 merged 2 commits intoAug 13, 2026
Merged
Conversation
PdfWriterProtocol declares `_reader`, but PdfWriter only assigned it in the incremental branch of __init__. A writer created the normal way has no such attribute, so it does not satisfy the protocol it gets passed as -- `isinstance(writer, PdfWriterProtocol)` is False, and typeguard fails 62 checks across the test suite on that one attribute. Declare it as Optional[PdfReader] defaulting to None. The three reads are all inside `if self.incremental`, so behaviour is unchanged; they get an assert to narrow the type, matching the existing style in this file.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3960 +/- ##
=======================================
Coverage 97.93% 97.93%
=======================================
Files 57 57
Lines 10936 10940 +4
Branches 2048 2048
=======================================
+ Hits 10710 10714 +4
Misses 125 125
Partials 101 101 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
stefan6419846
requested changes
Aug 13, 2026
Contributor
Author
|
Both trimmed — dropped the backticks since these are plain comments rather |
stefan6419846
approved these changes
Aug 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pulled on another one of the typeguard failures from #3689 and this one
turned out to be a real defect rather than test noise.
PdfWriterProtocol declares
_reader, but PdfWriter only assigns it in theincremental branch of init. So a writer built the normal way doesn't
actually satisfy the protocol it gets passed as — isinstance(writer,
PdfWriterProtocol) is False, and anything doing a runtime protocol check
rejects it. Under
make testtypeit's 62 of the failures, all on that onemissing attribute.
Declared it Optional[PdfReader] defaulting to None, next to the other
attribute docstrings in init. The three places that read it
(write(), _write_increment() twice) are all already inside
if self.incremental, so nothing changes at runtime — they just needed anassert to narrow the Optional, which is the same
assert ..., "mypy"stylealready used elsewhere in this file.
Added a test that fails on main with AttributeError. mypy on pypdf/ reports
the same 22 pre-existing errors before and after, none in these files; the
offline suite goes 1007 → 1008 passed with the only failure being
test_appearance_stream_rtl, which fails the same way on a clean checkout here.