From 8cc5f389f94f8a4dabd861f72ff89d78232047dd Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 28 Jul 2026 19:44:58 +0200 Subject: [PATCH] Fix --- src/pulse_sdk_py/pwp.py | 280 +++++++--------------------------------- 1 file changed, 44 insertions(+), 236 deletions(-) diff --git a/src/pulse_sdk_py/pwp.py b/src/pulse_sdk_py/pwp.py index 928e8ca..ae40c01 100644 --- a/src/pulse_sdk_py/pwp.py +++ b/src/pulse_sdk_py/pwp.py @@ -4,6 +4,7 @@ import types from typing import ( Protocol, TypeVar, + Any, get_type_hints, get_origin, get_args, @@ -38,313 +39,120 @@ def encode_value(value, typ) -> bytes: # Optional[T] if origin in (typing.Union, types.UnionType): args = get_args(typ) - if type(None) in args: if value is None: return b"\x00" - - real = next( - x for x in args - if x is not type(None) - ) - + real = next(x for x in args if x is not type(None)) return b"\x01" + encode_value(value, real) - # list[T] - if origin is list: - item_type = get_args(typ)[0] - - out = len(value).to_bytes(4, "little") - - for item in value: - out += encode_value(item, item_type) - - return out - - # tuple[T] - if origin is tuple: + # list[T] or list + if origin is list or typ is list: args = get_args(typ) + item_type = args[0] if args else Any out = len(value).to_bytes(4, "little") + for item in value: + # If item_type is Any, infer type dynamically from the item itself + actual_type = item_type if item_type is not Any else type(item) + out += encode_value(item, actual_type) + return out - for item, item_type in zip(value, args): + # tuple[T, ...] + if origin is tuple or typ is tuple: + args = get_args(typ) + out = len(value).to_bytes(4, "little") + for item, item_type in zip(value, args if args else [type(x) for x in value]): out += encode_value(item, item_type) - return out - # dict[K,V] - if origin is dict: - key_type, value_type = get_args(typ) - + # dict[K, V] + if origin is dict or typ is dict: + args = get_args(typ) + key_type, value_type = args if args else (Any, Any) out = len(value).to_bytes(4, "little") - for k, v in value.items(): - out += encode_value(k, key_type) - out += encode_value(v, value_type) - + out += encode_value(k, key_type if key_type is not Any else type(k)) + out += encode_value(v, value_type if value_type is not Any else type(v)) return out - - # primitives - + # Primitives if typ is bool: return b"\x01" if value else b"\x00" - if typ is int: return struct.pack("