Skip to content

Commit

Permalink
__str__ formatting (#411)
Browse files Browse the repository at this point in the history
* standardize str formatting
  • Loading branch information
barnjamin authored Jun 29, 2022
1 parent 9847b29 commit cb0a5c5
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions pyteal/ast/abi/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def encode(self) -> Expr:
return SetBit(Bytes(b"\x00"), Int(0), self.get())


Bool.__module__ = "pyteal"


def boolAwareStaticByteLength(types: Sequence[TypeSpec]) -> int:
length = 0
ignoreNext = 0
Expand Down
3 changes: 3 additions & 0 deletions pyteal/ast/abi/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ def _set_with_computed_type(self, value: "ComputedValue") -> Expr:
)
return value.store_into(self)

def __str__(self) -> str:
return str(self.type_spec())


BaseType.__module__ = "pyteal"

Expand Down
13 changes: 10 additions & 3 deletions pyteal/ast/abi/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,12 @@ def test_size_of():
# Test for byte/bool/address/strings
(algosdk.abi.ByteType(), "byte", abi.ByteTypeSpec(), abi.Byte),
(algosdk.abi.BoolType(), "bool", abi.BoolTypeSpec(), abi.Bool),
(algosdk.abi.AddressType(), "address", abi.AddressTypeSpec(), abi.Address),
(
algosdk.abi.AddressType(),
"address",
abi.AddressTypeSpec(),
abi.Address,
),
(algosdk.abi.StringType(), "string", abi.StringTypeSpec(), abi.String),
# Test for dynamic array type
(
Expand Down Expand Up @@ -491,7 +496,6 @@ def test_size_of():
# ]
# ),
# ),
(algosdk.abi.TupleType([]), "()", abi.TupleTypeSpec(), abi.Tuple0),
(
"cannot map ABI transaction type spec <pyteal.TransactionTypeSpec",
"txn",
Expand Down Expand Up @@ -612,13 +616,16 @@ def test_size_of():


@pytest.mark.parametrize(
"algosdk_abi, abi_string, pyteal_abi_ts, pyteal_abi", ABI_TRANSLATION_TEST_CASES
"algosdk_abi, abi_string, pyteal_abi_ts, pyteal_abi",
ABI_TRANSLATION_TEST_CASES,
)
def test_abi_type_translation(algosdk_abi, abi_string, pyteal_abi_ts, pyteal_abi):
print(f"({algosdk_abi}, {abi_string}, {pyteal_abi_ts}),")

assert pyteal_abi_ts == abi.type_spec_from_annotation(pyteal_abi)

assert str(pyteal_abi_ts.new_instance()) == abi_string

if abi_string in (
"account",
"application",
Expand Down
4 changes: 3 additions & 1 deletion pyteal/ast/binaryexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def __teal__(self, options: "CompileOptions"):
)

def __str__(self):
return "({} {} {})".format(self.op, self.argLeft, self.argRight)
return "({} {} {})".format(
str(self.op).title().replace("_", ""), self.argLeft, self.argRight
)

def type_of(self):
return self.outputType
Expand Down
4 changes: 2 additions & 2 deletions pyteal/ast/int.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __teal__(self, options: "CompileOptions"):
return TealBlock.FromOp(options, op)

def __str__(self):
return "(Int: {})".format(self.value)
return "(Int {})".format(self.value)

def type_of(self):
return TealType.uint64
Expand All @@ -59,7 +59,7 @@ def __teal__(self, options: "CompileOptions"):
return TealBlock.FromOp(options, op)

def __str__(self):
return "(IntEnum: {})".format(self.name)
return "(IntEnum {})".format(self.name)

def type_of(self):
return TealType.uint64
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/methodsig.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __teal__(self, options: "CompileOptions"):
return TealBlock.FromOp(options, op)

def __str__(self) -> str:
return "(method: {})".format(self.methodName)
return "(MethodSignature '{}')".format(self.methodName)

def type_of(self) -> TealType:
return TealType.bytes
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/naryexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __teal__(self, options: "CompileOptions"):
return start, end

def __str__(self):
ret_str = "(" + str(self.op)
ret_str = "(" + str(self.op).title().replace("_", "")
for a in self.args:
ret_str += " " + a.__str__()
ret_str += ")"
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/unaryexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __teal__(self, options: "CompileOptions"):
return TealBlock.FromOp(options, TealOp(self, self.op), self.arg)

def __str__(self):
return "({} {})".format(self.op, self.arg)
return "({} {})".format(str(self.op).title().replace("_", ""), self.arg)

def type_of(self):
return self.outputType
Expand Down

0 comments on commit cb0a5c5

Please sign in to comment.