Skip to content

Commit 87835fc

Browse files
committed
fix: use importlib.import_module instead of __import__
`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 ab0b9f7 commit 87835fc

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

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)