Vyper: Using Python slice syntax with Bytes array doesn't work

Created on 30 Jul 2019  路  6Comments  路  Source: vyperlang/vyper

Version Information

  • vyper Version (output of vyper --version): 0.1.0b11

What's your issue about?

The following doesn't compile:

@public
def foo(b: bytes[65]) -> bytes32:
    return convert(b[:32], bytes32)

and gives the following error message, which isn't helpful

...
vyper/ast_utils.py in _build_vyper_ast_list(source_code, node)
     28         yield parse_python_ast(
     29             source_code=source_code,
---> 30             node=n,
     31         )
     32

vyper/ast_utils.py in parse_python_ast(source_code, node)
     76             vyper_class = getattr(vyper_ast, class_name)
     77             init_kwargs = _build_vyper_ast_init_kwargs(
---> 78                 source_code, node, vyper_class, class_name
     79             )
     80             return vyper_class(**init_kwargs)

vyper/utils.py in f(*args, **kwargs)
    266         @functools.wraps(func)
    267         def f(*args, **kwargs):
--> 268             return cast_type(func(*args, **kwargs))
    269         return f
    270     return yf

vyper/ast_utils.py in _build_vyper_ast_init_kwargs(source_code, node, vyper_class, class_name)
     63                 parse_python_ast(
     64                     source_code=source_code,
---> 65                     node=val,
     66                 )
     67             )

vyper/ast_utils.py in parse_python_ast(source_code, node)
     78                 source_code, node, vyper_class, class_name
     79             )
---> 80             return vyper_class(**init_kwargs)
     81         else:
     82             raise SyntaxException(

vyper/ast.py in __init__(self, **kwargs)
     30             elif value:
     31                 raise CompilerPanic(
---> 32                     f'Unsupported non-empty value field_name: {field_name}, '
     33                     f' class: {type(self)} value: {value}'
     34                 )

CompilerPanic: Unsupported non-empty value field_name: upper,  class: <class 'vyper.ast.Slice'> value: <vyper.ast.Num object at 0x7efe06a46d18> Please create an issue.

How can it be fixed?

I don't think it _should_ compile, at least how we have the compiler currently set up, but this is probably a common hangup people are likely to run in to if that is the case, so we should probably have a more helpful error message like Please use 'slice(b, start=0, end=32)' instead.

Easy Pickings bug

All 6 comments

Maybe we can have tailored messages for specific unsupported syntax, but I also feel like a better default message would be something like this:

SyntaxError: line 31:3 Unsupported python syntax
     30
---> 31 b[:32]
----------^
     32

Do you mean unsupported Vyper syntax? That's valid python

Yeah, it's valid python syntax but it's unsupported by vyper. To me, unsupported Vyper syntax is confusing because it suggests there's some syntax which we consider to be valid vyper code but it's unsupported by the compiler or something. Maybe just Unsupported syntax is better? My point was really that the default error message could be a lot better. And then we could have a nice API for defining more specific error messages for certain common cases.

Invalid syntax or Unsupported syntax (without mentioning the project) would be better in my book.

:+1: for Unsupported syntax

This was fixed a while ago in one of the AST updates.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jacqueswww picture jacqueswww  路  3Comments

haydenadams picture haydenadams  路  3Comments

nrryuya picture nrryuya  路  3Comments

pipermerriam picture pipermerriam  路  3Comments

jacqueswww picture jacqueswww  路  4Comments