Skip to content

Commit dd98e31

Browse files
lovetoxmhils
andauthored
Add inline type annotations (#1089)
* crypto: Add type annotations * Don’t redefine var mypy complains about the redefinition * _util: Add type annotations * rand: Add type annotations * Prepare package & CI for running mypy * fix toxenv name Co-authored-by: Maximilian Hils <[email protected]>
1 parent 45ebb73 commit dd98e31

File tree

7 files changed

+319
-203
lines changed

7 files changed

+319
-203
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
# Meta
4343
- {VERSION: "3.9", TOXENV: "check-manifest"}
4444
- {VERSION: "3.9", TOXENV: "flake8"}
45+
- {VERSION: "3.6", TOXENV: "py36-mypy"}
4546
- {VERSION: "3.9", TOXENV: "docs"}
4647
name: "${{ matrix.PYTHON.TOXENV }}"
4748
steps:

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
include LICENSE MANIFEST.in *.rst tox.ini .coveragerc
2-
exclude codecov.yml .readthedocs.yml
2+
exclude codecov.yml .readthedocs.yml mypy.ini
33
recursive-include tests *.py
44
recursive-include doc *
55
prune doc/_build

mypy.ini

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[mypy]
2+
warn_unused_configs = True
3+
follow_imports = skip
4+
strict = True
5+
6+
exclude = (?x)(
7+
SSL\.py$
8+
)
9+
10+
[mypy-OpenSSL.crypto]
11+
warn_return_any = False
12+
disallow_any_expr = False
13+
14+
[mypy-OpenSSL.rand]
15+
warn_return_any = False
16+
17+
[mypy-OpenSSL._util]
18+
warn_return_any = False
19+
20+
[mypy-cryptography.*]
21+
ignore_missing_imports = True
22+
23+
[mypy-cffi.*]
24+
ignore_missing_imports = True

src/OpenSSL/_util.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22
import sys
33
import warnings
4+
from typing import Any, Callable, NoReturn, Type, Union
45

56
from cryptography.hazmat.bindings.openssl.binding import Binding
67

8+
StrOrBytesPath = Union[str, bytes, os.PathLike]
79

810
binding = Binding()
911
ffi = binding.ffi
@@ -16,7 +18,7 @@
1618
no_zero_allocator = ffi.new_allocator(should_clear_after_alloc=False)
1719

1820

19-
def text(charp):
21+
def text(charp: Any) -> str:
2022
"""
2123
Get a native string type representing of the given CFFI ``char*`` object.
2224
@@ -29,7 +31,7 @@ def text(charp):
2931
return ffi.string(charp).decode("utf-8")
3032

3133

32-
def exception_from_error_queue(exception_type):
34+
def exception_from_error_queue(exception_type: Type[Exception]) -> NoReturn:
3335
"""
3436
Convert an OpenSSL library failure into a Python exception.
3537
@@ -55,13 +57,13 @@ def exception_from_error_queue(exception_type):
5557
raise exception_type(errors)
5658

5759

58-
def make_assert(error):
60+
def make_assert(error: Type[Exception]) -> Callable[[bool], Any]:
5961
"""
6062
Create an assert function that uses :func:`exception_from_error_queue` to
6163
raise an exception wrapped by *error*.
6264
"""
6365

64-
def openssl_assert(ok):
66+
def openssl_assert(ok: bool) -> None:
6567
"""
6668
If *ok* is not True, retrieve the error from OpenSSL and raise it.
6769
"""
@@ -71,7 +73,7 @@ def openssl_assert(ok):
7173
return openssl_assert
7274

7375

74-
def path_bytes(s):
76+
def path_bytes(s: StrOrBytesPath) -> bytes:
7577
"""
7678
Convert a Python path to a :py:class:`bytes` for the path which can be
7779
passed into an OpenSSL API accepting a filename.
@@ -88,7 +90,7 @@ def path_bytes(s):
8890
return b
8991

9092

91-
def byte_string(s):
93+
def byte_string(s: str) -> bytes:
9294
return s.encode("charmap")
9395

9496

@@ -99,7 +101,7 @@ def byte_string(s):
99101
_TEXT_WARNING = "str for {0} is no longer accepted, use bytes"
100102

101103

102-
def text_to_bytes_and_warn(label, obj):
104+
def text_to_bytes_and_warn(label: str, obj: Any) -> Any:
103105
"""
104106
If ``obj`` is text, emit a warning that it should be bytes instead and try
105107
to convert it to bytes automatically.

0 commit comments

Comments
 (0)