Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beautify code #97

Merged
merged 11 commits into from
Feb 2, 2025
Prev Previous commit
Next Next commit
Replace pipe operator to Union, to support py3.9
Bump deps

Signed-off-by: andrew000 <[email protected]>
andrew000 committed Dec 9, 2024
commit 1c4f1556c0542ed1b03ff667b3f19a5508dbf729
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ repos:
- id: "check-json"

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.7.3
rev: v0.8.2
hooks:
- id: ruff
args: [ "--fix" ]
43 changes: 23 additions & 20 deletions oqs/oqs.py
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
import time
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar, Final, TypeVar, cast
from typing import TYPE_CHECKING, Any, ClassVar, Final, TypeVar, Union, cast

if TYPE_CHECKING:
from collections.abc import Sequence
@@ -32,7 +32,7 @@
logger = logging.getLogger(__name__)


def oqs_python_version() -> str | None:
def oqs_python_version() -> Union[str, None]:
"""liboqs-python version string."""
try:
result = importlib.metadata.version("liboqs-python")
@@ -57,7 +57,7 @@ def _countdown(seconds: int) -> None:

def _load_shared_obj(
name: str,
additional_searching_paths: Sequence[Path] | None = None,
additional_searching_paths: Union[Sequence[Path], None] = None,
) -> ct.CDLL:
"""Attempt to load shared library."""
paths: list[Path] = []
@@ -99,7 +99,10 @@ def _load_shared_obj(
raise RuntimeError(msg)


def _install_liboqs(target_directory: Path, oqs_version_to_install: str | None = None) -> None:
def _install_liboqs(
target_directory: Path,
oqs_version_to_install: Union[str, None] = None,
) -> None:
"""Install liboqs version oqs_version (if None, installs latest at HEAD) in the target_directory.""" # noqa: E501
with tempfile.TemporaryDirectory() as tmpdirname:
oqs_install_cmd = [
@@ -265,7 +268,7 @@ class KeyEncapsulation(ct.Structure):
("decaps_cb", ct.c_void_p),
]

def __init__(self, alg_name: str, secret_key: int | bytes | None = None) -> None:
def __init__(self, alg_name: str, secret_key: Union[int, bytes, None] = None) -> None:
"""
Create new KeyEncapsulation with the given algorithm.

@@ -305,13 +308,13 @@ def __enter__(self: TKeyEncapsulation) -> TKeyEncapsulation:

def __exit__(
self,
ctx_type: type[BaseException] | None,
ctx_value: BaseException | None,
ctx_traceback: TracebackType | None,
ctx_type: Union[type[BaseException], None],
ctx_value: Union[BaseException, None],
ctx_traceback: Union[TracebackType, None],
) -> None:
self.free()

def generate_keypair(self) -> bytes | int:
def generate_keypair(self) -> Union[bytes, int]:
"""
Generate a new keypair and returns the public key.

@@ -330,7 +333,7 @@ def export_secret_key(self) -> bytes:
"""Export the secret key."""
return bytes(self.secret_key)

def encap_secret(self, public_key: int | bytes) -> tuple[bytes, bytes | int]:
def encap_secret(self, public_key: Union[int, bytes]) -> tuple[bytes, Union[bytes, int]]:
"""
Generate and encapsulates a secret using the provided public key.

@@ -354,15 +357,15 @@ def encap_secret(self, public_key: int | bytes) -> tuple[bytes, bytes | int]:
)

# TODO: What should it return?
# 1. tuple[bytes | int, bytes | int]
# 2. tuple[bytes, bytes | int]
# 3. tuple[bytes, bytes] | int
# 1. tuple[Union[bytes, int], Union[bytes, int]]
# 2. tuple[bytes, Union[bytes, int]]
# 3. Union[tuple[bytes, bytes], int]
return (
bytes(cast(bytes, ciphertext)),
bytes(cast(bytes, shared_secret)) if rv == OQS_SUCCESS else 0,
)

def decap_secret(self, ciphertext: int | bytes) -> bytes | int:
def decap_secret(self, ciphertext: Union[int, bytes]) -> Union[bytes, int]:
"""
Decapsulate the ciphertext and returns the secret.

@@ -451,7 +454,7 @@ class Signature(ct.Structure):
("verify_cb", ct.c_void_p),
]

def __init__(self, alg_name: str, secret_key: int | bytes | None = None) -> None:
def __init__(self, alg_name: str, secret_key: Union[int, bytes, None] = None) -> None:
"""
Create new Signature with the given algorithm.

@@ -488,13 +491,13 @@ def __enter__(self: TSignature) -> TSignature:

def __exit__(
self,
ctx_type: type[BaseException] | None,
ctx_value: BaseException | None,
ctx_traceback: TracebackType | None,
ctx_type: Union[type[BaseException], None],
ctx_value: Union[BaseException, None],
ctx_traceback: Union[TracebackType, None],
) -> None:
self.free()

def generate_keypair(self) -> bytes | int:
def generate_keypair(self) -> Union[bytes, int]:
"""
Generate a new keypair and returns the public key.

@@ -515,7 +518,7 @@ def export_secret_key(self) -> bytes:
"""Export the secret key."""
return bytes(self.secret_key)

def sign(self, message: bytes) -> bytes | int:
def sign(self, message: bytes) -> Union[bytes, int]:
"""
Signs the provided message and returns the signature.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -17,8 +17,8 @@ package = true
dev = [
"isort==5.13.2",
"pre-commit==4.0.1",
"ruff==0.7.3",
"bandit==1.7.10",
"ruff==0.8.2",
"bandit==1.8.0",
"nose2==0.15.1",
]
lint = [
@@ -81,7 +81,8 @@ ignore = [
"PLR0911", "PLR0912", "PLR0913", "PLR0915", "PLR5501",
"PLW0120",
"RUF001",
"TD002", "TD003"
"TD002", "TD003",
"U007",
]

[tool.ruff.format]