Skip to content

sdk: tighten ReadableSpan.attributes to non-Optional Mapping (#4569)#5183

Open
WatchTree-19 wants to merge 2 commits intoopen-telemetry:mainfrom
WatchTree-19:fix/readable-span-attributes-non-optional
Open

sdk: tighten ReadableSpan.attributes to non-Optional Mapping (#4569)#5183
WatchTree-19 wants to merge 2 commits intoopen-telemetry:mainfrom
WatchTree-19:fix/readable-span-attributes-non-optional

Conversation

@WatchTree-19
Copy link
Copy Markdown

Description

ReadableSpan.attributes is annotated as types.Attributes, which resolves to Optional[Mapping[str, AttributeValue]]. The implementation always returns MappingProxyType(self._attributes or {}) - the or {} fallback ensures we never return None at runtime.

Pyright / Pylance flags Object of type "None" is not subscriptable on span.attributes["key"], forcing assert span.attributes is not None boilerplate at every call site.

This PR tightens the return annotation on ReadableSpan.attributes to the non-Optional Mapping[str, types.AttributeValue]. Implementation unchanged. Mapping is already imported in the file.

Scope:

  • ReadableSpan.attributes only. Inherited by Span and _Span, no further changes needed.
  • Event.attributes and Link.attributes return self._attributes directly (which can be None) and stay Optional.
  • types.Attributes global alias is unchanged for the same reason.

Per the comment thread on #4569 from @exekis and @tekumara, the implementation has never returned None from ReadableSpan.attributes, so this is type-system tightening rather than a behaviour change. Technically a breaking change for any caller explicitly handling None on the return; in practice there shouldn't be any.

Fixes #4569

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • patched file parses cleanly via python -m ast
  • existing call sites are unchanged, only the return-type annotation is narrower
  • have not run the project test suite locally; happy to chase a green CI here if anything trips

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@WatchTree-19 WatchTree-19 requested a review from a team as a code owner May 7, 2026 14:57
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented May 7, 2026

CLA Signed
The committers listed above are authorized under a signed CLA.

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Object of type "None" is not subscriptable

1 participant