Skip to content

Commit

Permalink
fix: string to bool conversion (#3391)
Browse files Browse the repository at this point in the history
* add StringT to cases in _convert.py
* add tests
  • Loading branch information
tserg authored May 8, 2023
1 parent 3c83947 commit d9e376c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion tests/parser/functions/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

BASE_TYPES = set(IntegerT.all()) | set(BytesM_T.all()) | {DecimalT(), AddressT(), BoolT()}

TEST_TYPES = BASE_TYPES | {BytesT(32)}
TEST_TYPES = BASE_TYPES | {BytesT(32)} | {StringT(32)}

ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"

Expand Down Expand Up @@ -163,6 +163,17 @@ def _cases_for_Bytes(typ):
# would not need this if we tested all Bytes[1]...Bytes[32] types.
for i in range(32):
ret.extend(_cases_for_bytes(BytesM_T(i + 1)))

ret.append(b"")
return uniq(ret)


def _cases_for_String(typ):
ret = []
# would not need this if we tested all Bytes[1]...Bytes[32] types.
for i in range(32):
ret.extend([str(c, "utf-8") for c in _cases_for_bytes(BytesM_T(i + 1))])
ret.append("")
return uniq(ret)


Expand All @@ -176,6 +187,8 @@ def interesting_cases_for_type(typ):
return _cases_for_bytes(typ)
if isinstance(typ, BytesT):
return _cases_for_Bytes(typ)
if isinstance(typ, StringT):
return _cases_for_String(typ)
if isinstance(typ, BoolT):
return _cases_for_bool(typ)
if isinstance(typ, AddressT):
Expand Down
2 changes: 1 addition & 1 deletion vyper/builtins/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def _literal_decimal(expr, arg_typ, out_typ):
def to_bool(expr, arg, out_typ):
_check_bytes(expr, arg, out_typ, 32) # should we restrict to Bytes[1]?

if isinstance(arg.typ, BytesT):
if isinstance(arg.typ, _BytestringT):
# no clamp. checks for any nonzero bytes.
arg = _bytes_to_num(arg, out_typ, signed=False)

Expand Down

0 comments on commit d9e376c

Please sign in to comment.