Vyper: Bytes doesn't accept 0x... literals

Created on 1 Jun 2018  路  4Comments  路  Source: vyperlang/vyper

What's your issue about?

When using bytes currently you must use "" in order to set literal value, this treats each input as a char.

@public
def a() -> bytes[64]:
     return "1111111100000000111111110000000011111111000000001111111100000000"

When trying to use 0x... in order to assign value to bytes it gives the following error: Invalid type, expected: bytes.

Example:

@public
def foo():
    calldata: bytes[64] = 0x1111111000000000000000000000000000000000000000000000000000000000

Note that this issue doesn't exist when using bytes32.

How can it be fixed?

Add support for 0x... like in bytes32 to the bytes type.

Cute Animal Picture

image

enhancement

Most helpful comment

@carver @ben-kaufman This has been fixed we have b''bytes" style for bytes now. And normal strings use "string". a: bytes[1] = 0b00001000 (multiple of 8s/bytes only) is also supported. Closing issue :rocket: :smile:

All 4 comments

Note that python hex literals trim leading zeros (since it's represented as an int), so that causes at least two complications:

  1. there would be no way to represent bytes with leading zeros in a hex literal
  2. people who try to represent bytes with leading zeros in a hex literal will silently end up with bytes without the leading zeros

It's not awesome, but it's less ambiguous to write a hex string:

val: bytes = '0x00000123456789abcdef'

and still maintain the ability to define bytes literals:

val2: bytes = b'ghij'

But I'm really unfamiliar with vyper syntax, so take all this with a giant grain of salt.

Vyper currently supports hex format, in bytes as follows:

val: bytes = '\x00\x00\x01\x23...'

Because of this I am tagging "0x000" Style hex has an enhancement.

To avoid confusion for people used to the Python3 bytes/str distinction, I'd highly recommend updating to:

val: bytes = b'\x00\x00\x01\x23...'

with the b prefix. Otherwise the syntax implies a string of unicode code points.

@carver @ben-kaufman This has been fixed we have b''bytes" style for bytes now. And normal strings use "string". a: bytes[1] = 0b00001000 (multiple of 8s/bytes only) is also supported. Closing issue :rocket: :smile:

Was this page helpful?
0 / 5 - 0 ratings