How to use the vyper.exceptions.InvalidTypeException function in vyper

To help you get started, we’ve selected a few vyper examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vyperlang / vyper / tests / parser / features / external_contracts / test_modifiable_external_contract_calls.py View on Github external
def test_invalid_external_contract_call_declaration_1(assert_compile_failed, get_contract):
    contract_1 = """
class Bar():
    def bar() -> int128: pass

bar_contract: static(Bar)

@public
def foo(contract_address: contract(Boo)) -> int128:
    self.bar_contract = contract_address
    return self.bar_contract.bar()
    """

    assert_compile_failed(lambda: get_contract(contract_1), InvalidTypeException)
github vyperlang / vyper / vyper / types / types.py View on Github external
return ContractType(item.args[0].id)
        # Struct types
        if (custom_structs is not None) and (item.func.id in custom_structs):
            return make_struct_type(
                item.id,
                location,
                custom_structs[item.id],
                custom_units,
                custom_structs,
                constants,
            )
        if not isinstance(item.func, ast.Name):
            raise InvalidTypeException("Malformed unit type:", item)
        base_type = item.func.id
        if base_type not in ('int128', 'uint256', 'decimal', 'address'):
            raise InvalidTypeException("You must use int128, uint256, decimal, address, contract, \
                for variable declarations and indexed for logging topics ", item)
        if len(item.args) == 0:
            raise InvalidTypeException("Malformed unit type", item)
        if isinstance(item.args[-1], ast.Name) and item.args[-1].id == "positional":
            positional = True
            argz = item.args[:-1]
        else:
            positional = False
            argz = item.args
        if len(argz) != 1:
            raise InvalidTypeException("Malformed unit type", item)
        unit = parse_unit(argz[0], custom_units=custom_units)
        return BaseType(base_type, unit, positional)
    # Subscripts
    elif isinstance(item, ast.Subscript):
        if isinstance(item.slice, ast.Slice):
github vyperlang / vyper / vyper / types / types.py View on Github external
return {item.id: 1}
    elif isinstance(item, ast.Num) and item.n == 1:
        return {}
    elif not isinstance(item, ast.BinOp):
        raise InvalidTypeException("Invalid unit expression", item)
    elif isinstance(item.op, ast.Mult):
        left, right = parse_unit(item.left, custom_units), parse_unit(item.right, custom_units)
        return combine_units(left, right)
    elif isinstance(item.op, ast.Div):
        left, right = parse_unit(item.left, custom_units), parse_unit(item.right, custom_units)
        return combine_units(left, right, div=True)
    elif isinstance(item.op, ast.Pow):
        if not isinstance(item.left, ast.Name):
            raise InvalidTypeException("Can only raise a base type to an exponent", item)
        if not isinstance(item.right, ast.Num) or not isinstance(item.right.n, int) or item.right.n <= 0:  # noqa: E501
            raise InvalidTypeException("Exponent must be positive integer", item)
        return {item.left.id: item.right.n}
    else:
        raise InvalidTypeException("Invalid unit expression", item)
