Skip to content

Commit 7ae33ea

Browse files
committed
refactor: change value generator signature to fix mypy issue
1 parent a3afb80 commit 7ae33ea

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

docs/testing-guide/avm-types.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ bytes_value = algopy.FixedBytes[typing.Literal[16]](b"Hello, Algorand!")
6060
# Instantiate test context
6161
...
6262
63-
# Generate random byte sequences of length
64-
random_bytes = context.any.fixed_bytes(length=32)
63+
# Generate random byte sequences of length 32
64+
random_bytes = context.any.fixed_bytes(FixedBytes[typing.Literal[32]])
6565
```
6666

6767
## String

src/_algopy_testing/value_generators/avm.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from _algopy_testing.models.account import AccountFields
1919
from _algopy_testing.models.application import ApplicationContextData, ApplicationFields
2020
from _algopy_testing.models.asset import AssetFields
21-
from _algopy_testing.utils import generate_random_int, get_type_generic_from_int_literal
21+
from _algopy_testing.utils import generate_random_int
2222

2323
if typing.TYPE_CHECKING:
2424
import algopy
@@ -200,15 +200,18 @@ def bytes(self, length: int | None = None) -> algopy.Bytes:
200200
length = length or MAX_BYTES_SIZE
201201
return _algopy_testing.Bytes(secrets.token_bytes(length))
202202

203-
def fixed_bytes(self, length: _TBytesLength) -> algopy.FixedBytes[_TBytesLength]:
203+
def fixed_bytes(
204+
self, fixed_bytes_type: type[algopy.FixedBytes[_TBytesLength]]
205+
) -> algopy.FixedBytes[_TBytesLength]:
204206
"""Generate a random fixed byte sequence of a specified length.
205207
206-
:param length: Length of the fixed byte sequence.
208+
:param fixed_bytes_type: The FixedBytes type with length parameter (e.g.,
209+
FixedBytes[typing.Literal[10]]).
207210
:returns: The randomly generated fixed byte sequence.
208211
"""
209-
210-
length_t = get_type_generic_from_int_literal(length)
211-
return _algopy_testing.FixedBytes[length_t](secrets.token_bytes(length)) # type: ignore[valid-type]
212+
# Extract the length from the type parameter
213+
length = fixed_bytes_type._length
214+
return fixed_bytes_type(secrets.token_bytes(length))
212215

213216

214217
def _get_app_id(app: algopy.Application | algopy.UInt64 | int) -> int:

tests/value_generators/test_avm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from _algopy_testing.primitives.bytes import Bytes
1111
from _algopy_testing.primitives.fixed_bytes import FixedBytes
1212
from _algopy_testing.primitives.string import String
13+
from _algopy_testing.utils import get_type_generic_from_int_literal
1314

1415

1516
@pytest.fixture()
@@ -66,7 +67,8 @@ def test_avm_bytes_generator(context: AlgopyTestContext, length: int | None) ->
6667

6768
@pytest.mark.parametrize("length", [0, 10, MAX_BYTES_SIZE])
6869
def test_avm_fixed_bytes_generator(context: AlgopyTestContext, length: int) -> None:
69-
value = context.any.fixed_bytes(length)
70+
length_t = get_type_generic_from_int_literal(length)
71+
value = context.any.fixed_bytes(FixedBytes[length_t]) # type: ignore[valid-type]
7072
assert isinstance(value, algopy.FixedBytes)
7173
assert_length(value, length)
7274

0 commit comments

Comments
 (0)