Skip to content

Commit

Permalink
wip: .vyi files
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Nov 23, 2023
1 parent 6c7f795 commit c923714
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 222 deletions.
2 changes: 1 addition & 1 deletion examples/tokens/ERC721.vy
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface ERC721Receiver:
_operator: address,
_from: address,
_tokenId: uint256,
_data: Bytes[1024]
_data: Bytes[...]
) -> bytes4: nonpayable


Expand Down
2 changes: 2 additions & 0 deletions vyper/ast/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ def visit_Constant(self, node):
node.ast_type = "Str"
elif isinstance(node.value, bytes):
node.ast_type = "Bytes"
elif isinstance(node.value, Ellipsis.__class__):
node.ast_type = "Ellipsis"
else:
raise SyntaxException(
"Invalid syntax (unsupported Python Constant AST node).",
Expand Down
12 changes: 8 additions & 4 deletions vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,12 +898,16 @@ def validate(self):
raise InvalidLiteral("Cannot have an empty tuple", self)


class Dict(ExprNode):
__slots__ = ("keys", "values")
class NameConstant(Constant):
__slots__ = ()


class NameConstant(Constant):
__slots__ = ("value",)
class Ellipsis(Constant):
__slots__ = ()


class Dict(ExprNode):
__slots__ = ("keys", "values")


class Name(ExprNode):
Expand Down
6 changes: 4 additions & 2 deletions vyper/ast/nodes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class Bytes(Constant):
@property
def s(self): ...

class NameConstant(Constant): ...

class Ellipsis(Constant): ...

class List(VyperNode):
elements: list = ...

Expand All @@ -131,8 +135,6 @@ class Dict(VyperNode):
keys: list = ...
values: list = ...

class NameConstant(Constant): ...

class Name(VyperNode):
id: str = ...
_type: str = ...
Expand Down
16 changes: 13 additions & 3 deletions vyper/semantics/analysis/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,22 @@ def _load_import(self, node: vy_ast.VyperNode, level: int, module_str: str, alia
except FileNotFoundError:
pass

try:
file = self.input_bundle.load_file(path.with_suffix(".vyi"))
assert isinstance(file, FileInput) # mypy hint
interface_ast = vy_ast.parse_to_ast(file.source_code, contract_name=str(file.path))
return InterfaceT.from_vyi(interface_ast)
except FileNotFoundError:
pass

try:
file = self.input_bundle.load_file(path.with_suffix(".json"))
assert isinstance(file, ABIInput) # mypy hint
return InterfaceT.from_json_abi(str(file.path), file.abi)
except FileNotFoundError:
raise ModuleNotFoundError(module_str)
pass

raise ModuleNotFoundError(module_str)


# convert an import to a path (without suffix)
Expand Down Expand Up @@ -425,7 +435,7 @@ def _load_builtin_import(level: int, module_str: str) -> InterfaceT:
remapped_module = remapped_module.removeprefix("vyper.interfaces")
remapped_module = vyper.builtins.interfaces.__package__ + remapped_module

path = _import_to_path(level, remapped_module).with_suffix(".vy")
path = _import_to_path(level, remapped_module).with_suffix(".vyi")

try:
file = input_bundle.load_file(path)
Expand All @@ -435,4 +445,4 @@ def _load_builtin_import(level: int, module_str: str) -> InterfaceT:

# TODO: it might be good to cache this computation
interface_ast = vy_ast.parse_to_ast(file.source_code, module_path=path)
return InterfaceT.from_Module(interface_ast, name=module_str)
return InterfaceT.from_vyi(interface_ast)
Loading

0 comments on commit c923714

Please sign in to comment.