sdk: tighten ReadableSpan.attributes to non-Optional Mapping (#4569) - #5183
sdk: tighten ReadableSpan.attributes to non-Optional Mapping (#4569)#5183WatchTree-19 wants to merge 4 commits into
Conversation
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 {})` |
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, |
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]: |
There was a problem hiding this comment.
Do / should we update the API definition too?
…try#5183) Signed-off-by: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com>
|
thanks @MikeGoldsmith. pushed a follow-up addressing all three:
@property
def attributes(self) -> Mapping[str, types.AttributeValue]:
# `or {}` keeps the return non-None; see #4569.
return MappingProxyType(self._attributes or {})
let me know if there's anything else. |
|
Thanks for the PR! Just a heads-up: we no longer update Please add the appropriate changelog fragment for this change instead of editing |
|
@emdneto already addressed in the follow-up - reverted the |
6b96015 to
6e05d7d
Compare
|
@MikeGoldsmith @emdneto gentle bump - rebased onto current main to clear the |
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
|
still active. branch is rebased onto current main, single commit (SDK annotation |
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
6e05d7d to
29614aa
Compare
|
@MikeGoldsmith thanks - addressed both points:
re-requesting review. branch is behind |
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
…n-telemetry#4569) The implementation always returns a `MappingProxyType`, never `None`, because `self._attributes or {}` falls back to an empty dict. Tightening the return annotation lets callers index `span.attributes["key"]` without type-checker complaints. Adds the towncrier fragment per CONTRIBUTING. Signed-off-by: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com>
29614aa to
11369be
Compare
|
Rebased onto current main ( Where this actually stands, since it's been round the stale-bot loop a few times:
@MikeGoldsmith — could you either re-review or dismiss the stale review if you're happy? Genuinely no rush on my side, but the review can't clear itself and the bot keeps threatening to bin it. And if on reflection this isn't a change the SDK wants — narrowing a public return annotation is a real (if small) compatibility surface — I'd rather close it than keep it cycling. Just say and I'll take care of it. |
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
|
gentle nudge before the stale bot closes this. the change request predates the rework, i tightened the annotation and @tekumara approved on the current shape. @MikeGoldsmith would you re-review when you get a chance? happy to adjust if anything still reads wrong. |
Pull request dashboard statusWaiting on the author · refreshed 2026-08-22 21:24 UTC Respond to 1 review item (e.g. link a commit, explain why not, ask a follow-up):
Status above doesn't look right?
|
3297186 to
4188951
Compare
|
Hi @WatchTree-19 — just a friendly reminder that this pull request is waiting on you. The dashboard status comment has the open items and is kept current.
|
|
@MikeGoldsmith friendly ping on this one, I think all three points are covered now:
Rebased on main and green, and @tekumara approved in July. Mind taking another look when you get a chance? |
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: