Coverage for src / template_python / compat.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-14 20:19 +0000

1""" 

2Compatibility abstractions for optional dependencies. 

3 

4This module centralizes fallback logic for safely importing optional dependencies 

5like ``opentelemetry``. It provides standardized access points for these modules, 

6avoiding scattered ``try-except`` blocks across the codebase. Maintainers should 

7import optional dependencies from this module rather than attempting direct 

8imports elsewhere. 

9""" 

10 

11from __future__ import annotations 

12 

13import types 

14from typing import Annotated 

15 

16try: 

17 from opentelemetry import ( 

18 trace as _opentelemetry_trace, # type: ignore[import-not-found] 

19 ) 

20except ImportError: 

21 _opentelemetry_trace = None # type: ignore[assignment] 

22 

23__all__ = ["opentelemetry_trace"] 

24 

25opentelemetry_trace: Annotated[ 

26 types.ModuleType | None, 

27 "Enables distributed tracing integration. Used for tracing execution paths " 

28 "when OpenTelemetry is present. Provides the ``opentelemetry.trace`` module " 

29 "or ``None``.", 

30] = _opentelemetry_trace