BUG: Type _id_translated to match what it holds - #3970
Merged
stefan6419846 merged 2 commits intoAug 19, 2026
Conversation
`_id_translated` is declared `dict[int, dict[int, int]]`, but each inner mapping also stores the source document under a `"PreventGC"` key so it is not garbage collected while the translation table is live. The two writes needed `# type: ignore[index]` to get past that, and a runtime protocol check on `PdfWriterProtocol` fails on the string key. Widen the declaration to `dict[int, dict[Union[int, str], Any]]`, which is what the dictionary actually contains, and drop the two ignores that are then unnecessary. Every reader looks entries up by idnum rather than iterating, so behaviour is unchanged. Under `make testtype` this accounts for 55 failures across test_generic and test_writer; they go to 0. mypy reports the same 11 pre-existing errors before and after, none in these files.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3970 +/- ##
=======================================
Coverage 97.94% 97.94%
=======================================
Files 57 57
Lines 11031 11031
Branches 2065 2065
=======================================
Hits 10804 10804
Misses 126 126
Partials 101 101 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
It is the only string key the mapping ever holds, so the narrower type is the accurate one.
Contributor
Author
|
Fair point - "PreventGC" is the only string key that ever goes in there, so |
stefan6419846
approved these changes
Aug 19, 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.
_id_translatedis declareddict[int, dict[int, int]], but each innermapping also stores the source document under a "PreventGC" key so it is
not garbage collected while the translation table is live. The two writes
needed
# type: ignore[index]to get past that, and a runtime protocolcheck on PdfWriterProtocol fails on the string key.
Widened to
dict[int, dict[Union[int, str], Any]], which is what thedictionary actually contains, and dropped the two ignores that became
unnecessary. Every reader looks entries up by idnum rather than iterating,
so behaviour is unchanged.
Under
make testtypethis accounts for 55 failures across test_generic andtest_writer; they go to 0. mypy reports the same 11 pre-existing errors
before and after, none in these files.