Skip to content

Commit

Permalink
wip - get rid of resolved_path, use search_prefix instead
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Mar 2, 2024
1 parent b4429cf commit cbe8d5e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ class TopLevel(VyperNode):

class Module(TopLevel):
# metadata
__slots__ = ("path", "resolved_path", "source_id")
__slots__ = ("path", "search_prefix", "sha256sum", "source_id")

@contextlib.contextmanager
def namespace(self):
Expand Down
21 changes: 11 additions & 10 deletions vyper/ast/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def parse_to_ast_with_settings(
source_code: str,
source_id: int = 0,
module_path: Optional[str] = None,
resolved_path: Optional[str] = None,
search_prefix: Optional[str] = None,
add_fn_node: Optional[str] = None,
) -> tuple[Settings, vy_ast.Module]:
"""
Expand All @@ -44,9 +44,9 @@ def parse_to_ast_with_settings(
module_path: str, optional
The path of the source code
Corresponds to FileInput.path
resolved_path: str, optional
The resolved path of the source code
Corresponds to FileInput.resolved_path
search_prefix: str, optional
The search_prefix of the source code
Corresponds to FileInput.search_prefix
Returns
-------
Expand Down Expand Up @@ -77,7 +77,7 @@ def parse_to_ast_with_settings(
for_loop_annotations,
source_id,
module_path=module_path,
resolved_path=resolved_path,
search_prefix=search_prefix,
)

# postcondition: consumed all the for loop annotations
Expand Down Expand Up @@ -122,7 +122,7 @@ def annotate_python_ast(
for_loop_annotations: dict,
source_id: int = 0,
module_path: Optional[str] = None,
resolved_path: Optional[str] = None,
search_prefix: Optional[str] = None,
) -> python_ast.AST:
"""
Annotate and optimize a Python AST in preparation conversion to a Vyper AST.
Expand Down Expand Up @@ -152,7 +152,7 @@ def annotate_python_ast(
tokens,
source_id,
module_path=module_path,
resolved_path=resolved_path,
search_prefix=search_prefix,
)
visitor.visit(parsed_ast)

Expand All @@ -172,12 +172,12 @@ def __init__(
tokens: asttokens.ASTTokens,
source_id: int,
module_path: Optional[str] = None,
resolved_path: Optional[str] = None,
search_prefix: Optional[str] = None,
):
self._tokens = tokens
self._source_id = source_id
self._module_path = module_path
self._resolved_path = resolved_path
self._search_prefix = search_prefix
self._source_code = source_code
self._modification_offsets = modification_offsets
self._for_loop_annotations = for_loop_annotations
Expand Down Expand Up @@ -241,8 +241,9 @@ def _visit_docstring(self, node):

def visit_Module(self, node):
node.path = self._module_path
node.resolved_path = self._resolved_path
node.search_prefix = self._search_prefix
node.source_id = self._source_id
node.sha256sum = sha256(self._source_code)
return self._visit_docstring(node)

def visit_FunctionDef(self, node):
Expand Down
6 changes: 4 additions & 2 deletions vyper/compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def compile_code(
source_code: str,
contract_path: str | PathLike = UNKNOWN_CONTRACT_NAME,
source_id: int = -1,
resolved_path: PathLike | None = None,
search_prefix: PathLike | None = None,
*args,
**kwargs,
):
Expand All @@ -147,10 +147,12 @@ def compile_code(
"""
if isinstance(contract_path, str):
contract_path = Path(contract_path)
if search_prefix is None:
search_prefix = Path("")
file_input = FileInput(
source_id=source_id,
source_code=source_code,
path=contract_path,
resolved_path=resolved_path or contract_path, # type: ignore
search_prefix=search_prefix,
)
return compile_from_file_input(file_input, *args, **kwargs)
7 changes: 4 additions & 3 deletions vyper/compiler/input_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class CompilerInput:
# an input to the compiler, basically an abstraction for file contents
source_id: int
path: PathLike # the path that was asked for
search_prefix: PathLike # the specific prefix that was used from the search path

# resolved_path is the real path that was resolved to.
# mainly handy for debugging at this point
resolved_path: PathLike
@property
def resolved_path(self) -> PathLike:
return self.prefix / self.path


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion vyper/compiler/phases.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def __init__(
source_code=file_input,
source_id=-1,
path=DEFAULT_CONTRACT_PATH,
resolved_path=DEFAULT_CONTRACT_PATH,
search_path=Path(),
)
self.file_input = file_input
self.storage_layout_override = storage_layout
Expand Down

0 comments on commit cbe8d5e

Please sign in to comment.