CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Python agent-frameworkagent-harnessagent-runtimeagentic-aiai-agentsai-frameworkanthropicharnessharness-engineeringharness-frameworklanggraphlanggraph-alternativellmmcpopenaipi-agentpi-agent-corepythonpython-agent
refactor(tracing): lazy package init so schema imports without opentelemetry
xf gong635a50df30c5
diff --git a/cubepi/tracing/__init__.py b/cubepi/tracing/__init__.pyindex 60a9c8b..f406b63 100644--- a/cubepi/tracing/__init__.py+++ b/cubepi/tracing/__init__.py@@ -5,23 +5,17 @@ spans will flow to whichever :class:`~opentelemetry.sdk.trace.export.SpanExporteimplementations you configure (e.g. :class:`JsonlSpanExporter` for localfiles, or the OTLP exporter shipped with ``opentelemetry-exporter-otlp-proto-http``).-This module requires the optional ``opentelemetry-sdk`` dependency; install-with ``pip install cubepi[tracing]``.+The heavyweight members (:class:`Tracer`, :class:`Meter`,+:class:`JsonlSpanExporter`, :func:`tracing_context`, ``SCHEMA_URL``) require+the optional ``opentelemetry-sdk`` dependency; install with+``pip install cubepi[tracing]``. They are imported lazily so that pure-metadata+submodules such as :mod:`cubepi.tracing.schema` remain importable without the+SDK (used by the ``cubepi trace`` CLI)."""-try: # noqa: SIM105 — explicit ImportError handling for the optional extra.- import opentelemetry.sdk.trace # noqa: F401-except ImportError as exc: # pragma: no cover — exercised only without the extra.- raise ImportError(- "cubepi.tracing requires the 'opentelemetry-sdk' package. "- "Install it via: pip install cubepi[tracing]"- ) from exc+from __future__ import annotations-from cubepi.tracing.context import tracing_context-from cubepi.tracing.exporters import JsonlSpanExporter-from cubepi.tracing.meter import Meter-from cubepi.tracing.schema import SCHEMA_URL-from cubepi.tracing.tracer import Tracer+from typing import TYPE_CHECKING, Any__all__ = ["JsonlSpanExporter",@@ -30,3 +24,42 @@ __all__ = ["Tracer","tracing_context",]++if TYPE_CHECKING: # pragma: no cover+ from cubepi.tracing.context import tracing_context+ from cubepi.tracing.exporters import JsonlSpanExporter+ from cubepi.tracing.meter import Meter+ from cubepi.tracing.schema import SCHEMA_URL+ from cubepi.tracing.tracer import Tracer++_LAZY = {+ "tracing_context": ("cubepi.tracing.context", "tracing_context"),+ "JsonlSpanExporter": ("cubepi.tracing.exporters", "JsonlSpanExporter"),+ "Meter": ("cubepi.tracing.meter", "Meter"),+ "SCHEMA_URL": ("cubepi.tracing.schema", "SCHEMA_URL"),+ "Tracer": ("cubepi.tracing.tracer", "Tracer"),+}+++def __getattr__(name: str) -> Any:+ try:+ module_name, attr = _LAZY[name]+ except KeyError:+ raise AttributeError(+ f"module {__name__!r} has no attribute {name!r}"+ ) from None+ try:+ import opentelemetry.sdk.trace # noqa: F401+ except ImportError as exc: # pragma: no cover — only without the extra.+ raise ImportError(+ "cubepi.tracing requires the 'opentelemetry-sdk' package. "+ "Install it via: pip install cubepi[tracing]"+ ) from exc+ import importlib++ module = importlib.import_module(module_name)+ return getattr(module, attr)+++def __dir__() -> list[str]:+ return sorted(__all__)diff --git a/tests/tracing/test_lazy_import.py b/tests/tracing/test_lazy_import.pynew file mode 100644index 0000000..d0a744c--- /dev/null+++ b/tests/tracing/test_lazy_import.py@@ -0,0 +1,27 @@+"""schema constants must be importable without the opentelemetry SDK."""+from __future__ import annotations++import subprocess+import sys+++def test_schema_importable_without_opentelemetry():+ # Run in a subprocess with opentelemetry hidden, so the lazy __init__+ # is exercised exactly as a trace-cli-only install would see it.+ code = (+ "import builtins, sys\n"+ "real_import = builtins.__import__\n"+ "def fake(name, *a, **k):\n"+ " if name == 'opentelemetry' or name.startswith('opentelemetry.'):\n"+ " raise ImportError('hidden')\n"+ " return real_import(name, *a, **k)\n"+ "builtins.__import__ = fake\n"+ "from cubepi.tracing import schema\n"+ "assert schema.CUBEPI_RUN_ID == 'cubepi.run_id'\n"+ "print('ok')\n"+ )+ result = subprocess.run(+ [sys.executable, "-c", code], capture_output=True, text=True+ )+ assert result.returncode == 0, result.stderr+ assert "ok" in result.stdout