Skip to content

Commit

Permalink
style: add some type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Aug 19, 2024
1 parent 97f2133 commit c4a9b03
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tests/encryption_algorithms/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@


@pytest.mark.parametrize("in_signature_params", utils.SOME_SIGNATURE_PARAMS)
def test_digest_size(in_signature_params):
def test_digest_size(in_signature_params: dict[str, str]) -> None:
some_hash = eac.sign_bytes(b"some_msg", b"some_key", in_signature_params)
assert eac.digest_size(in_signature_params) == len(some_hash)
8 changes: 7 additions & 1 deletion tests/encryption_algorithms/test_encryption_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
import pytest

from .. import utils
Expand All @@ -6,7 +7,12 @@
@pytest.mark.parametrize("in_bytes", utils.BYTES_LIST)
@pytest.mark.parametrize("in_pass", utils.BYTES_LIST)
@pytest.mark.parametrize(("encrypt", "decrypt"), utils.ENCRYPT_DECRYPT_PAIRS)
def test_encryption_decryption(in_bytes, in_pass, encrypt, decrypt):
def test_encryption_decryption(
in_bytes: bytes,
in_pass: bytes,
encrypt: typing.Callable[[bytes, bytes], bytes],
decrypt: typing.Callable[[bytes, bytes], bytes | None],
) -> None:
encrypted = encrypt(in_bytes, in_pass)

assert encrypted != in_bytes
Expand Down
10 changes: 5 additions & 5 deletions tests/test_bytes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@
(2 ** (8 * 255), 256),
],
)
def test_byte_length(in_value, expected):
def test_byte_length(in_value: int, expected: int) -> None:
assert bu.byte_length(in_value) == expected


def test_byte_length_raises_for_negative_input():
def test_byte_length_raises_for_negative_input() -> None:
with pytest.raises(ValueError, match="in_value must be non-negative"):
bu.byte_length(-1)


@pytest.mark.parametrize(
"in_value", [0, 1, 2, 3, 255, 256, 257, 3239949409384, 10**570, 2 ** (8 * 255) - 1]
)
def test_int_to_bytes(in_value):
def test_int_to_bytes(in_value: int) -> None:
assert bu.bytes_to_int(bu.int_to_bytes(in_value)) == in_value


def test_int_to_bytes_raises_when_input_is_too_big():
def test_int_to_bytes_raises_when_input_is_too_big() -> None:
with pytest.raises(ValueError, match="in_value must be 255 bytes or less"):
bu.int_to_bytes(2 ** (8 * 255))


@pytest.mark.parametrize(
("in_str", "in_bytes"), itertools.product(utils.STRS, utils.BYTES_LIST)
)
def test_split_and_join(in_str, in_bytes):
def test_split_and_join(in_str: str, in_bytes: bytes) -> None:
res_str, res_bytes = bu.split(bu.join(in_str, in_bytes))
assert res_str == in_str
assert res_bytes == in_bytes
2 changes: 1 addition & 1 deletion tests/test_bytestr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"in_bytes",
utils.BYTES_LIST,
)
def test_bytes_to_bytestr(in_bytes):
def test_bytes_to_bytestr(in_bytes: bytes) -> None:
bytestr = bu.bytes_to_bytestr(in_bytes)
assert isinstance(bytestr, str)
assert bu.bytestr_to_bytes(bytestr) == in_bytes
2 changes: 1 addition & 1 deletion tests/test_configurators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@


@pytest.mark.parametrize("configurator", [simple.Simple, spiced.Spiced])
def test_configurators_raise_when_invalid_args(configurator):
def test_configurators_raise_when_invalid_args(configurator) -> None:
with pytest.raises(TypeError):
configurator(wrong_param=10)
15 changes: 10 additions & 5 deletions tests/test_create_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


