Skip to content

Commit d57132e

Browse files
authored
fix: use importlib.import_module instead of __import__ (#745)
`import_module` is newer, and provides a couple of advantages over a bare `__import__`: > The import_module() function acts as a simplifying wrapper around importlib.__import__(). This means all semantics of the function are derived from importlib.__import__(). The most important difference between these two functions is that import_module() returns the specified package or module (e.g. pkg.mod), while __import__() returns the top-level package or module (e.g. pkg). It's unclear if this will actually fix any problems in the real world, but there's no recent to use `__import__` at this point AFAICT.
1 parent c4a87a3 commit d57132e

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

src/taskgraph/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ def register(self):
148148
sys.path.insert(0, self.root_dir)
149149
register_path = self["taskgraph"].get("register")
150150
if register_path:
151-
find_object(register_path)(self)
151+
register = find_object(register_path)
152+
assert callable(register)
153+
register(self)
152154

153155
@property
154156
def vcs_root(self):

src/taskgraph/decision.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ def get_decision_parameters(graph_config, options):
263263
parameters["optimize_target_tasks"] = options["optimize_target_tasks"]
264264

265265
if "decision-parameters" in graph_config["taskgraph"]:
266-
find_object(graph_config["taskgraph"]["decision-parameters"])(
267-
graph_config, parameters
268-
)
266+
decision_params = find_object(graph_config["taskgraph"]["decision-parameters"])
267+
assert callable(decision_params)
268+
decision_params(graph_config, parameters)
269269

270270
if options.get("try_task_config_file"):
271271
task_config_file = os.path.abspath(options.get("try_task_config_file"))

src/taskgraph/generator.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ class Kind:
3737
config: Dict
3838
graph_config: GraphConfig
3939

40-
def _get_loader(self):
40+
def _get_loader(self) -> Callable:
4141
try:
42-
loader = self.config["loader"]
42+
loader_path = self.config["loader"]
4343
except KeyError:
44-
loader = "taskgraph.loader.default:loader"
45-
return find_object(loader)
44+
loader_path = "taskgraph.loader.default:loader"
45+
loader = find_object(loader_path)
46+
assert callable(loader)
47+
return loader
4648

4749
def load_tasks(self, parameters, loaded_tasks, write_artifacts):
4850
loader = self._get_loader()

src/taskgraph/util/python_path.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5+
import importlib
56
import inspect
67
import os
78

89

9-
def find_object(path):
10+
def find_object(path: str):
1011
"""
1112
Find a Python object given a path of the form <modulepath>:<objectpath>.
1213
Conceptually equivalent to
@@ -19,11 +20,10 @@ def find_object(modulepath, objectpath):
1920
raise ValueError(f'python path {path!r} does not have the form "module:object"')
2021

2122
modulepath, objectpath = path.split(":")
22-
obj = __import__(modulepath)
23-
for a in modulepath.split(".")[1:]:
24-
obj = getattr(obj, a)
23+
obj = importlib.import_module(modulepath)
2524
for a in objectpath.split("."):
2625
obj = getattr(obj, a)
26+
2727
return obj
2828

2929

0 commit comments

Comments
 (0)