From 1d1eb8564db14e5c757a8dfb1768fcaf4cd47148 Mon Sep 17 00:00:00 2001 From: Ben Hearsum Date: Thu, 14 Aug 2025 11:18:59 -0400 Subject: [PATCH] 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. --- src/taskgraph/util/python_path.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/taskgraph/util/python_path.py b/src/taskgraph/util/python_path.py index ff7e255e5..6119e0ae7 100644 --- a/src/taskgraph/util/python_path.py +++ b/src/taskgraph/util/python_path.py @@ -2,11 +2,13 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +import importlib import inspect import os +from typing import Callable -def find_object(path): +def find_object(path: str) -> Callable: """ Find a Python object given a path of the form :. Conceptually equivalent to @@ -19,11 +21,11 @@ def find_object(modulepath, objectpath): raise ValueError(f'python path {path!r} does not have the form "module:object"') modulepath, objectpath = path.split(":") - obj = __import__(modulepath) - for a in modulepath.split(".")[1:]: - obj = getattr(obj, a) + obj = importlib.import_module(modulepath) for a in objectpath.split("."): obj = getattr(obj, a) + + assert callable(obj) return obj