Skip to content

Hardfork doesn't respect base when importing sub-modules #1373

@SamWilsn

Description

@SamWilsn

What was wrong?

The Hardfork class allows specifying a base path to search for modules:

def discover(cls: Type[H], base: Optional[PurePath] = None) -> List[H]:

It also provides methods for importing submodules of discovered forks:

def import_module(self) -> ModuleType:
"""
Return the module containing this specification.
"""
return self.mod
def module(self, name: str) -> Any:
"""
Import if necessary, and return the given module belonging to this hard
fork.
"""
return importlib.import_module(self.mod.__name__ + "." + name)
def iter_modules(self) -> Iterator[ModuleInfo]:
"""
Iterate through the (sub-)modules describing this hardfork.
"""
if self.path is None:
raise ValueError(f"cannot walk {self.name}, path is None")
return pkgutil.iter_modules(self.path, self.name + ".")
def walk_packages(self) -> Iterator[ModuleInfo]:
"""
Iterate recursively through the (sub-)modules describing this hardfork.
"""
if self.path is None:
raise ValueError(f"cannot walk {self.name}, path is None")
return pkgutil.walk_packages([self.path], self.name + ".")

Unfortunately, Hardfork doesn't consider base when importing those submodules. It just relies on the default import machinery.

How can it be fixed?

Use spec_from_file_location and module_from_spec when loading submodules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-toolArea: toolingC-bugCategory: this is a bug, deviation, or other problem.P-low

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions