11import re
22from collections .abc import Iterable
33
4+ from algosdk import abi
5+
46from algokit_client_generator .document import DocumentParts , Part
57
68
@@ -23,41 +25,45 @@ def get_method_name(name: str, string_suffix: str = "") -> str:
2325 return "_" .join (p for p in parts )
2426
2527
26- def map_abi_type_to_python (abi_type : str ) -> str : # noqa: ignore[PLR0911]
27- match = re .match (r".*\[([0-9]*)]$" , abi_type )
28- if match :
29- array_size = match .group (1 )
30- if array_size :
31- abi_type = abi_type [: - 2 - len (array_size )]
32- array_size = int (array_size )
33- inner_type = ", " .join ([map_abi_type_to_python (abi_type )] * array_size )
34- tuple_type = f"tuple[{ inner_type } ]"
35- if abi_type == "byte" :
36- return f"bytes | { tuple_type } "
37- return tuple_type
38- else :
39- abi_type = abi_type [:- 2 ]
40- if abi_type == "byte" :
41- return "bytes"
42- return f"list[{ map_abi_type_to_python (abi_type )} ]"
43- if abi_type .startswith ("(" ) and abi_type .endswith (")" ):
44- abi_type = abi_type [1 :- 1 ]
45- inner_types = [map_abi_type_to_python (t ) for t in abi_type .split ("," )]
46- return f"tuple[{ ', ' .join (inner_types )} ]"
47- # TODO validate or annotate ints
48- python_type = {
49- "string" : "str" ,
50- "uint8" : "int" , # < 256
51- "uint32" : "int" , # < 2^32
52- "uint64" : "int" , # < 2^64
53- "void" : "None" ,
54- "byte[]" : "bytes" ,
55- "byte" : "int" , # length 1
56- "pay" : "TransactionWithSigner" ,
57- }.get (abi_type )
58- if python_type :
59- return python_type
60- return abi_type
28+ def abi_type_to_python (abi_type : abi .ABIType ) -> str : # noqa: ignore[PLR0911]
29+ match abi_type :
30+ case abi .UintType ():
31+ return "int"
32+ case abi .ArrayDynamicType () as array :
33+ child = array .child_type
34+ if isinstance (child , abi .ByteType ):
35+ return "bytes | bytearray"
36+ return f"list[{ abi_type_to_python (child )} ]"
37+ case abi .ArrayStaticType () as array :
38+ child = array .child_type
39+ if isinstance (child , abi .ByteType ):
40+ return f"bytes | bytearray | tuple[{ ', ' .join ('int' for _ in range (array .static_length ))} ]"
41+ inner_type = abi_type_to_python (child )
42+ return f"list[{ inner_type } ] | tuple[{ ', ' .join (inner_type for _ in range (array .static_length ))} ]"
43+ case abi .AddressType ():
44+ return "str"
45+ case abi .BoolType ():
46+ return "bool"
47+ case abi .UfixedType ():
48+ return "decimal.Decimal"
49+ case abi .TupleType () as tuple_type :
50+ return f"tuple[{ ', ' .join (abi_type_to_python (t ) for t in tuple_type .child_types )} ]"
51+ case abi .ByteType ():
52+ return "int"
53+ case abi .StringType ():
54+ return "str"
55+ case _:
56+ return "typing.Any"
57+
58+
59+ def map_abi_type_to_python (abi_type_str : str ) -> str :
60+ if abi_type_str == "void" :
61+ return "None"
62+ if abi .is_abi_transaction_type (abi_type_str ):
63+ # TODO: generic TransactionWithSigner and/or allow unsigned types signed with signer used in transaction
64+ return "TransactionWithSigner"
65+ abi_type = abi .ABIType .from_string (abi_type_str )
66+ return abi_type_to_python (abi_type )
6167
6268
6369def get_unique_symbol_by_incrementing (existing_symbols : set [str ], base_name : str ) -> str :
0 commit comments