2020import time
2121import warnings
2222from pathlib import Path
23- from typing import TYPE_CHECKING , Any , ClassVar , Final , TypeVar , cast
23+ from typing import TYPE_CHECKING , Any , ClassVar , Final , TypeVar , Union , cast
2424
2525if TYPE_CHECKING :
2626 from collections .abc import Sequence
3232logger = logging .getLogger (__name__ )
3333
3434
35- def oqs_python_version () -> str | None :
35+ def oqs_python_version () -> Union [ str , None ] :
3636 """liboqs-python version string."""
3737 try :
3838 result = importlib .metadata .version ("liboqs-python" )
@@ -57,7 +57,7 @@ def _countdown(seconds: int) -> None:
5757
5858def _load_shared_obj (
5959 name : str ,
60- additional_searching_paths : Sequence [Path ] | None = None ,
60+ additional_searching_paths : Union [ Sequence [Path ], None ] = None ,
6161) -> ct .CDLL :
6262 """Attempt to load shared library."""
6363 paths : list [Path ] = []
@@ -99,7 +99,10 @@ def _load_shared_obj(
9999 raise RuntimeError (msg )
100100
101101
102- def _install_liboqs (target_directory : Path , oqs_version_to_install : str | None = None ) -> None :
102+ def _install_liboqs (
103+ target_directory : Path ,
104+ oqs_version_to_install : Union [str , None ] = None ,
105+ ) -> None :
103106 """Install liboqs version oqs_version (if None, installs latest at HEAD) in the target_directory.""" # noqa: E501
104107 with tempfile .TemporaryDirectory () as tmpdirname :
105108 oqs_install_cmd = [
@@ -265,7 +268,7 @@ class KeyEncapsulation(ct.Structure):
265268 ("decaps_cb" , ct .c_void_p ),
266269 ]
267270
268- def __init__ (self , alg_name : str , secret_key : int | bytes | None = None ) -> None :
271+ def __init__ (self , alg_name : str , secret_key : Union [ int , bytes , None ] = None ) -> None :
269272 """
270273 Create new KeyEncapsulation with the given algorithm.
271274
@@ -305,13 +308,13 @@ def __enter__(self: TKeyEncapsulation) -> TKeyEncapsulation:
305308
306309 def __exit__ (
307310 self ,
308- ctx_type : type [BaseException ] | None ,
309- ctx_value : BaseException | None ,
310- ctx_traceback : TracebackType | None ,
311+ ctx_type : Union [ type [BaseException ], None ] ,
312+ ctx_value : Union [ BaseException , None ] ,
313+ ctx_traceback : Union [ TracebackType , None ] ,
311314 ) -> None :
312315 self .free ()
313316
314- def generate_keypair (self ) -> bytes | int :
317+ def generate_keypair (self ) -> Union [ bytes , int ] :
315318 """
316319 Generate a new keypair and returns the public key.
317320
@@ -330,7 +333,7 @@ def export_secret_key(self) -> bytes:
330333 """Export the secret key."""
331334 return bytes (self .secret_key )
332335
333- def encap_secret (self , public_key : int | bytes ) -> tuple [bytes , bytes | int ]:
336+ def encap_secret (self , public_key : Union [ int , bytes ] ) -> tuple [bytes , Union [ bytes , int ] ]:
334337 """
335338 Generate and encapsulates a secret using the provided public key.
336339
@@ -354,15 +357,15 @@ def encap_secret(self, public_key: int | bytes) -> tuple[bytes, bytes | int]:
354357 )
355358
356359 # TODO: What should it return?
357- # 1. tuple[bytes | int, bytes | int]
358- # 2. tuple[bytes, bytes | int]
359- # 3. tuple[bytes, bytes] | int
360+ # 1. tuple[Union[ bytes, int], Union[ bytes, int] ]
361+ # 2. tuple[bytes, Union[ bytes, int] ]
362+ # 3. Union[ tuple[bytes, bytes], int]
360363 return (
361364 bytes (cast (bytes , ciphertext )),
362365 bytes (cast (bytes , shared_secret )) if rv == OQS_SUCCESS else 0 ,
363366 )
364367
365- def decap_secret (self , ciphertext : int | bytes ) -> bytes | int :
368+ def decap_secret (self , ciphertext : Union [ int , bytes ] ) -> Union [ bytes , int ] :
366369 """
367370 Decapsulate the ciphertext and returns the secret.
368371
@@ -451,7 +454,7 @@ class Signature(ct.Structure):
451454 ("verify_cb" , ct .c_void_p ),
452455 ]
453456
454- def __init__ (self , alg_name : str , secret_key : int | bytes | None = None ) -> None :
457+ def __init__ (self , alg_name : str , secret_key : Union [ int , bytes , None ] = None ) -> None :
455458 """
456459 Create new Signature with the given algorithm.
457460
@@ -488,13 +491,13 @@ def __enter__(self: TSignature) -> TSignature:
488491
489492 def __exit__ (
490493 self ,
491- ctx_type : type [BaseException ] | None ,
492- ctx_value : BaseException | None ,
493- ctx_traceback : TracebackType | None ,
494+ ctx_type : Union [ type [BaseException ], None ] ,
495+ ctx_value : Union [ BaseException , None ] ,
496+ ctx_traceback : Union [ TracebackType , None ] ,
494497 ) -> None :
495498 self .free ()
496499
497- def generate_keypair (self ) -> bytes | int :
500+ def generate_keypair (self ) -> Union [ bytes , int ] :
498501 """
499502 Generate a new keypair and returns the public key.
500503
@@ -515,7 +518,7 @@ def export_secret_key(self) -> bytes:
515518 """Export the secret key."""
516519 return bytes (self .secret_key )
517520
518- def sign (self , message : bytes ) -> bytes | int :
521+ def sign (self , message : bytes ) -> Union [ bytes , int ] :
519522 """
520523 Signs the provided message and returns the signature.
521524
0 commit comments