Skip to content

Commit 7bdbdaa

Browse files
saifelsesybrenstuvel
authored andcommitted
Fix hashlib mypy types for Python 3.x
As captured in python/typeshed#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
1 parent 72f8f72 commit 7bdbdaa

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

Diff for: .coveragerc

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
[report]
22
# Regexes for lines to exclude from consideration
33
exclude_lines =
4+
# Re-enable the standard pragma
5+
pragma: no cover
46
# Don't complain if non-runnable code isn't run
57
if __name__ == .__main__.:
8+
# TYPE_CHECKING is False at runtime
9+
if typing.TYPE_CHECKING:

Diff for: rsa/pkcs1.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434

3535
from . import common, transform, core, key
3636

37+
if typing.TYPE_CHECKING:
38+
HashType = hashlib._Hash
39+
else:
40+
HashType = typing.Any
41+
3742
# ASN.1 codes that describe the hash algorithm used.
3843
HASH_ASN1 = {
3944
'MD5': b'\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10',
@@ -44,7 +49,7 @@
4449
'SHA-512': b'\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40',
4550
}
4651

47-
HASH_METHODS = {
52+
HASH_METHODS: typing.Dict[str, typing.Callable[[], HashType]] = {
4853
'MD5': hashlib.md5,
4954
'SHA-1': hashlib.sha1,
5055
'SHA-224': hashlib.sha224,

Diff for: tests/test_mypy.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pathlib
2+
import sys
23
import unittest
34

45
import mypy.api
@@ -9,8 +10,11 @@
910
class MypyRunnerTest(unittest.TestCase):
1011
def test_run_mypy(self):
1112
proj_root = pathlib.Path(__file__).parent.parent
12-
args = ['--incremental', '--ignore-missing-imports'] + [str(proj_root / dirname) for dirname
13-
in test_modules]
13+
args = [
14+
'--incremental',
15+
'--ignore-missing-imports',
16+
f'--python-version={sys.version_info.major}.{sys.version_info.minor}'
17+
] + [str(proj_root / dirname) for dirname in test_modules]
1418

1519
result = mypy.api.run(args)
1620

0 commit comments

Comments
 (0)