Skip to content

MAINT: Fix unsound-assignment findings in pyrit/score (UA-5) - #4519

Merged
Roman Lutz (romanlutz) merged 7 commits into
microsoft:mainfrom
sainimal1ba-hue:ua5-score-unsound-assignment
Nov 22, 2026
Merged

Roman Lutz (romanlutz) merged 7 commits into
microsoft:mainfrom
sainimal1ba-hue:ua5-score-unsound-assignment

Conversation

@sainimal1ba-hue

Copy link
Copy Markdown
Contributor

Description

UA-5 for #4402: fixes the seven ty unsound-assignment findings in pyrit/score and removes the score files from the temporary override. Rebased on current main (a943e99).

  • scorer.py (2): the legacy hook call and the _prompt_target lookup hand back Any; both are cast with a comment stating why the type is guaranteed.
  • llm_scoring.py (1): fixed at the source. pyrit_json_retry erased the decorated function's signature to Callable[..., Any], which left the UnvalidatedScore return unchecked; the decorator now preserves the signature with the same ParamSpec/TypeVar pair pyrit_target_retry already uses. This also covers the identical shape in prompt_normalizer/json_retry.py, which stays on the override for UA-2.
  • true_false/prompt_shield_scorer.py (1): userPromptAnalysis is now validated. Absent, False or a dict passes, anything else raises ValueError, with a regression test for the rejected case.
  • float_scale/local_violence_classifier_scorer.py (1), observation/otel_execution_trace_source.py (1) and scorer_prompt_validator.py (1): values typed by construction are cast with comments (torch's tolist() is untyped in the stubs, re.split widens its element type for patterns with capture groups, get_args returns Any).

No # type: ignore added and no annotation weakened to Any.

Testing

ty check pyrit passes with the score files off the override. Reverting the six score files and the decorator on this branch brings back exactly the seven findings, so the fixes are what keep the check green. pytest tests/unit/score passes (7272 tests).

Fix the seven ty unsound-assignment findings in pyrit/score and remove those files from the temporary override. Values typed by construction are cast with a comment stating why; the Prompt Shield response's userPromptAnalysis field is validated with a clear error and a regression test. The pyrit_json_retry decorator preserves the decorated function's signature instead of erasing it to Any, which made the call sites unchecked and also accounts for the finding in prompt_normalizer/json_retry.py.

Part of microsoft#4402 (UA-5).

Signed-off-by: sainimal1ba-hue <230497022+sainimal1ba-hue@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 10:21
@sainimal1ba-hue

Copy link
Copy Markdown
Contributor Author

a12san (@sainimal1ba-hue) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

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.

🟡 Changes recommended

The parser-only test leaves the public score_text_async failure behavior—a RuntimeError caused by ValueError—and malformed falsy responses such as null or [] untested.

1 open issue
What changed in this PR

This PR completes UA-5 of #4402, addressing seven unsound-assignment findings in PyRIT’s scoring layer and enabling the rule for those files.

Changes:

  • Preserve decorated function signatures in pyrit_json_retry.
  • Add documented casts where types are guaranteed by construction.
  • Validate Prompt Shield’s userPromptAnalysis field and add a rejection test.
File Description
tests/​unit/​score/​test_prompt_shield_scorer.py Adds malformed-response rejection coverage.
pyrit/​score/​true_false/​prompt_shield_scorer.py Validates userPromptAnalysis before parsing detections.
pyrit/​score/​scorer.py Casts legacy hook results and dynamic target lookups.
pyrit/​score/​scorer_prompt_validator.py Casts roles derived from the role type.
pyrit/​score/​observation/​otel_execution_trace_source.py Casts regex split results used for natural sorting.
pyrit/​score/​float_scale/​local_violence_classifier_scorer.py Casts tensor-derived embedding lists.
pyrit/​exceptions/​exception_classes.py Preserves signatures through the JSON retry decorator.
pyproject.toml Removes six score files from the temporary typing override.

Review effort: Balanced

…ing path

null and [] used to come back as a negative detection, and the failure behavior through score_text_async was untested.

Signed-off-by: sainimal1ba-hue <230497022+sainimal1ba-hue@users.noreply.github.com>
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Nov 21, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 21, 2026
@sainimal1ba-hue

Copy link
Copy Markdown
Contributor Author

resolved the conflicts

…ieldScorer

The sibling field one line below the UA-5 fix had the same hazard: a
truthy non-list crashed in the detection comprehension and a falsy
non-list silently scored [False]. Reject both with the same ValueError
treatment, covered through the public scoring path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Nov 22, 2026
Merged via the queue into microsoft:main with commit 5d60c62 Nov 22, 2026
66 checks passed
Roman Lutz (romanlutz) added a commit to romanlutz/PyRIT that referenced this pull request Nov 23, 2026
Resolve the pyproject.toml conflict with UA-3, UA-4, UA-5 and UA-7 (microsoft#4520,
microsoft#4537, microsoft#4519, microsoft#4517): each change removes its own files from the temporary
unsound-assignment override, so the merged list drops those and the GCG
entries and keeps only the UA-6 files.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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