Skip to content

Commit a22e479

Browse files
committed
refactor: contravariant typevars for typed factory protocol
plus a fix for box decoding
1 parent 9fd3d10 commit a22e479

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/algokit_utils/applications/app_factory.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,14 @@ def create(
506506
)
507507

508508

509-
class TypedAppFactoryProtocol(Protocol):
509+
CreateParamsT = TypeVar( # noqa: PLC0105
510+
"CreateParamsT", bound=AppClientMethodCallCreateParams | AppClientBareCallCreateParams, contravariant=True
511+
)
512+
UpdateParamsT = TypeVar("UpdateParamsT", bound=AppClientMethodCallParams | AppClientBareCallParams, contravariant=True) # noqa: PLC0105
513+
DeleteParamsT = TypeVar("DeleteParamsT", bound=AppClientMethodCallParams | AppClientBareCallParams, contravariant=True) # noqa: PLC0105
514+
515+
516+
class TypedAppFactoryProtocol(Protocol, Generic[CreateParamsT, UpdateParamsT, DeleteParamsT]):
510517
def __init__(
511518
self,
512519
algorand: AlgorandClientProtocol,
@@ -519,9 +526,9 @@ def deploy( # noqa: PLR0913
519526
deploy_time_params: TealTemplateParams | None = None,
520527
on_update: OnUpdate = OnUpdate.Fail,
521528
on_schema_break: OnSchemaBreak = OnSchemaBreak.Fail,
522-
create_params: AppClientMethodCallCreateParams | AppClientBareCallCreateParams | None = None,
523-
update_params: AppClientMethodCallParams | AppClientBareCallParams | None = None,
524-
delete_params: AppClientMethodCallParams | AppClientBareCallParams | None = None,
529+
create_params: CreateParamsT | None = None,
530+
update_params: UpdateParamsT | None = None,
531+
delete_params: DeleteParamsT | None = None,
525532
existing_deployments: AppLookup | None = None,
526533
ignore_cache: bool = False,
527534
updatable: bool | None = None,

src/algokit_utils/applications/app_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def get_box_value_from_abi_type(self, app_id: int, box_name: BoxIdentifier, abi_
216216
value = self.get_box_value(app_id, box_name)
217217
try:
218218
parse_to_tuple = isinstance(abi_type, algosdk.abi.TupleType)
219-
decoded_value = abi_type.decode(base64.b64decode(value))
219+
decoded_value = abi_type.decode(value)
220220
return tuple(decoded_value) if parse_to_tuple else decoded_value
221221
except Exception as e:
222222
raise ValueError(f"Failed to decode box value {value.decode('utf-8')} with ABI type {abi_type}") from e

tests/applications/test_app_client.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,11 @@ def test_retrieve_state(test_app_client: AppClient, funded_account: Account) ->
517517

518518
assert sorted(b.name.name_base64 for b in box_values) == sorted([box_name1_base64, box_name2_base64])
519519
box1 = next(b for b in box_values if b.name.name_base64 == box_name1_base64)
520-
assert box1.value == base64.b64encode(bytes("value1", "utf-8"))
520+
assert box1.value == b"value1"
521521
assert box1_value == box1.value
522522

523523
box2 = next(b for b in box_values if b.name.name_base64 == box_name2_base64)
524-
assert box2.value == base64.b64encode(bytes("value2", "utf-8"))
524+
assert box2.value == b"value2"
525525

526526
# Legacy contract strips ABI prefix; manually encoded ABI string after
527527
# passing algosdk's atc results in \x00\n\x00\n1234524352.
@@ -659,8 +659,7 @@ def test_box_methods_with_arc4_returns_parametrized(
659659
)
660660

661661
# Encode the expected value using the specified ABI type
662-
value_encoded = ABIType.from_string(value_type).encode(arg_value)
663-
expected_value = base64.b64encode(value_encoded)
662+
expected_value = ABIType.from_string(value_type).encode(arg_value)
664663

665664
# Retrieve the actual box value
666665
actual_box_value = test_app_client_puya.get_box_value(box_reference)

0 commit comments

Comments
 (0)