Repository navigation
models: preserve subclass type for token usage metadata - #3884
Merged
Roman Lutz (romanlutz) merged 1 commit intoNov 3, 2026
Merged
Roman Lutz (romanlutz) merged 1 commit into
Roman Lutz (romanlutz) merged 1 commit into
Conversation
Part of #3166 Signed-off-by: Slai.life <huong8373tt@gmail.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the Self | None annotation update and accompanying regression coverage.
No open issues 🎉
What changed in this PR
This PR advances the Python 3.11 Self adoption in #3166 by making TokenUsage.from_metadata accurately describe its existing subclass-preserving behavior, without changing runtime logic.
Changes:
- Update the factory’s return annotation and docstring to
Self | None. - Add regression coverage for subclass construction and static type narrowing.
| File | Description |
|---|---|
tests/unit/models/test_token_usage.py |
Add subclass runtime assertions and assert_type checks. |
pyrit/models/target/token_usage.py |
Use `Self |
Review effort: Balanced
Roman Lutz (romanlutz)
self-requested a review
November 3, 2026 17:47
19 of 25 tasks
Roman Lutz (romanlutz)
approved these changes
Nov 3, 2026
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.
Summary
TokenUsage.from_metadatato returnSelf | Noneso subclasses retain their type when reconstructed.assert_typenarrowing.Part of #3166.
Validation
pytest tests/unit/models/test_token_usage.py -qty check pyrit/models/target/token_usage.py tests/unit/models/test_token_usage.pyruff check pyrit/models/target/token_usage.py tests/unit/models/test_token_usage.pyruff format --check pyrit/models/target/token_usage.py tests/unit/models/test_token_usage.pygit diff --checkAI disclosure
Slai Agent Runtime performed the investigation, implementation, and test execution. The account owner authorized this contribution and is responsible for its content.