summaryrefslogtreecommitdiff
path: root/rsa
diff options
context:
space:
mode:
authorSaif Hakim <saif@benchling.com>2021-02-16 01:40:51 -0800
committerSybren A. Stüvel <sybren@stuvel.eu>2021-03-24 18:20:29 +0100
commit7bdbdaa16ee644d00845b94eaa3008061dc5ec79 (patch)
treeb9f4ffcb009e1f986d7745920e56bd6d0a40af85 /rsa
parent72f8f7214909006c39a2c98c6a65538601b0dd3d (diff)
downloadrsa-git-7bdbdaa16ee644d00845b94eaa3008061dc5ec79.tar.gz
Fix hashlib mypy types for Python 3.x
As captured in https://github.com/python/typeshed/pull/1663, the types for SHA-1 and SHA-2 family of functions are callables that return a Hash instance, whilst the SHA-3 family of functions are Hash `type`s (at least in Python 3.6). Mixing the two kinds of functions together in a dictionary confuses mypy's type inference as noted in #153, so we instead add an annotation as a hint. Also, update test_my.py to match the python version set by tox.ini in CI instead of always targeting Python 3.7 (as configured in setup.cfg) to validate the types in all supported Python 3.x versions. This fix also avoids the issue with the older mypy releases for Python 3.6 / Python 3.7 found in distro repos... ... for Ubuntu: ``` docker run \ -v $(pwd):/tmp/rsa \ -w /tmp/rsa ubuntu:18.04 \ /bin/bash -c 'apt-get update -qqy \ && apt-get install -qqy python3-pyasn1 python3-setuptools python3-mypy \ && python3 setup.py test' ``` ... and for Fedora: ``` docker run \ -v $(pwd):/tmp/rsa \ -w /tmp/rsa docker.io/fedora \ /bin/bash -c 'dnf -y install wget python3-devel python3-pyasn1 python3-setuptools python3-mypy \ && python3 setup.py test' ``` Fixes #153
Diffstat (limited to 'rsa')
-rw-r--r--rsa/pkcs1.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/rsa/pkcs1.py b/rsa/pkcs1.py
index 07cf85b..9adad90 100644
--- a/rsa/pkcs1.py
+++ b/rsa/pkcs1.py
@@ -34,6 +34,11 @@ from hmac import compare_digest
from . import common, transform, core, key
+if typing.TYPE_CHECKING:
+ HashType = hashlib._Hash
+else:
+ HashType = typing.Any
+
# ASN.1 codes that describe the hash algorithm used.
HASH_ASN1 = {
'MD5': b'\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10',
@@ -44,7 +49,7 @@ HASH_ASN1 = {
'SHA-512': b'\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40',
}
-HASH_METHODS = {
+HASH_METHODS: typing.Dict[str, typing.Callable[[], HashType]] = {
'MD5': hashlib.md5,
'SHA-1': hashlib.sha1,
'SHA-224': hashlib.sha224,