11import os
22import sys
33import warnings
4+ from typing import Any , Callable , NoReturn , Type , Union
45
56from cryptography .hazmat .bindings .openssl .binding import Binding
67
8+ StrOrBytesPath = Union [str , bytes , os .PathLike ]
79
810binding = Binding ()
911ffi = binding .ffi
1618no_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