From 2e101d1175423f7edd8da16767e4df43501be634 Mon Sep 17 00:00:00 2001 From: "Gabriele N. Tornetta" Date: Wed, 5 Mar 2025 14:11:18 +0000 Subject: [PATCH] chore(internal): prevent import of importlib.abc (#12641) We prevent the unnecessary import of importlib.abc in module.py since it is just used for type checking. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) --- ddtrace/internal/module.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ddtrace/internal/module.py b/ddtrace/internal/module.py index e5d8e66e023..d510e6ec931 100644 --- a/ddtrace/internal/module.py +++ b/ddtrace/internal/module.py @@ -1,7 +1,6 @@ import abc from collections import defaultdict from importlib._bootstrap import _init_module_attrs -from importlib.abc import Loader from importlib.machinery import ModuleSpec from importlib.util import find_spec from pathlib import Path @@ -17,6 +16,10 @@ from ddtrace.internal.wrapping.context import WrappingContext +if t.TYPE_CHECKING: + from importlib.abc import Loader + + ModuleHookType = t.Callable[[ModuleType], None] TransformerType = t.Callable[[CodeType, ModuleType], CodeType] PreExecHookType = t.Callable[[t.Any, ModuleType], None] @@ -139,7 +142,7 @@ def _resolve(path: Path) -> t.Optional[Path]: # https://github.com/GrahamDumpleton/wrapt/blob/df0e62c2740143cceb6cafea4c306dae1c559ef8/src/wrapt/importer.py -def find_loader(fullname: str) -> t.Optional[Loader]: +def find_loader(fullname: str) -> t.Optional["Loader"]: return getattr(find_spec(fullname), "loader", None) @@ -152,7 +155,7 @@ def is_namespace_spec(spec: ModuleSpec) -> bool: class _ImportHookChainedLoader: - def __init__(self, loader: t.Optional[Loader], spec: t.Optional[ModuleSpec] = None) -> None: + def __init__(self, loader: t.Optional["Loader"], spec: t.Optional[ModuleSpec] = None) -> None: self.loader = loader self.spec = spec @@ -356,7 +359,7 @@ def after_import(self, module: ModuleType) -> None: def transform(self, code: CodeType, _module: ModuleType) -> CodeType: return code - def find_module(self, fullname: str, path: t.Optional[str] = None) -> t.Optional[Loader]: + def find_module(self, fullname: str, path: t.Optional[str] = None) -> t.Optional["Loader"]: if fullname in self._finding: return None @@ -374,7 +377,7 @@ def find_module(self, fullname: str, path: t.Optional[str] = None) -> t.Optional loader.add_callback(type(self), self.after_import) loader.add_transformer(type(self), self.transform) - return t.cast(Loader, loader) + return t.cast("Loader", loader) finally: self._finding.remove(fullname) @@ -402,7 +405,7 @@ def find_spec( loader = getattr(spec, "loader", None) if not isinstance(loader, _ImportHookChainedLoader): - spec.loader = t.cast(Loader, _ImportHookChainedLoader(loader, spec)) + spec.loader = t.cast("Loader", _ImportHookChainedLoader(loader, spec)) t.cast(_ImportHookChainedLoader, spec.loader).add_callback(type(self), self.after_import) t.cast(_ImportHookChainedLoader, spec.loader).add_transformer(type(self), self.transform)