Skip to content

Commit

Permalink
fix: interfaces access from .vyi files
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Feb 9, 2024
1 parent 161d3ac commit 5c30bf4
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion vyper/semantics/types/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ def __init__(self, module: vy_ast.Module, name: Optional[str] = None):
for i in self.interface_defs:
# add the type of the interface so it can be used in call position
self.add_member(i.name, TYPE_T(i._metadata["interface_type"])) # type: ignore
self._helper.add_member(i.name, TYPE_T(i._metadata["interface_type"])) # type: ignore

for v in self.variable_decls:
self.add_member(v.target.id, v.target._metadata["varinfo"])
Expand All @@ -326,6 +325,10 @@ def __init__(self, module: vy_ast.Module, name: Optional[str] = None):
import_info = i._metadata["import_info"]
self.add_member(import_info.alias, import_info.typ)

for name, interface_t in self.interfaces.items():
# can access interfaces in type position
self._helper.add_member(name, TYPE_T(interface_t))

# __eq__ is very strict on ModuleT - object equality! this is because we
# don't want to reason about where a module came from (i.e. input bundle,
# search path, symlinked vs normalized path, etc.)
Expand Down Expand Up @@ -355,6 +358,21 @@ def struct_defs(self):
def interface_defs(self):
return self._module.get_children(vy_ast.InterfaceDef)

@cached_property
def interfaces(self) -> dict[str, InterfaceT]:
ret = {}
for i in self.interface_defs:
assert i.name not in ret # precondition
ret[i.name] = i._metadata["interface_type"]

for i in self.import_stmts:
import_info = i._metadata["import_info"]
if isinstance(import_info.typ, InterfaceT):
assert import_info.alias not in ret # precondition
ret[import_info.alias] = import_info.typ

return ret

@property
def import_stmts(self):
return self._module.get_children((vy_ast.Import, vy_ast.ImportFrom))
Expand Down

0 comments on commit 5c30bf4

Please sign in to comment.