|
11 | 11 | from pyteal.ast.bytes import Bytes |
12 | 12 | from pyteal.ast.unaryexpr import Itob, Len |
13 | 13 | from pyteal.ast.substring import Suffix |
14 | | -from pyteal.ast.int import Int |
15 | | -from pyteal.ast.expr import Expr |
16 | 14 | from pyteal.ast.naryexpr import Concat |
17 | 15 |
|
18 | 16 | from pyteal.types import TealType, require_type |
|
22 | 20 | def encoded_string(s: Expr): |
23 | 21 | return Concat(Suffix(Itob(Len(s)), Int(6)), s) |
24 | 22 |
|
| 23 | + |
25 | 24 | T = TypeVar("T", bound=BaseType) |
26 | 25 |
|
| 26 | + |
27 | 27 | class StringTypeSpec(DynamicArrayTypeSpec): |
28 | 28 | def __init__(self) -> None: |
29 | 29 | super().__init__(ByteTypeSpec()) |
@@ -53,17 +53,31 @@ def get(self) -> Expr: |
53 | 53 | self.stored_value.load(), Int(Uint16TypeSpec().byte_length_static()) |
54 | 54 | ) |
55 | 55 |
|
56 | | - def set(self, value: Union[Sequence[T], DynamicArray[T], ComputedValue[DynamicArray[T]], "String", str, Expr]) -> Expr: |
| 56 | + def set( |
| 57 | + self, |
| 58 | + value: Union[ |
| 59 | + Sequence[T], |
| 60 | + DynamicArray[T], |
| 61 | + ComputedValue[DynamicArray[T]], |
| 62 | + "String", |
| 63 | + str, |
| 64 | + Expr, |
| 65 | + ], |
| 66 | + ) -> Expr: |
57 | 67 |
|
58 | 68 | # Assume length prefixed |
59 | 69 | if isinstance(value, ComputedValue): |
60 | 70 | if value.produced_type_spec() is not StringTypeSpec(): |
61 | | - raise TealInputError(f"Got ComputedValue with type spec {value.produced_type_spec()}, expected StringTypeSpec") |
| 71 | + raise TealInputError( |
| 72 | + f"Got ComputedValue with type spec {value.produced_type_spec()}, expected StringTypeSpec" |
| 73 | + ) |
62 | 74 | return value.store_into(self) |
63 | 75 |
|
64 | 76 | if isinstance(value, BaseType): |
65 | 77 | if value.type_spec() is not StringTypeSpec(): |
66 | | - raise TealInputError(f"Got {value.__class__} with type spec {value.type_spec()}, expected StringTypeSpec") |
| 78 | + raise TealInputError( |
| 79 | + f"Got {value.__class__} with type spec {value.type_spec()}, expected StringTypeSpec" |
| 80 | + ) |
67 | 81 | return value.decode(value.encode()) |
68 | 82 |
|
69 | 83 | # Assume not length prefixed |
|
0 commit comments