@pytest.fixture(name="puzzle_tc")
def fixture_puzzle_tc():
def fixture_puzzle_tc() -> _PuzzleTestCase:
qa_list = [
"Question 1?",
"Answer 1",
Expand Down Expand Up @@ -157,7 +157,12 @@ def _input_simulator() -> str:


@pytest.mark.parametrize(("encrypt", "decrypt"), utils.ENCRYPT_DECRYPT_PAIRS)
def test_run_puzzle_all_good_answers(capsys, puzzle_tc, encrypt, decrypt) -> None:
def test_run_puzzle_all_good_answers(
capsys,
puzzle_tc,
encrypt: typing.Callable[[bytes, bytes], bytes],
decrypt: typing.Callable[[bytes, bytes], bytes | None],
) -> None:
encrypted_puzzle = pde.encrypt_data(
cp.question_answer_list_to_dict(puzzle_tc.qa_list), encrypt
)
Expand Down Expand Up @@ -224,7 +229,7 @@ def test_run_puzzle_wrong_answers(capsys, puzzle_tc, encrypt, decrypt) -> None:
),
],
)
def test_question_answer_list_to_dict(qa_list, expected):
def test_question_answer_list_to_dict(qa_list: list[str], expected) -> None:
assert cp.question_answer_list_to_dict(qa_list) == expected


Expand All @@ -237,8 +242,8 @@ def test_question_answer_list_to_dict(qa_list, expected):
],
)
def test_question_answer_list_to_dict_raises_when_input_list_has_even_length(
wrong_qa_list,
):
wrong_qa_list: list[str],
) -> None:
with pytest.raises(
ValueError, match="The question/answer list must have odd length."
):
Expand Down
23 changes: 21 additions & 2 deletions tests/test_puzzle_data_encryption.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import typing
import pytest

import puzzle_generator.puzzle_data_encryption as pde
from . import utils


def _decrypt_data(
puzzle_bytes: bytes,
cur_pass: str,
decrypt: typing.Callable[[bytes, bytes], bytes | None],
) -> tuple[str, bytes]:
res = pde.decrypt_data(
puzzle_bytes,
cur_pass,
decrypt,
)
assert res is not None
return res


@pytest.mark.parametrize(
"in_puzzle",
[
Expand Down Expand Up @@ -47,7 +62,11 @@
],
)
@pytest.mark.parametrize(("encrypt", "decrypt"), utils.ENCRYPT_DECRYPT_PAIRS)
def test_pde(in_puzzle, encrypt, decrypt):
def test_pde(
in_puzzle,
encrypt: typing.Callable[[bytes, bytes], bytes],
decrypt: typing.Callable[[bytes, bytes], bytes | None],
) -> None:
encrypted_puzzle = pde.encrypt_data(in_puzzle, encrypt)
tmp_puzzle_data = in_puzzle
while len(encrypted_puzzle[1]) > 0:
Expand All @@ -61,7 +80,7 @@ def test_pde(in_puzzle, encrypt, decrypt):
)
is None
)
encrypted_puzzle = pde.decrypt_data(
encrypted_puzzle = _decrypt_data(
encrypted_puzzle[1],
cur_pass,
decrypt,
Expand Down
15 changes: 13 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import string
import itertools
import typing

import puzzle_generator.encryption_algorithms.simple.simple as se
import puzzle_generator.encryption_algorithms.simple.spiced as sse
Expand Down Expand Up @@ -40,11 +41,21 @@
SOME_SIGNATURE_PARAMS = [{"digest": _} for _ in _SOME_HASHES]


def _get_simple_encrypt_decrypt_pair(*args):
def _get_simple_encrypt_decrypt_pair(
*args,
) -> typing.Tuple[
typing.Callable[[bytes, bytes], bytes],
typing.Callable[[bytes, bytes], bytes | None],
]:
return se.get_encrypt(*args), se.get_decrypt(*args)


def _get_spiced_simple_encrypt_decrypt_pair(*args):
def _get_spiced_simple_encrypt_decrypt_pair(
*args,
) -> typing.Tuple[
typing.Callable[[bytes, bytes], bytes],
typing.Callable[[bytes, bytes], bytes | None],
]:
return sse.get_encrypt(*args), sse.get_decrypt(*args)


Expand Down

0 comments on commit c4a9b03

Please sign in to comment.