Repository: vil02/puzzle_generator

Issue:
Remove `assert`s from `bytes_utils.int_to_bytes`
The `assert`s in [`bytes_utils.int_to_bytes`](https://github.com/vil02/puzzle_generator/blob/227703e928d7e1ae855dd0f95f88cd8e07c331f1/puzzle_generator/bytes_utils.py#L7) should be removed. What needs to be done:

- proper exception should be raised if the input is negative or does not fit into 256 bytes,
- function `byte_length` should be introduced.

Discussion:
codecov-commenter: ## [Codecov](https://app.codecov.io/gh/vil02/puzzle_generator/pull/72?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Piotr+Idzik) Report
Attention: Patch coverage is `0%` with `4 lines` in your changes missing coverage. Please review.
> Project coverage is 98.35%. Comparing base [(`4b1b2ed`)](https://app.codecov.io/gh/vil02/puzzle_generator/commit/4b1b2ed3c7905a4994f264b850e435428afacf8a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Piotr+Idzik) to head [(`5603834`)](https://app.codecov.io/gh/vil02/puzzle_generator/commit/5603834c986655c12ceb43bc1f39f579a02181a1?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Piotr+Idzik).
> Report is 10 commits behind head on master.

| [Files](https://app.codecov.io/gh/vil02/puzzle_generator/pull/72?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Piotr+Idzik) | Patch % | Lines |
|---|---|---|
| [puzzle\_generator/bytes\_utils.py](https://app.codecov.io/gh/vil02/puzzle_generator/pull/72?src=pr&el=tree&filepath=puzzle_generator%2Fbytes_utils.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Piotr+Idzik#diff-cHV6emxlX2dlbmVyYXRvci9ieXRlc191dGlscy5weQ==) | 0.00% | [2 Missing and 2 partials :warning: ](https://app.codecov.io/gh/vil02/puzzle_generator/pull/72?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Piotr+Idzik) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##            master      #72      +/-   ##
===========================================
- Coverage   100.00%   98.35%   -1.65%     
===========================================
  Files           12       13       +1     
  Lines          233      243      +10     
  Branches        21       26       +5     
===========================================
+ Hits           233      239       +6     
- Misses           0        2       +2     
- Partials         0        2       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/vil02/puzzle_generator/pull/72?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Piotr+Idzik).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Piotr+Idzik).


Patch:
diff --git a/puzzle_generator/bytes_utils.py b/puzzle_generator/bytes_utils.py
index bb78a78..f804d41 100644
--- a/puzzle_generator/bytes_utils.py
+++ b/puzzle_generator/bytes_utils.py
@@ -4,15 +4,19 @@ import typing
 BYTEORDER: typing.Literal["little", "big"] = "little"
 
 
-def int_to_bytes(in_value: int) -> bytes:
-    assert in_value >= 0  # nosec B101
+def byte_length(in_value: int) -> int:
+    if in_value < 0:
+        raise ValueError("in_value must be non-negative")
     number_of_bytes = in_value.bit_length() // 8
     if 8 * number_of_bytes < in_value.bit_length():
         number_of_bytes += 1
-    assert number_of_bytes <= 256  # nosec B101
-    assert (
-        number_of_bytes - 1 <= in_value.bit_length() // 8 <= number_of_bytes
-    )  # nosec B101
+    return number_of_bytes
+
+
+def int_to_bytes(in_value: int) -> bytes:
+    number_of_bytes = byte_length(in_value)
+    if number_of_bytes > 255:
+        raise ValueError("in_value must be 255 bytes or less")
     return number_of_bytes.to_bytes(length=1, byteorder=BYTEORDER) + in_value.to_bytes(
         length=number_of_bytes, byteorder=BYTEORDER
     )
diff --git a/pyproject.toml b/pyproject.toml
index 32ece39..c257aba 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "puzzle-generator"
-version = "0.10.2"
+version = "0.11.0"
 description = "Generates python code representing a puzzle"
 authors = ["piotr.idzik <vil02_puzzle_generator@10g.pl>"]
 readme = "./puzzle_generator/README.md"


Test patch:
diff --git a/tests/test_bytes_utils.py b/tests/test_bytes_utils.py
index 93ecce7..24aa0b0 100644
--- a/tests/test_bytes_utils.py
+++ b/tests/test_bytes_utils.py
@@ -7,12 +7,40 @@ from . import utils
 
 
 @pytest.mark.parametrize(
-    "in_value", [0, 1, 2, 3, 255, 256, 257, 3239949409384, 10**570]
+    ("in_value", "expected"),
+    [
+        (0, 0),
+        (1, 1),
+        (2, 1),
+        (3, 1),
+        (255, 1),
+        (256, 2),
+        (257, 2),
+        (2 ** (8 * 255) - 1, 255),
+        (2 ** (8 * 255), 256),
+    ],
+)
+def test_byte_length(in_value, expected):
+    assert bu.byte_length(in_value) == expected
+
+
+def test_byte_length_raises_for_negative_input():
+    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):
     assert bu.bytes_to_int(bu.int_to_bytes(in_value)) == in_value
 
 
+def test_int_to_bytes_raises_when_input_is_too_big():
+    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)
 )