DEV: Align mypy Makefile target with strict mode#3690
DEV: Align mypy Makefile target with strict mode#3690stefan6419846 merged 8 commits intopy-pdf:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3690 +/- ##
==========================================
+ Coverage 97.48% 97.59% +0.10%
==========================================
Files 55 55
Lines 10211 10218 +7
Branches 1875 1875
==========================================
+ Hits 9954 9972 +18
+ Misses 150 141 -9
+ Partials 107 105 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
stefan6419846
left a comment
There was a problem hiding this comment.
Thanks for the PR. I have left some inline comments where I think that the chosen fix is not appropriate or could be improved.
8497bf5 to
a087db1
Compare
|
Addressed all the review feedback — here's a summary of the two items I kept as-is:
|
0968a91 to
9f3bdb0
Compare
8968012 to
cfcd9aa
Compare
cfcd9aa to
95e051a
Compare
|
Thanks for the thorough review! I've rebased on main and reworked this PR to address all feedback. Here's a summary of changes: Rebased on main (5 commits behind) — resolved the merge conflict in pyproject.toml and dropped the accidental _writer.py reverts. The compress_identical_objects() deprecation refactor is now preserved as intended. pyproject.toml — kept Crypto providers — reverted the typed locals approach. Instead, added Addressing specific comments:
Bug fix note: the Public API types preserved: |
|
Follow-up: pushed additional commits to fix strict mypy errors surfaced in CI. Key finding: [[tool.mypy.overrides]]
module = "pypdf._crypt_providers._pycryptodome"
warn_return_any = falseAlso added Only |
This is wrong - the latest wheel contains corresponding stub files besides the regular files. What are the messages you receive when enabling it in the pre-commit configuration? Currently, the trove classifier is missing, which I have created a corresponding PR for, although the docs suggest that this should not be required: Legrandin/pycryptodome#909 |
08c851e to
00b7fa3
Compare
stefan6419846
left a comment
There was a problem hiding this comment.
Besides the new remarks, could you please have a look at the merge conflict?
Replace individual mypy flags with `strict = true` in pyproject.toml and fix all resulting type errors across the codebase. Changes: - pyproject.toml: use `strict = true`, keep test overrides from py-pdf#3660 - .pre-commit-config.yaml: add cryptography and pycryptodome as additional_dependencies for mypy hook - Makefile: simplify mypy target (config now in pyproject.toml) - Fix type annotations in ~20 source files - Fix latent bug: `data[-1] != b"\n"` compared int to bytes (always True), changed to `data[-1:] != b"\n"` for correct bytes comparison - Add tests for AnnotationDictionary.flags, Destination defaults, ArrayObject._to_lst
- Add mypy override for pycryptodome (no type stubs available) - Add type: ignore for comparison-overlap in _writer.py and test_files.py - Add type: ignore for attr-defined in test_image_xobject.py (PIL) - Remove pycryptodome from pre-commit additional_dependencies (no stubs)
pycryptodome ships .pyi stubs and a py.typed marker, so adding it to additional_dependencies lets mypy resolve its types directly. The warn_return_any override for the pycryptodome provider is no longer needed.
The direct `mypy .` step in CI installs from requirements/ci-3.11.txt which was missing pycryptodome. Without it installed, mypy treats Crypto module types as Any, causing no-any-return errors in _pycryptodome.py. pycryptodome 3.23.0 ships proper .pyi stubs and a py.typed marker, so installing it lets mypy resolve types directly.
5a92c01 to
b01f488
Compare
|
Thanks for the follow-up. Pushed a commit addressing three of the four April 16 remarks plus the rebase:
For On the older threads:
|
In theory, we lately guarded the code enough to let
If I remember correctly, you exchanged |
stefan6419846
left a comment
There was a problem hiding this comment.
Thanks for your patience. For now, I do not see any obvious issues any more. If you think that future cleanups make sense, consider opening dedicated PRs.
Summary
Moves the
--strict,--ignore-missing-imports, and--check-untyped-defsflags from the Makefile intopyproject.toml[tool.mypy], so thatmake mypyand directmypy pypdfinvocations use the same configuration.Fixes all 55 strict-mode violations across 18 source files:
no-any-return: addedcast()for return values originating from untyped libraries (pycryptodome), dict lookups, and stream readscomparison-overlap: fixed a latent bytes-indexing bug (data[-1]returnsint, notbytes— changed todata[-1:]) and addedtype: ignorewhere runtime comparisons are intentional (FloatObject == 0, PdfObject == string)assignment/index: usedbytearrayfor mutable byte buffers infilters.pyThe
data[-1]→data[-1:]fix corrects a real bug: comparinginttobyteswas alwaysTrue, causing incorrect newline detection in array-based content streams. The test assertion for output byte count was updated accordingly.Test plan
mypy pypdfreports 0 errors with strict modemake mypyworks with simplified command (justmypy pypdf)test_content_stream__array_based__output_lengthassertion to match corrected byte countCloses #3688