Exemple #1
0
    def fetch_call_return(self, node: vy_ast.Call) -> StructDefinition:
        validate_call_args(node, 1)
        if not isinstance(node.args[0], vy_ast.Dict):
            raise VariableDeclarationException(
                "Struct values must be declared via dictionary", node.args[0])
        if next(
            (i for i in self.members.values()
             if isinstance(i, MappingDefinition)),
                False,
        ):
            raise VariableDeclarationException(
                "Struct contains a mapping and so cannot be declared as a literal",
                node)

        members = self.members.copy()
        for key, value in zip(node.args[0].keys, node.args[0].values):
            if key is None or key.get("id") not in members:
                raise UnknownAttribute("Unknown or duplicate struct member",
                                       key or value)
            validate_expected_type(value, members.pop(key.id))
        if members:
            raise VariableDeclarationException(
                f"Struct declaration does not define all fields: {', '.join(list(members))}",
                node,
            )

        return StructDefinition(self._id, self.members)
Exemple #2
0
 def get_member(self, key: str, node: vy_ast.Attribute) -> EnumDefinition:
     if key in self.members:
         return self.from_annotation(node.value)
     suggestions_str = get_levenshtein_error_suggestions(
         key, self.members, 0.3)
     raise UnknownAttribute(
         f"{self} has no member '{key}'. {suggestions_str}", node)
Exemple #3
0
 def get_member(self, key: str, node: vy_ast.VyperNode) -> BaseTypeDefinition:
     if key in self.members:
         return self.members[key]
     elif key in getattr(self, "_type_members", []):
         type_ = copy.deepcopy(self._type_members[key])
         type_.location = self.location
         type_.is_immutable = self.is_immutable
         return type_
     raise UnknownAttribute(f"{self} has no member '{key}'", node)
Exemple #4
0
 def get_member(self, key: str,
                node: vy_ast.VyperNode) -> BaseTypeDefinition:
     if key in self.members:
         return self.members[key]
     elif key in getattr(self, "_type_members", []):
         type_ = copy.deepcopy(self._type_members[key])
         type_.location = self.location
         type_.is_constant = self.is_constant
         return type_
     suggestions_str = get_levenshtein_error_suggestions(
         key, self.members, 0.3)
     raise UnknownAttribute(
         f"{self} has no member '{key}'. {suggestions_str}", node)
Exemple #5
0
    def fetch_call_return(self, node: vy_ast.Call) -> StructDefinition:
        validate_call_args(node, 1)
        if not isinstance(node.args[0], vy_ast.Dict):
            raise VariableDeclarationException(
                "Struct values must be declared via dictionary", node.args[0])
        if next((i for i in self.members.values()
                 if isinstance(i, MappingDefinition)), False):
            raise VariableDeclarationException(
                "Struct contains a mapping and so cannot be declared as a literal",
                node)

        members = self.members.copy()
        keys = list(self.members.keys())
        for i, (key,
                value) in enumerate(zip(node.args[0].keys,
                                        node.args[0].values)):
            if key is None or key.get("id") not in members:
                suggestions_str = get_levenshtein_error_suggestions(
                    key.get("id"), members, 1.0)
                raise UnknownAttribute(
                    f"Unknown or duplicate struct member. {suggestions_str}",
                    key or value)
            expected_key = keys[i]
            if key.id != expected_key:
                raise InvalidAttribute(
                    "Struct keys are required to be in order, but got "
                    f"`{key.id}` instead of `{expected_key}`. (Reminder: the "
                    f"keys in this struct are {list(self.members.items())})",
                    key,
                )

            validate_expected_type(value, members.pop(key.id))

        if members:
            raise VariableDeclarationException(
                f"Struct declaration does not define all fields: {', '.join(list(members))}",
                node)

        return StructDefinition(self._id, self.members)