Skip to content

Commit d80c3f2

Browse files
add missing type annotations and clean up type ignore usage
1 parent 1462692 commit d80c3f2

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

src/electripy/observability/ai_telemetry/adapters.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from datetime import UTC, datetime
2424
from hashlib import sha256
2525
from pathlib import Path
26+
from types import TracebackType
2627
from typing import Any
2728

2829
from electripy.core.logging import get_logger
@@ -147,7 +148,12 @@ def __enter__(self) -> TelemetryContext:
147148
self._sink.emit_event(event)
148149
return ctx
149150

150-
def __exit__(self, exc_type, exc, tb) -> None: # type: ignore[override]
151+
def __exit__(
152+
self,
153+
exc_type: type[BaseException] | None,
154+
exc: BaseException | None,
155+
tb: TracebackType | None,
156+
) -> bool | None:
151157
end = datetime.now(tz=UTC)
152158
ctx = self._get_current()
153159
if self._start_time is not None and ctx is not None:
@@ -166,11 +172,17 @@ def __exit__(self, exc_type, exc, tb) -> None: # type: ignore[override]
166172
self._set_current(self._previous_ctx)
167173
return None
168174

169-
async def __aenter__(self) -> TelemetryContext: # type: ignore[override]
175+
async def __aenter__(self) -> TelemetryContext:
170176
return self.__enter__()
171177

172-
async def __aexit__(self, exc_type, exc, tb) -> None: # type: ignore[override]
178+
async def __aexit__(
179+
self,
180+
exc_type: type[BaseException] | None,
181+
exc: BaseException | None,
182+
tb: TracebackType | None,
183+
) -> bool | None:
173184
self.__exit__(exc_type, exc, tb)
185+
return None
174186

175187

176188
@dataclass(slots=True)
@@ -351,11 +363,11 @@ def span(
351363

352364

353365
try: # Optional OpenTelemetry integration
354-
from opentelemetry import trace as _otel_trace # type: ignore[import]
366+
from opentelemetry import trace as _otel_trace # type: ignore[import-not-found]
355367

356368
_HAS_OTEL = True
357369
except Exception: # pragma: no cover - depends on optional extra
358-
_otel_trace: Any
370+
_otel_trace = None
359371
_HAS_OTEL = False
360372

361373

@@ -381,7 +393,7 @@ def __post_init__(self) -> None:
381393
def emit_event(self, event: TelemetryEvent) -> None:
382394
span = _otel_trace.get_current_span()
383395
attrs = _sanitize_attributes(dict(event.attributes))
384-
span.add_event( # type: ignore[union-attr]
396+
span.add_event(
385397
name=event.name,
386398
attributes={**attrs, "severity": event.severity.value},
387399
timestamp=int(event.timestamp.timestamp() * 1_000_000_000),
@@ -459,7 +471,7 @@ def span(
459471
class _OtelSpan(SpanContextManager):
460472
def __init__(self, adapter: OpenTelemetryAdapter) -> None:
461473
self._adapter = adapter
462-
self._cm = adapter._tracer.start_as_current_span(name) # type: ignore[union-attr]
474+
self._cm = adapter._tracer.start_as_current_span(name)
463475

464476
def __enter__(self) -> TelemetryContext:
465477
self._cm.__enter__()
@@ -474,11 +486,16 @@ def __enter__(self) -> TelemetryContext:
474486
tags={},
475487
)
476488

477-
def __exit__(self, exc_type, exc, tb) -> None: # type: ignore[override]
489+
def __exit__(
490+
self,
491+
exc_type: type[BaseException] | None,
492+
exc: BaseException | None,
493+
tb: TracebackType | None,
494+
) -> bool | None:
478495
self._cm.__exit__(exc_type, exc, tb)
479496
return None
480497

481-
async def __aenter__(self) -> TelemetryContext: # type: ignore[override]
498+
async def __aenter__(self) -> TelemetryContext:
482499
self.__enter__()
483500
return ctx or TelemetryContext(
484501
trace_id="",
@@ -491,8 +508,14 @@ async def __aenter__(self) -> TelemetryContext: # type: ignore[override]
491508
tags={},
492509
)
493510

494-
async def __aexit__(self, exc_type, exc, tb) -> None: # type: ignore[override]
511+
async def __aexit__(
512+
self,
513+
exc_type: type[BaseException] | None,
514+
exc: BaseException | None,
515+
tb: TracebackType | None,
516+
) -> bool | None:
495517
self.__exit__(exc_type, exc, tb)
518+
return None
496519

497520
return _OtelSpan(self)
498521

0 commit comments

Comments
 (0)