Skip to content

Commit 828d3ea

Browse files
committed
update remmaping logic
1 parent b5e3825 commit 828d3ea

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

vyper/semantics/analysis/imports.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,26 @@ def _is_builtin(module_str):
283283

284284
_builtins_cache: dict[PathLike, tuple[CompilerInput, vy_ast.Module]] = {}
285285

286+
# builtin import path -> prefix for removal, package, suffix
287+
BUILTIN_MODULE_RULES = {
288+
"ethereum.ercs": ("ethereum.ercs", vyper.builtins.interfaces.__package__, ".vyi"),
289+
"math": ("", vyper.builtins.stdlib.__package__, ".vy"),
290+
}
291+
292+
293+
def _get_builtin_prefix(module_str: str):
294+
for prefix in BUILTIN_PREFIXES:
295+
if module_str.startswith(prefix):
296+
return prefix
297+
return None
298+
286299

287300
def _load_builtin_import(level: int, module_str: str) -> tuple[CompilerInput, vy_ast.Module]:
288-
if not _is_builtin(module_str): # pragma: nocover
301+
if not _is_builtin(module_str):
289302
raise CompilerPanic("unreachable!")
290303

304+
assert level == 0, "builtin imports are absolute"
305+
291306
builtins_path = vyper.builtins.__path__[0]
292307
# hygiene: convert to relpath to avoid leaking user directory info
293308
# (note Path.relative_to cannot handle absolute to relative path
@@ -298,18 +313,15 @@ def _load_builtin_import(level: int, module_str: str) -> tuple[CompilerInput, vy
298313
# generate an input bundle just because it knows how to build paths.
299314
input_bundle = FilesystemInputBundle([search_path])
300315

301-
# remap builtins directory --
302-
# ethereum/ercs => vyper/builtins/interfaces
303-
is_erc = module_str.startswith("ethereum.ercs")
304-
remapped_module = module_str
305-
if is_erc:
306-
remapped_module = remapped_module.removeprefix("ethereum.ercs")
307-
remapped_module = vyper.builtins.interfaces.__package__ + remapped_module
308-
else:
309-
remapped_module = vyper.builtins.stdlib.__package__ + "." + remapped_module
316+
module_prefix = _get_builtin_prefix(module_str)
317+
assert module_prefix is not None
318+
319+
remove_prefix, target_package, suffix = BUILTIN_MODULE_RULES[module_prefix]
320+
base_name = module_str.removeprefix(remove_prefix + ".")
321+
remapped_module = f"{target_package}.{base_name}"
310322

311323
path = _import_to_path(level, remapped_module)
312-
path = path.with_suffix(".vy") if not is_erc else path.with_suffix(".vyi")
324+
path = path.with_suffix(suffix)
313325

314326
# builtins are globally the same, so we can safely cache them
315327
# (it is also *correct* to cache them, so that types defined in builtins

0 commit comments

Comments
 (0)