sdk: tighten ReadableSpan.attributes to non-Optional Mapping (#4569)#5183
Open
WatchTree-19 wants to merge 2 commits intoopen-telemetry:mainfrom
Open
sdk: tighten ReadableSpan.attributes to non-Optional Mapping (#4569)#5183WatchTree-19 wants to merge 2 commits intoopen-telemetry:mainfrom
WatchTree-19 wants to merge 2 commits intoopen-telemetry:mainfrom
Conversation
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: