[Rpm-maint] [rpm-software-management/rpm] Add type hints to the Python interface (#1615)
Jerry James
notifications at github.com
Thu Apr 1 03:19:33 UTC 2021
I am working on a project that consumes the rpm python interface (https://pagure.io/depict for the curious). I would like to typecheck my project, so I have created some stubs for that purpose. Are you interested in having the rpm stubs?
Note that, with this commit applied, mypy has several complaints:
```
rpm/_rpm.pyi:583: error: Invalid signature "def [AnyStr in (builtins.str, builtins.bytes)] (rpm._rpm.hdr, Union[rpm._rpm.rpmTag, builtins.bytes]) -> Union[builtins.int, builtins.str*]" for "__getattr__"
rpm/_rpm.pyi:583: error: Invalid signature "def [AnyStr in (builtins.str, builtins.bytes)] (rpm._rpm.hdr, Union[rpm._rpm.rpmTag, builtins.bytes]) -> Union[builtins.int, builtins.bytes*]" for "__getattr__"
rpm/transaction.py:74: error: Signature of "addInstall" incompatible with supertype "ts"
rpm/transaction.py:87: error: Return type "None" of "addReinstall" incompatible with return type "bool" in supertype "ts"
rpm/transaction.py:93: error: Return type "None" of "addErase" incompatible with return type "bool" in supertype "ts"
rpm/transaction.py:121: error: Signature of "run" incompatible with supertype "ts"
rpm/transaction.py:137: error: Signature of "check" incompatible with supertype "ts"
rpm/transaction.py:179: error: Return type "None" of "hdrCheck" incompatible with return type "Tuple[rpmRC, Optional[str]]" in supertype "ts"
rpm/transaction.py:189: error: Return type "hdr" of "hdrFromFdno" incompatible with return type "Tuple[rpmRC, hdr]" in supertype "ts"
rpm/__init__.py:53: error: Cannot assign to a type
Found 10 errors in 3 files (checked 3 source files)
```
The first two errors arise from mypy having magic knowledge of what a `__getattr__` function's signature should look like. I think the only way to eliminate those errors is to remove the `__getattr__` declarations and require any code that actually takes advantage of the non-str interface to work around mypy.
The remaining 8 errors can be fixed, but would require backwards-incompatible changes to rpm's python interface, mostly of the form "don't have a method in a subclass with the same name but a different signature as a function in a superclass".
I had to comment out the `file.class` attribute, as mypy objects to an attribute named "class".
Since `archive.hascontent` and `keyring.addKey` both return one of two integers (one zero and one nonzero), I wonder if it might not be better to make those functions return a boolean, although that would also be a backwards-incompatible change.
In `TransactionSet.check`, I made a change to check whether `p.altNEVR` is `None`. I could not rule out that possibility by reading through the sources.
Let me know if this interests you. I am willing to keep working on it until you are satisfied.
You can view, comment on, or merge this pull request online at:
https://github.com/rpm-software-management/rpm/pull/1615
-- Commit Summary --
* Add type hints to the Python interface
-- File Changes --
M python/Makefile.am (4)
M python/rpm/__init__.py (29)
A python/rpm/_rpm.pyi (863)
M python/rpm/transaction.py (75)
-- Patch Links --
https://github.com/rpm-software-management/rpm/pull/1615.patch
https://github.com/rpm-software-management/rpm/pull/1615.diff
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/rpm-software-management/rpm/pull/1615
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rpm.org/pipermail/rpm-maint/attachments/20210331/ede3b45b/attachment-0001.html>
More information about the Rpm-maint
mailing list