github vyperlang / vyper / vyper / types / types.py View on Github external
custom_structs,
                constants,
            )
        else:
            raise InvalidTypeException("Invalid base type: " + item.id, item)
    # Units, e.g. num (1/sec) or contracts
    elif isinstance(item, ast.Call) and isinstance(item.func, ast.Name):
        # Mapping type.
        if item.func.id == 'map':
            if location == 'memory':
                raise InvalidTypeException(
                    "No mappings allowed for in-memory types, only fixed-size arrays",
                    item,
                )
            if len(item.args) != 2:
                raise InvalidTypeException(
                    "Mapping requires 2 valid positional arguments.",
                    item,
                )
            keytype = parse_type(
                item.args[0],
                None,
                custom_units=custom_units,
                custom_structs=custom_structs,
                constants=constants,
            )
            if not isinstance(keytype, (BaseType, ByteArrayLike)):
                raise InvalidTypeException("Mapping keys must be base or bytes/string types", item)
            return MappingType(
                keytype,
                parse_type(
                    item.args[1],
github vyperlang / vyper / vyper / types / types.py View on Github external
item,
                )
            if len(item.args) != 2:
                raise InvalidTypeException(
                    "Mapping requires 2 valid positional arguments.",
                    item,
                )
            keytype = parse_type(
                item.args[0],
                None,
                custom_units=custom_units,
                custom_structs=custom_structs,
                constants=constants,
            )
            if not isinstance(keytype, (BaseType, ByteArrayLike)):
                raise InvalidTypeException("Mapping keys must be base or bytes/string types", item)
            return MappingType(
                keytype,
                parse_type(
                    item.args[1],
                    location,
                    custom_units=custom_units,
                    custom_structs=custom_structs,
                    constants=constants,
                ),
            )
        # Contract_types
        if item.func.id == 'address':
            if sigs and item.args[0].id in sigs:
                return ContractType(item.args[0].id)
        # Struct types
        if (custom_structs is not None) and (item.func.id in custom_structs):
github vyperlang / vyper / vyper / types / types.py View on Github external
else:
                return ListType(parse_type(
                    item.value,
                    location,
                    custom_units=custom_units,
                    custom_structs=custom_structs,
                    constants=constants,
                ), n_val)
        # Mappings, e.g. num[address]
        else:
            warnings.warn(
                "Mapping definitions using subscript have deprecated (see VIP564). "
                "Use map(type1, type2) instead.",
                DeprecationWarning
            )
            raise InvalidTypeException('Unknown list type.', item)

    # Dicts, used to represent mappings, e.g. {uint: uint}. Key must be a base type
    elif isinstance(item, ast.Dict):
        warnings.warn(
            "Anonymous structs have been removed in"
            " favor of named structs, see VIP300",
            DeprecationWarning
        )
        raise InvalidTypeException("Invalid type", item)
    elif isinstance(item, ast.Tuple):
        members = [
            parse_type(
                x,
                location,
                custom_units=custom_units,
                custom_structs=custom_structs,
github vyperlang / vyper / vyper / types / types.py View on Github external
if isinstance(item, ast.Name):
        if item.id in BASE_TYPES:
            return BaseType(item.id)
        elif item.id in SPECIAL_TYPES:
            return SPECIAL_TYPES[item.id]
        elif (custom_structs is not None) and (item.id in custom_structs):
            return make_struct_type(
                item.id,
                location,
                custom_structs[item.id],
                custom_units,
                custom_structs,
                constants,
            )
        else:
            raise InvalidTypeException("Invalid base type: " + item.id, item)
    # Units, e.g. num (1/sec) or contracts
    elif isinstance(item, ast.Call) and isinstance(item.func, ast.Name):
        # Mapping type.
        if item.func.id == 'map':
            if location == 'memory':
                raise InvalidTypeException(
                    "No mappings allowed for in-memory types, only fixed-size arrays",
                    item,
                )
            if len(item.args) != 2:
                raise InvalidTypeException(
                    "Mapping requires 2 valid positional arguments.",
                    item,
                )
            keytype = parse_type(
                item.args[0],
github vyperlang / vyper / vyper / types / types.py View on Github external
return make_struct_type(
                item.id,
                location,
                custom_structs[item.id],
                custom_units,
                custom_structs,
                constants,
            )
        else:
            raise InvalidTypeException("Invalid base type: " + item.id, item)
    # Units, e.g. num (1/sec) or contracts
    elif isinstance(item, ast.Call) and isinstance(item.func, ast.Name):
        # Mapping type.
        if item.func.id == 'map':
            if location == 'memory':
                raise InvalidTypeException(
                    "No mappings allowed for in-memory types, only fixed-size arrays",
                    item,
                )
            if len(item.args) != 2:
                raise InvalidTypeException(
                    "Mapping requires 2 valid positional arguments.",
                    item,
                )
            keytype = parse_type(
                item.args[0],
                None,
                custom_units=custom_units,
                custom_structs=custom_structs,
                constants=constants,
            )
            if not isinstance(keytype, (BaseType, ByteArrayLike)):