Skip to content

Commit

Permalink
Import importlib.util explicitly (#1222)
Browse files Browse the repository at this point in the history
Co-authored-by: Ofek Lev <[email protected]>
  • Loading branch information
krassowski and ofek authored Jan 25, 2024
1 parent d167faa commit 41e13ca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions backend/src/hatchling/plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@


def load_plugin_from_script(path: str, script_name: str, plugin_class: type[T], plugin_id: str) -> type[T]:
import importlib
from importlib.util import module_from_spec, spec_from_file_location

spec = importlib.util.spec_from_file_location(script_name, path) # type: ignore
module = importlib.util.module_from_spec(spec) # type: ignore
spec.loader.exec_module(module)
spec = spec_from_file_location(script_name, path)
module = module_from_spec(spec) # type: ignore
spec.loader.exec_module(module) # type: ignore

plugin_finder = f'get_{plugin_id}'
names = dir(module)
Expand Down
8 changes: 4 additions & 4 deletions backend/src/hatchling/version/source/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class CodeSource(VersionSourceInterface):
PLUGIN_NAME = 'code'

def get_version_data(self) -> dict:
import importlib
import sys
from importlib.util import module_from_spec, spec_from_file_location

relative_path = self.config.get('path')
if not relative_path:
Expand Down Expand Up @@ -44,13 +44,13 @@ def get_version_data(self) -> dict:

absolute_search_paths.append(os.path.normpath(os.path.join(self.root, search_path)))

spec = importlib.util.spec_from_file_location(os.path.splitext(path)[0], path) # type: ignore
module = importlib.util.module_from_spec(spec) # type: ignore
spec = spec_from_file_location(os.path.splitext(path)[0], path)
module = module_from_spec(spec) # type: ignore

old_search_paths = list(sys.path)
try:
sys.path[:] = [*absolute_search_paths, *old_search_paths]
spec.loader.exec_module(module)
spec.loader.exec_module(module) # type: ignore
finally:
sys.path[:] = old_search_paths

Expand Down
8 changes: 4 additions & 4 deletions src/hatch/plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
def load_plugin_from_script(
path: str, script_name: str, plugin_class: type[EnvironmentCollectorInterface], plugin_id: str
) -> type[EnvironmentCollectorInterface]:
import importlib
from importlib.util import module_from_spec, spec_from_file_location

spec = importlib.util.spec_from_file_location(script_name, path) # type: ignore
module = importlib.util.module_from_spec(spec) # type: ignore
spec.loader.exec_module(module)
spec = spec_from_file_location(script_name, path)
module = module_from_spec(spec) # type: ignore
spec.loader.exec_module(module) # type: ignore

plugin_finder = f'get_{plugin_id}'
names = dir(module)
Expand Down

0 comments on commit 41e13ca

Please sign in to comment.