Repository navigation
Python: return NotImplemented for unsupported operands in CompletionUsage.__add__ - #14707
Open
Hardik K (hardikkaurani) wants to merge 1 commit into
Open
Hardik K (hardikkaurani) wants to merge 1 commit into
Hardik K (hardikkaurani) wants to merge 1 commit into
Conversation
…tionUsage.__add__
Hardik K (hardikkaurani)
temporarily deployed
to
github-app-auth
November 2, 2026 14:21 — with
GitHub Actions
Inactive
Contributor
Author
|
Hi Eduard van Valkenburg (@eavanvalkenburg), following up on your feedback in [#14706](#14706), this PR is strictly scoped to the bug fix in It adds the missing type check to return As requested, all new APIs ( |
Contributor
Author
|
Eduard van Valkenburg (@eavanvalkenburg) can you please take a look for the review ! |
This branch was previously deployed
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.
Motivation and Context
In
CompletionUsage.__add__, unsupported operand types (such as adding an integer,None, or an incompatible instance) previously caused an immediateAttributeErrorwhen attempting to accessother.prompt_tokens.According to standard Python binary operator conventions (Python Data Model / PEP 207), binary dunder methods should return
NotImplementedwhen an operand type is not supported. This allows Python to fall back to the reflected operator (__radd__) or raise a standardTypeError: unsupported operand type(s) for +.Per maintainer guidance in #14706, this PR is strictly scoped to this bugfix and excludes any new APIs (
total_tokensproperty or__radd__/sum()support).Description
CompletionUsage.__add__, added anisinstance(other, CompletionUsage)guard returningNotImplementedfor non-CompletionUsageinstances.test_completion_usage_invalid_add()intest_completion_token_usage.pyverifying that adding an unsupported operand raisesTypeError.Contribution Checklist