Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Apr 22, 2022
1 parent c5815a4 commit a9936c4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
23 changes: 19 additions & 4 deletions pyteal/ast/abi/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
T = TypeVar("T", bound=BaseType)
N = TypeVar("N", bound=int)


class AddressTypeSpec(StaticArrayTypeSpec):
def __init__(self) -> None:
super().__init__(ByteTypeSpec(), ADDRESS_LENGTH)
Expand Down Expand Up @@ -40,16 +41,30 @@ def type_spec(self) -> AddressTypeSpec:
def get(self) -> Expr:
return self.stored_value.load()

def set(self, value: Union[Sequence[T], StaticArray[T, N], ComputedValue[StaticArray[T, N]], "Address", str, Expr]):

def set(
self,
value: Union[
Sequence[T],
StaticArray[T, N],
ComputedValue[StaticArray[T, N]],
"Address",
str,
Expr,
],
):

if isinstance(value, ComputedValue):
if value.produced_type_spec() is not AddressTypeSpec():
raise TealInputError(f"Got ComputedValue with type spec {value.produced_type_spec()}, expected AddressTypeSpec")
raise TealInputError(
f"Got ComputedValue with type spec {value.produced_type_spec()}, expected AddressTypeSpec"
)
return value.store_into(self)

if isinstance(value, BaseType):
if value.type_spec() is not AddressTypeSpec():
raise TealInputError(f"Got {value.__class__} with type spec {value.type_spec()}, expected AddressTypeSpec")
raise TealInputError(
f"Got {value.__class__} with type spec {value.type_spec()}, expected AddressTypeSpec"
)
return self.decode(self.encode())

if isinstance(value, str):
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/abi/array_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pyteal.ast.expr import Expr
from pyteal.ast.seq import Seq

from pyteal.ast.abi.type import ComputedValue, TypeSpec, BaseType
from pyteal.ast.abi.type import ComputedValue, BaseType
from pyteal.ast.abi.uint import Uint16
from pyteal.ast.abi.array_base import ArrayTypeSpec, Array

Expand Down
24 changes: 19 additions & 5 deletions pyteal/ast/abi/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from pyteal.ast.bytes import Bytes
from pyteal.ast.unaryexpr import Itob, Len
from pyteal.ast.substring import Suffix
from pyteal.ast.int import Int
from pyteal.ast.expr import Expr
from pyteal.ast.naryexpr import Concat

from pyteal.types import TealType, require_type
Expand All @@ -22,8 +20,10 @@
def encoded_string(s: Expr):
return Concat(Suffix(Itob(Len(s)), Int(6)), s)


T = TypeVar("T", bound=BaseType)


class StringTypeSpec(DynamicArrayTypeSpec):
def __init__(self) -> None:
super().__init__(ByteTypeSpec())
Expand Down Expand Up @@ -53,17 +53,31 @@ def get(self) -> Expr:
self.stored_value.load(), Int(Uint16TypeSpec().byte_length_static())
)

def set(self, value: Union[Sequence[T], DynamicArray[T], ComputedValue[DynamicArray[T]], "String", str, Expr]) -> Expr:
def set(
self,
value: Union[
Sequence[T],
DynamicArray[T],
ComputedValue[DynamicArray[T]],
"String",
str,
Expr,
],
) -> Expr:

# Assume length prefixed
if isinstance(value, ComputedValue):
if value.produced_type_spec() is not StringTypeSpec():
raise TealInputError(f"Got ComputedValue with type spec {value.produced_type_spec()}, expected StringTypeSpec")
raise TealInputError(
f"Got ComputedValue with type spec {value.produced_type_spec()}, expected StringTypeSpec"
)
return value.store_into(self)

if isinstance(value, BaseType):
if value.type_spec() is not StringTypeSpec():
raise TealInputError(f"Got {value.__class__} with type spec {value.type_spec()}, expected StringTypeSpec")
raise TealInputError(
f"Got {value.__class__} with type spec {value.type_spec()}, expected StringTypeSpec"
)
return value.decode(value.encode())

# Assume not length prefixed
Expand Down

0 comments on commit a9936c4

Please sign in to comment.