diff options
author | lovetox <philipp@hoerist.com> | 2022-05-20 14:44:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 14:44:59 +0200 |
commit | dd98e31ad6f9e33f351d47246d6d09c804ba8c29 (patch) | |
tree | b356ff30f5d8d45ef0023f6985059575522364ca /src/OpenSSL/_util.py | |
parent | 45ebb73416a67cb87b7ca0bfcfe7902b4f38250a (diff) | |
download | pyopenssl-dd98e31ad6f9e33f351d47246d6d09c804ba8c29.tar.gz |
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 <github@maximilianhils.com>
Diffstat (limited to 'src/OpenSSL/_util.py')
-rw-r--r-- | src/OpenSSL/_util.py | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/OpenSSL/_util.py b/src/OpenSSL/_util.py index a3d5075..7a102e6 100644 --- a/src/OpenSSL/_util.py +++ b/src/OpenSSL/_util.py @@ -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 @@ -16,7 +18,7 @@ lib = binding.lib 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. @@ -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. @@ -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. """ @@ -71,7 +73,7 @@ def make_assert(error): 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. @@ -88,7 +90,7 @@ def path_bytes(s): return b -def byte_string(s): +def byte_string(s: str) -> bytes: return s.encode("charmap") @@ -99,7 +101,7 @@ UNSPECIFIED = object() _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. |