Skip to content

fix(types): parameterize Span's context manager and annotate otel span processor - #847

Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit into
braintrustdata:mainfrom
NorthIsUp:fix/span-context-manager-and-otel-annotations
Oct 5, 2026
Merged

Abhijeet Prasad (AbhiPrasad) merged 1 commit into
braintrustdata:mainfrom
NorthIsUp:fix/span-context-manager-and-otel-annotations

Conversation

@NorthIsUp

Copy link
Copy Markdown

Consumers that run pyright in strict mode see reportUnknown* warnings at every start_span call and every with ... as span block, because:

  • Span subclasses a bare contextlib.AbstractContextManager, so __enter__ returns Unknown and span is untyped inside the with block. Now AbstractContextManager["Span"]; both implementations already return self.
  • start_span's parent: str | dict | None uses a bare dict. Now dict[str, str], which is what extract_trace_context builds (its return type is narrowed to match).
  • add_braintrust_span_processor leaves tracer_provider and custom_filter unannotated. Now TracerProvider and Callable[[ReadableSpan], bool | None], imported under TYPE_CHECKING so the optional otel dependency stays optional.
  • _MemoryBackgroundLogger.pop gets its list[dict[str, Any]] return type (what merge_row_batch returns).

No runtime change. Adds type_tests/test_span_types.py; its assert_type(span, Span) fails on main ("expected Span but received Unknown") and passes here under both pyright and mypy. Repo-root pyright on the two touched files goes from 239 to 228 errors and 656 to 625 warnings.

Checks run: nox test_types steps (pyright + mypy + pytest on type_tests), pytest src/braintrust/test_logger.py, ruff 0.15.21 check and format.

…n processor

`Span` subclassed a bare `contextlib.AbstractContextManager`, so
`with start_span(...) as span` typed `span` as Unknown under pyright strict.
`start_span`'s `parent` used a bare `dict`, and `add_braintrust_span_processor`
left `tracer_provider` and `custom_filter` unannotated, which made both
functions partially unknown to consumers that ship strict type checks.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) merged commit 4365035 into braintrustdata:main Oct 5, 2026
83 checks passed
@AbhiPrasad

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants