Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7bdbdaa

Browse files
saifelsesybrenstuvel
authored andcommitted
Fix hashlib mypy types for Python 3.x
As captured inpython/typeshed#1663, the types forSHA-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 typeinference as noted insybrenstuvel#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 CIinstead of always targeting Python 3.7 (as configured in setup.cfg) tovalidate the types in all supported Python 3.x versions.This fix also avoids the issue with the older mypy releases forPython 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'```Fixessybrenstuvel#153
1 parent72f8f72 commit7bdbdaa

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

‎.coveragerc

Lines changed: 4 additions & 0 deletions
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:

‎rsa/pkcs1.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434

3535
from .importcommon,transform,core,key
3636

37+
iftyping.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,

‎tests/test_mypy.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
importpathlib
2+
importsys
23
importunittest
34

45
importmypy.api
@@ -9,8 +10,11 @@
910
classMypyRunnerTest(unittest.TestCase):
1011
deftest_run_mypy(self):
1112
proj_root=pathlib.Path(__file__).parent.parent
12-
args= ['--incremental','--ignore-missing-imports']+ [str(proj_root/dirname)fordirname
13-
intest_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)fordirnameintest_modules]
1418

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp