MAINT: Making Score a Pydantic model#1891
Merged
Merged
Conversation
Convert Score from a hand-written class to a Pydantic v2 BaseModel, mirroring the MessagePiece refactor (PR microsoft#1871). UnvalidatedScore stays a dataclass. Move the ComponentIdentifierField alias into score.py and have message_piece.py consume it plus the native Score type. Switch internal serialization paths (attack_result, sqlite_memory) to model_dump/model_validate so the deprecated to_dict/from_dict shims don't fire. Make score_value_description/score_rationale/score_metadata accept None to preserve the old permissive behavior, and update tests that relied on the unvalidated class. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…time All Score timestamp inputs are already tz-aware (memory wraps with _ensure_utc, scorers pass MessagePiece.timestamp, UnvalidatedScore.to_score falls back to now(utc)), so the before-validator was redundant. Naive datetimes now raise via AwareDatetime instead of being silently coerced. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Reintroduces Score.validate() as a deprecated no-op-style method that re-runs construction-time validation on the current instance. Args are ignored; it no longer needs to be called explicitly since validation happens at construction. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tato # Conflicts: # pyrit/models/message_piece.py # pyrit/models/score.py
romanlutz
reviewed
Jun 2, 2026
romanlutz
approved these changes
Jun 2, 2026
Replace Optional[X]/Union[...] with X | None and PEP 604 unions per the PyRIT style guide (str | None, dict[str, str | int | float] | None, etc.) across Score and UnvalidatedScore. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tato # Conflicts: # pyrit/models/message_piece.py
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.
This converts Score to a Pydantic BaseModel. It is phase 5 of the pyrit.models refactor: https://gist.github.com/rlundeen2/3e8daa8e12a11b4b6e52587b3c9b1dca