Skip to content

Commit 1d1eb85

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 1fdbb6c commit 1d1eb85

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/taskgraph/util/python_path.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
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
8+
from typing import Callable
79

810

9-
def find_object(path):
11+
def find_object(path: str) -> Callable:
1012
"""
1113
Find a Python object given a path of the form <modulepath>:<objectpath>.
1214
Conceptually equivalent to
@@ -19,11 +21,11 @@ def find_object(modulepath, objectpath):
1921
raise ValueError(f'python path {path!r} does not have the form "module:object"')
2022

2123
modulepath, objectpath = path.split(":")
22-
obj = __import__(modulepath)
23-
for a in modulepath.split(".")[1:]:
24-
obj = getattr(obj, a)
24+
obj = importlib.import_module(modulepath)
2525
for a in objectpath.split("."):
2626
obj = getattr(obj, a)
27+
28+
assert callable(obj)
2729
return obj
2830

2931

0 commit comments

Comments
 (0)