Skip to content

Commit

Permalink
chore(internal): prevent import of importlib.abc (#12641)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
P403n1x87 authored Mar 5, 2025
1 parent 3d79bf6 commit 2e101d1
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ddtrace/internal/module.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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)


Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2e101d1

Please sign in to comment.