sdk: tighten ReadableSpan.attributes to non-Optional Mapping (#4569)#5183
Open
WatchTree-19 wants to merge 2 commits into
Open
sdk: tighten ReadableSpan.attributes to non-Optional Mapping (#4569)#5183WatchTree-19 wants to merge 2 commits into
WatchTree-19 wants to merge 2 commits into
Conversation
MikeGoldsmith
requested changes
May 11, 2026
Member
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Thanks @WatchTree-19 - left some feedback on things we need to tidy up
|
|
||
| ## Unreleased | ||
|
|
||
| - `opentelemetry-sdk`: tighten `ReadableSpan.attributes` annotation to non-Optional `Mapping` so callers don't need `assert ... is not None` boilerplate; runtime guarantee was already in place via `MappingProxyType(self._attributes or {})` |
Member
There was a problem hiding this comment.
Looks like you got some other changelog entries in here - please can you clean this up?
| @property | ||
| def attributes(self) -> types.Attributes: | ||
| def attributes(self) -> Mapping[str, types.AttributeValue]: | ||
| # The implementation always returns a MappingProxyType, never None, |
Member
There was a problem hiding this comment.
This is very verbose, can we tighten this up?
|
|
||
| @property | ||
| def attributes(self) -> types.Attributes: | ||
| def attributes(self) -> Mapping[str, types.AttributeValue]: |
Member
There was a problem hiding this comment.
Do / should we update the API definition too?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
ReadableSpan.attributesis annotated astypes.Attributes, which resolves toOptional[Mapping[str, AttributeValue]]. The implementation always returnsMappingProxyType(self._attributes or {})- theor {}fallback ensures we never return None at runtime.Pyright / Pylance flags
Object of type "None" is not subscriptableonspan.attributes["key"], forcingassert span.attributes is not Noneboilerplate at every call site.This PR tightens the return annotation on
ReadableSpan.attributesto the non-OptionalMapping[str, types.AttributeValue]. Implementation unchanged.Mappingis already imported in the file.Scope:
ReadableSpan.attributesonly. Inherited bySpanand_Span, no further changes needed.Event.attributesandLink.attributesreturnself._attributesdirectly (which can be None) and stay Optional.types.Attributesglobal 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
How Has This Been Tested?
python -m astDoes This PR Require a Contrib Repo Change?
Checklist: