Skip to content

Commit

Permalink
Add inline type annotations (#1089)
Browse files Browse the repository at this point in the history
* 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 <github@maximilianhils.com>
  • Loading branch information
lovetox and mhils committed May 20, 2022
1 parent 45ebb73 commit dd98e31
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 203 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
# Meta
- {VERSION: "3.9", TOXENV: "check-manifest"}
- {VERSION: "3.9", TOXENV: "flake8"}
- {VERSION: "3.6", TOXENV: "py36-mypy"}
- {VERSION: "3.9", TOXENV: "docs"}
name: "${{ matrix.PYTHON.TOXENV }}"
steps:
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include LICENSE MANIFEST.in *.rst tox.ini .coveragerc
exclude codecov.yml .readthedocs.yml
exclude codecov.yml .readthedocs.yml mypy.ini
recursive-include tests *.py
recursive-include doc *
prune doc/_build
24 changes: 24 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[mypy]
warn_unused_configs = True
follow_imports = skip
strict = True

exclude = (?x)(
SSL\.py$
)

[mypy-OpenSSL.crypto]
warn_return_any = False
disallow_any_expr = False

[mypy-OpenSSL.rand]
warn_return_any = False

[mypy-OpenSSL._util]
warn_return_any = False

[mypy-cryptography.*]
ignore_missing_imports = True

[mypy-cffi.*]
ignore_missing_imports = True
16 changes: 9 additions & 7 deletions src/OpenSSL/_util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
import sys
import warnings
from typing import Any, Callable, NoReturn, Type, Union

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

StrOrBytesPath = Union[str, bytes, os.PathLike]

binding = Binding()
ffi = binding.ffi
Expand All @@ -16,7 +18,7 @@
no_zero_allocator = ffi.new_allocator(should_clear_after_alloc=False)


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


def exception_from_error_queue(exception_type):
def exception_from_error_queue(exception_type: Type[Exception]) -> NoReturn:
"""
Convert an OpenSSL library failure into a Python exception.
Expand All @@ -55,13 +57,13 @@ def exception_from_error_queue(exception_type):
raise exception_type(errors)


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

def openssl_assert(ok):
def openssl_assert(ok: bool) -> None:
"""
If *ok* is not True, retrieve the error from OpenSSL and raise it.
"""
Expand All @@ -71,7 +73,7 @@ def openssl_assert(ok):
return openssl_assert


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


def byte_string(s):
def byte_string(s: str) -> bytes:
return s.encode("charmap")


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


def text_to_bytes_and_warn(label, obj):
def text_to_bytes_and_warn(label: str, obj: Any) -> Any:
"""
If ``obj`` is text, emit a warning that it should be bytes instead and try
to convert it to bytes automatically.
Expand Down
Loading

0 comments on commit dd98e31

Please sign in to comment.