Skip to content

Commit

Permalink
MAINT: Type annotations for all functions/methods (#854)
Browse files Browse the repository at this point in the history
This PR uses 4 ways to disable checks by mypy:

*  74x `typing.cast`: Has no effect on the running code, just on mypy
* 161x `typing.Any`: If we tell mypy that a parameter / variable can have any possible type, mypy cannot check anything.
*  82x `# type: ignore`
*  36x `assert foo is not None`: This can have an effect in running code and should only be used if mypy fails to understand that foo is guaranteed not to be None

Those are especially necessary for `.getObject()` calls. If possible, they
should be removed.

`cast` is preferrable over `# type: ignore` as it gives more information. `assert` should only be used in rare cases.

Uncertainties:

* `ByteStringObject` vs `TextStringObject`, e.g. for `_authenticateUserPassword` and the functions in `_security`
* `ContentStream`: I have no clue what type the stream argument should have

Details:

* utils.ConvertFunctionsToVirtualList -> _VirtualList 
* Typeguard was used to verify that the annotated types are correct. There are
  still some cases where Typeguard fails, but the reason might be that
  Typeguard does not consider inheritance.
* BUG: Potential None attribute access in _reader.getDestinationPageNumber
* BUG: _security._alg33_1 : Adding str and bytes argument
* STY: Fix typo 'test_get_destination_age_number'
* BUG: Field.additionalActions was missing a return statement
  • Loading branch information
MartinThoma authored May 8, 2022
1 parent e48bc6d commit 50e9079
Show file tree
Hide file tree
Showing 29 changed files with 1,483 additions and 909 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
if: matrix.python-version == '3.10.1'
- name: Test with mypy
run : |
mypy PyPDF2 --show-error-codes
mypy PyPDF2 --show-error-codes --disallow-untyped-defs --disallow-incomplete-defs
- name: Upload coverage data
uses: actions/upload-artifact@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ Deprecations (DEP):
- PyPDF2 2.0 requires Python 3.6+. Python 2.7 and 3.5 support were dropped.
- PdfFileReader and PdfFileMerger no longer have the `overwriteWarnings`
parameter. The new behavior is `overwriteWarnings=False`.
- PdfFileReader: The "warndest" parameter was removed
- merger: OutlinesObject was removed.
- utils:
* `ConvertFunctionsToVirtualList` was removed
* `formatWarning` was removed
* `isInt(obj)`: Use `instance(obj, int)` instead
* `u_(s)`: Use `s` directly
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ clean:
test:
pytest tests --cov --cov-report term-missing -vv --cov-report html --durations=3 --timeout=30

testtype:
pytest tests --cov --cov-report term-missing -vv --cov-report html --durations=3 --timeout=30 --typeguard-packages=PyPDF2

mutation-test:
mutmut run

Expand Down
Loading

0 comments on commit 50e9079

Please sign in to comment.