Repository navigation
MAINT: Fix unsound assignments in models and registry (UA-6) - #4523
Shawn Russ (shawnrruss) wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the scoped typing and taxonomy-validation changes.
No open issues 🎉
What changed in this PR
Advances UA-6 of #4402 by addressing 13 unsound-assignment findings across nine models and registry files, enabling the type-checking rule for that scope.
Changes:
- Adds taxonomy YAML validation and unit tests for defaults and malformed inputs.
- Refines model and registry typing while preserving response metadata precedence and credential and destination checks.
- Removes the nine scoped files from the temporary
tyoverride.
| File | Description |
|---|---|
tests/unit/models/test_harm_category.py |
Tests taxonomy defaults and invalid input rejection. |
pyrit/registry/registry_recipe_save.py |
Reuses the typed credential fallback helper. |
pyrit/registry/registry_recipe_rules.py |
Types fallback, authentication, and destination policies. |
pyrit/registry/registry_recipe_restore.py |
Preserves registry metadata types through generic parameters. |
pyrit/registry/components/target_registry.py |
Types projected authentication modes. |
pyrit/models/score/scorable.py |
Explicitly types scorable classes derived from the union. |
pyrit/models/score/observation.py |
Creates a typed dictionary for message digest calculation. |
pyrit/models/parameter.py |
Uses an object boundary when rendering literal type names. |
pyrit/models/messages/conversations.py |
Copies request metadata and applies response overrides. |
pyrit/models/harm_category.py |
Validates taxonomy version and definition types at load time. |
pyproject.toml |
Enables unsound-assignment checks for the nine scoped files. |
Review effort: Balanced
55caf42 to
c849565
Compare
Roman Lutz (romanlutz)
left a comment
There was a problem hiding this comment.
Reviewed as a draft per request (the force-pushed rebase onto latest main is content-identical in pyrit/ and tests/, so this covers the current head). All 13 UA-6 findings are fixed per the #4402 rules: YAML boundary validated with tests, casts only where guaranteed by construction (each verified against the PromptTarget ClassVar declarations and carrying a justifying comment), and _class_parameters fixed at the source with real generics. Verified locally on the branch: ty check pyrit clean with the 9 files off the override, and pytest tests/unit/models tests/unit/registry → 4740 passed / 19 skipped / 2 xfailed. I checked each hunk for behavior changes: the only one is the intentional fail-fast on malformed taxonomy YAML, which is declared and tested; MessagePiece.prompt_metadata can never be None (default-factory dict field), so the unguarded dict(...) in construct_response_from_request is safe and the merge semantics are preserved.
One small asymmetry, with ready-to-apply suggestions inline: a bare version: or definitions: key parses as None, which _parse_taxonomy rejects (and that takes down import pyrit.models), while an absent key or an empty document gets the default. The four suggestions below treat None like an absent key, keeping the errors for other wrong types. Applied together they are exactly a commit I verified locally on this head: the 69 harm-category tests pass and ty is clean on both files. I would have pushed it directly, but the PR does not allow maintainer edits.
| version = data.get("version", "v1.0.0") | ||
| if not isinstance(version, str): | ||
| raise ValueError("The harm category taxonomy version must be a string.") |
There was a problem hiding this comment.
A bare version: key parses as None; treat it like an absent key instead of failing the import.
| version = data.get("version", "v1.0.0") | |
| if not isinstance(version, str): | |
| raise ValueError("The harm category taxonomy version must be a string.") | |
| version = data.get("version") | |
| if version is None: | |
| version = "v1.0.0" | |
| if not isinstance(version, str): | |
| raise ValueError("The harm category taxonomy version must be a string.") |
| raise ValueError("The harm category taxonomy version must be a string.") | ||
|
|
||
| definitions: dict[str, str] = {} | ||
| raw_definitions = data.get("definitions", {}) |
There was a problem hiding this comment.
Same for a bare definitions: key (an empty YAML section), which currently raises while an empty document gets the default.
| raw_definitions = data.get("definitions", {}) | |
| raw_definitions = data.get("definitions") | |
| if raw_definitions is None: | |
| raw_definitions = {} |
| assert HARM_CATEGORY_TAXONOMY_VERSION == "v1.0.0" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("data", [None, {}]) |
There was a problem hiding this comment.
With the two suggestions on _parse_taxonomy, bare keys join the defaulting cases:
| @pytest.mark.parametrize("data", [None, {}]) | |
| @pytest.mark.parametrize("data", [None, {}, {"version": None, "definitions": None}]) |
| [ | ||
| ([], "taxonomy must be a mapping"), | ||
| ({"version": 1}, "version must be a string"), | ||
| ({"definitions": None}, "definitions must map strings to strings"), |
There was a problem hiding this comment.
{"definitions": None} moves to the defaulting cases above; a non-mapping like [] keeps this rejection path covered:
| ({"definitions": None}, "definitions must map strings to strings"), | |
| ({"definitions": []}, "definitions must map strings to strings"), |
Refs #4402 (UA-6).
Description
Fix the 13
unsound-assignmentfindings across the nine scoped models and registry files, and remove only those files from the temporarytyoverride. Validate the harm-category taxonomy at the YAML boundary and cover malformed inputs with unit tests. Preserve response metadata precedence and the existing registry credential and destination checks.The locked all-extras
tycheck went from 13 findings to zero. The offline models, registry, and selected downstream unit suites had 5,346 passes, 5 skips, and 2 expected failures.