From 21a04c34a02f0ac656f14616a0027294552aa4d6 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Fri, 10 Nov 2023 20:52:05 -0500 Subject: [PATCH] remove `.name` from module ast 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. --- vyper/ast/annotation.py | 11 +---------- vyper/ast/utils.py | 10 +--------- vyper/compiler/phases.py | 5 +---- vyper/semantics/analysis/base.py | 5 ++--- vyper/semantics/analysis/module.py | 6 +++--- vyper/semantics/types/module.py | 2 +- vyper/semantics/types/user.py | 2 -- 7 files changed, 9 insertions(+), 32 deletions(-) diff --git a/vyper/ast/annotation.py b/vyper/ast/annotation.py index e21f3ae5a2..dad164b1bb 100644 --- a/vyper/ast/annotation.py +++ b/vyper/ast/annotation.py @@ -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 = {} @@ -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) @@ -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. @@ -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) diff --git a/vyper/ast/utils.py b/vyper/ast/utils.py index 4dde196cf6..06eda716ca 100644 --- a/vyper/ast/utils.py +++ b/vyper/ast/utils.py @@ -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]: """ @@ -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) diff --git a/vyper/compiler/phases.py b/vyper/compiler/phases.py index 25581531eb..b4abd26978 100644 --- a/vyper/compiler/phases.py +++ b/vyper/compiler/phases.py @@ -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 "
" + self.source_code, self.source_id, module_path=str(self.contract_path) ) # validate the compiler settings diff --git a/vyper/semantics/analysis/base.py b/vyper/semantics/analysis/base.py index c39342384b..b47d2e6feb 100644 --- a/vyper/semantics/analysis/base.py +++ b/vyper/semantics/analysis/base.py @@ -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], []) diff --git a/vyper/semantics/analysis/module.py b/vyper/semantics/analysis/module.py index 8999084373..d47a6e6045 100644 --- a/vyper/semantics/analysis/module.py +++ b/vyper/semantics/analysis/module.py @@ -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] @@ -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) diff --git a/vyper/semantics/types/module.py b/vyper/semantics/types/module.py index b3e93fdd1b..f2f9eb6422 100644 --- a/vyper/semantics/types/module.py +++ b/vyper/semantics/types/module.py @@ -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 diff --git a/vyper/semantics/types/user.py b/vyper/semantics/types/user.py index e50ae7a7a2..57456dae94 100644 --- a/vyper/semantics/types/user.py +++ b/vyper/semantics/types/user.py @@ -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