Skip to content

models: preserve subclass type for token usage metadata - #3884

Merged
Roman Lutz (romanlutz) merged 1 commit into
microsoft:mainfrom
huong8373tt-beep:fix/token-usage-self-3166
Nov 3, 2026
Merged

Roman Lutz (romanlutz) merged 1 commit into
microsoft:mainfrom
huong8373tt-beep:fix/token-usage-self-3166

Conversation

@huong8373tt-beep

Copy link
Copy Markdown
Contributor

Summary

  • Update TokenUsage.from_metadata to return Self | None so subclasses retain their type when reconstructed.
  • Add a regression test covering subclass construction and static assert_type narrowing.

Part of #3166.

Validation

  • pytest tests/unit/models/test_token_usage.py -q
  • ty check pyrit/models/target/token_usage.py tests/unit/models/test_token_usage.py
  • ruff check pyrit/models/target/token_usage.py tests/unit/models/test_token_usage.py
  • ruff format --check pyrit/models/target/token_usage.py tests/unit/models/test_token_usage.py
  • git diff --check

AI disclosure

Slai Agent Runtime performed the investigation, implementation, and test execution. The account owner authorized this contribution and is responsible for its content.

Part of #3166

Signed-off-by: Slai.life <huong8373tt@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 3, 2026 13:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Nov 3, 2026
Merged via the queue into microsoft:main with commit d56764d Nov 3, 2026
65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants