Skip to content

Commit

Permalink
remove .name from module ast
Browse files Browse the repository at this point in the history
it could be different, depending on its alias at import time. just
remove it, so there is a 1:1 correspondence between module ast and
source code.
  • Loading branch information
charles-cooper committed Nov 11, 2023
1 parent 19404e9 commit 21a04c3
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 32 deletions.
11 changes: 1 addition & 10 deletions vyper/ast/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ def __init__(
tokens: asttokens.ASTTokens,
source_id: int,
module_path: Optional[str] = None,
module_name: Optional[str] = None,
):
self._tokens = tokens
self._source_id = source_id
self._module_path = module_path
self._module_name = module_name
self._source_code: str = source_code
self.counter: int = 0
self._modification_offsets = {}
Expand Down Expand Up @@ -85,7 +83,6 @@ def _visit_docstring(self, node):
return node

def visit_Module(self, node):
node.name = self._module_name
node.path = self._module_path
node.source_id = self._source_id
return self._visit_docstring(node)
Expand Down Expand Up @@ -255,7 +252,6 @@ def annotate_python_ast(
modification_offsets: Optional[ModificationOffsets] = None,
source_id: int = 0,
module_path: Optional[str] = None,
module_name: Optional[str] = None,
) -> python_ast.AST:
"""
Annotate and optimize a Python AST in preparation conversion to a Vyper AST.
Expand All @@ -276,12 +272,7 @@ def annotate_python_ast(

tokens = asttokens.ASTTokens(source_code, tree=cast(Optional[python_ast.Module], parsed_ast))
visitor = AnnotatingVisitor(
source_code,
modification_offsets,
tokens,
source_id,
module_name=module_name,
module_path=module_path,
source_code, modification_offsets, tokens, source_id, module_path=module_path
)
visitor.visit(parsed_ast)

Expand Down
10 changes: 1 addition & 9 deletions vyper/ast/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def parse_to_ast_with_settings(
source_code: str,
source_id: int = 0,
module_path: Optional[str] = None,
module_name: Optional[str] = None,
add_fn_node: Optional[str] = None,
) -> tuple[Settings, vy_ast.Module]:
"""
Expand Down Expand Up @@ -55,14 +54,7 @@ def parse_to_ast_with_settings(
fn_node.args = python_ast.arguments(defaults=[])
py_ast.body = [fn_node]

annotate_python_ast(
py_ast,
source_code,
class_types,
source_id,
module_name=module_name,
module_path=module_path,
)
annotate_python_ast(py_ast, source_code, class_types, source_id, module_path=module_path)

# Convert to Vyper AST.
module = vy_ast.get_node(py_ast)
Expand Down
5 changes: 1 addition & 4 deletions vyper/compiler/phases.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ def __init__(
@cached_property
def _generate_ast(self):
settings, ast = vy_ast.parse_to_ast_with_settings(
self.source_code,
self.source_id,
module_path=str(self.contract_path),
module_name=str(self.contract_path), # something better than "<main>"
self.source_code, self.source_id, module_path=str(self.contract_path)
)

# validate the compiler settings
Expand Down
5 changes: 2 additions & 3 deletions vyper/semantics/analysis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ class ImportGraph:

def push_path(self, module_ast: vy_ast.Module):
if module_ast in self._path:
raise ImportCycle(
msg=" imports ".join(f'"{t.name}" (located at {t.path})' for t in self._path)
)
cycle = self._path + [module_ast]
raise ImportCycle(" imports ".join(f'"{t.path}"' for t in cycle))

if len(self._path) > 0:
parent = self._graph.setdefault(self._path[-1], [])
Expand Down
6 changes: 3 additions & 3 deletions vyper/semantics/analysis/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def analyze_call_graph(self):
def _ast_from_file(cls, file: FileInput, alias: str):
if file.source_id not in cls._ast_of:
cls._ast_of[file.source_id] = vy_ast.parse_to_ast(
file.source_code, module_path=str(file.path), module_name=alias
file.source_code, module_path=str(file.path)
)

return cls._ast_of[file.source_id]
Expand Down Expand Up @@ -464,5 +464,5 @@ def _load_builtin_import(level: int, module_str: str) -> InterfaceT:
raise ModuleNotFoundError(f"Not a builtin: {module_str}") from None

# TODO: it might be good to cache this computation
interface_ast = vy_ast.parse_to_ast(file.source_code, module_path=path, module_name=module_str)
return InterfaceT.from_Module(interface_ast)
interface_ast = vy_ast.parse_to_ast(file.source_code, module_path=path)
return InterfaceT.from_Module(interface_ast, name=module_str)
2 changes: 1 addition & 1 deletion vyper/semantics/types/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ModuleT(VyperType):
def __init__(self, module: vy_ast.Module, name: Optional[str] = None):
self._module = module

self._id = name or module.name
self._id = name or module.path

# compute the interface, note this has the side effect of checking
# for function collisions
Expand Down
2 changes: 0 additions & 2 deletions vyper/semantics/types/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,6 @@ def from_Module(cls, node: vy_ast.Module, name: Optional[str] = None) -> "Interf
"""
members, events = _get_module_definitions(node)

name = name or node.name

return cls(name, members, events)

@classmethod
Expand Down

0 comments on commit 21a04c3

Please sign in to comment.