Skip to content

MAINT: Enable the ty unsound-assignment rule in stages (267 findings) #4402

Open

Description

@romanlutz

ty's unsound-assignment rule is switched off in pyproject.toml (unsound-assignment = "ignore" under [tool.ty.rules], where everything else runs at all = "error"). This issue tracks turning it on.

What the rule catches

It flags a variable annotated with a concrete type that is assigned a value the checker only knows as Any:

# pyrit/common/cli_helpers.py: getattr returns Any, so nothing checks that this is an int
level_value: int = getattr(logging, level_upper)

The annotation states a type that nothing verified. If the value is something else, type checking still passes and the failure shows up later at runtime, far from where the wrong value came in. With the rule on, every such annotation becomes a checked boundary.

Current state

UA-0 is merged (#4407): the rule is on for pyrit/, and ty check pyrit is clean because the existing findings are fenced off by two overrides in pyproject.toml.

Measured at a943e99 with ty 0.0.99 on Python 3.14, with those overrides removed locally: 309 findings in 49 files. (This issue first said 267 in 45 files. That count missed 16 findings in three GCG files, and main has moved since.)

To see the findings for your chunk, delete its files from the temporary override in pyproject.toml and run:

uv run --frozen --extra all ty check pyrit --output-format concise

The overrides take precedence over --error unsound-assignment on the command line, so the flag alone no longer lists them.

Group Findings Files What they are
pyrit/memory/memory_models.py 218 1 The standard SQLAlchemy declarative idiom: 177 x col: Mapped[T] = mapped_column(...) and 40 x rel: Mapped[T] = relationship(...), one root cause. The remaining one is a json.loads result assigned to a `dict[str, str]
Everything else 91 48 Individual Any values (JSON, getattr, config and environment lookups, untyped third-party returns) assigned to annotated variables.

One PR or several? Several.

  1. There are two different kinds of work here. memory_models.py is a tooling decision: x: Mapped[T] = mapped_column(...) is how SQLAlchemy 2.0 models are meant to be written, so the right answer is one scoped override, not rewriting more than 200 declarations in the file our migrations depend on. The other 91 findings each need a local judgment about whether the value really has the annotated type, and some of them may turn out to be real unchecked assumptions.
  2. Reviewability. The 91 findings are spread over 48 files in 14 packages. A fix that adds validation or narrowing changes behavior on bad input and needs a test next to the code. That cannot be reviewed carefully in one sweep.
  3. Merge conflicts. Small per-package PRs rebase easily against the many PRs that are open at any time; one PR touching 49 files does not.
  4. No backsliding while the work is in progress. Step 1 below turns the rule on immediately, so new code is checked from day one and the remaining findings can only go down.

Plan

Step 1 (UA-0): done, merged in #4407. Configuration only, no source changes:

  • Delete unsound-assignment = "ignore".
  • Add a permanent [[tool.ty.overrides]] entry for pyrit/memory/memory_models.py that ignores the rule, with a comment explaining the SQLAlchemy idiom.
  • Add a second, temporary [[tool.ty.overrides]] entry that ignores the rule for the remaining files listed below, with a comment pointing to this issue.
  • Result: ty check pyrit is clean and every file not on the temporary list is checked.
  • make ty also checks tests/unit, so MAINT: Enable the ty unsound-assignment rule with staged overrides (UA-0) #4407 added the rule to the existing tests/unit/** override as well.

Step 2, in parallel, one PR per chunk. Fix the findings in the chunk and remove its files from the temporary override.

ID Scope Findings
UA-1 pyrit/executor/promptgen/gcg (4 files) 28
UA-2 rest of pyrit/executor, plus pyrit/scenario, pyrit/converter, pyrit/prompt_normalizer 7
UA-3 pyrit/prompt_target, pyrit/embedding, pyrit/auth 14
UA-4 pyrit/backend 10
UA-5 pyrit/score 7
UA-6 pyrit/models, pyrit/registry 13
UA-7 pyrit/memory (without memory_models.py), pyrit/cli, pyrit/common 12

Step 3. Whoever empties the temporary override deletes it, and this issue can be closed.

Not covered by the chunks. Two things stay unchecked after UA-1 to UA-7 and are not assigned to anyone:

  • tests/unit: 33 findings in 18 files. The rule is off there through the tests/unit/** override.
  • pyrit/memory/memory_models.py line 2799: display_group_map = json.loads(self.display_group_map_json) is a real finding, not the SQLAlchemy idiom. The permanent override is file-wide, so the checker does not report it.

How to fix a finding

In order of preference:

  1. Fix the type at the source. If the Any comes from one of our own functions, give that function a real return type.
  2. Check the value when it comes from outside the program (JSON, HTTP responses, environment, files): narrow with isinstance or validate, and raise a clear error otherwise. Add a test for the rejected case.
  3. cast(...) only when the type is guaranteed by construction, with a short comment saying why.

Do not add # type: ignore, and do not weaken the annotation to Any to silence the rule. If a finding turns out to be a real bug, say so in the PR description.

The 48 files outside memory_models.py, with finding counts
  • pyrit/auth/browser_session_copilot_authenticator.py (1)
  • pyrit/backend/middleware/runtime.py (1)
  • pyrit/backend/routes/targets.py (1)
  • pyrit/backend/routes/version.py (3)
  • pyrit/backend/services/attack_service.py (2)
  • pyrit/backend/services/finding_export.py (1)
  • pyrit/backend/services/prompt_generation_service.py (1)
  • pyrit/backend/services/scenario_service.py (1)
  • pyrit/cli/api_client.py (2)
  • pyrit/cli/pyrit_scan.py (2)
  • pyrit/common/cli_helpers.py (1)
  • pyrit/common/yaml_loadable.py (1)
  • pyrit/converter/decomposition_converter.py (1)
  • pyrit/embedding/openai_text_embedding.py (2)
  • pyrit/executor/attack/multi_turn/tree_of_attacks.py (1)
  • pyrit/executor/attack/single_turn/many_shot_jailbreak.py (1)
  • pyrit/executor/promptgen/gcg/attack/base/attack_manager.py (17)
  • pyrit/executor/promptgen/gcg/attack/gcg/candidate_proposer.py (4)
  • pyrit/executor/promptgen/gcg/attack/gcg/gcg_attack.py (1)
  • pyrit/executor/promptgen/gcg/default_implementations.py (6)
  • pyrit/memory/alembic/env.py (1)
  • pyrit/memory/attack_analytics.py (1)
  • pyrit/memory/azure_sql_memory.py (2)
  • pyrit/memory/memory_interface.py (2)
  • pyrit/models/harm_category.py (2)
  • pyrit/models/messages/conversations.py (1)
  • pyrit/models/parameter.py (1)
  • pyrit/models/score/observation.py (1)
  • pyrit/models/score/scorable.py (1)
  • pyrit/prompt_normalizer/json_retry.py (1)
  • pyrit/prompt_normalizer/prompt_normalizer.py (1)
  • pyrit/prompt_target/azure_blob_storage_target.py (1)
  • pyrit/prompt_target/common/discover_target_capabilities.py (1)
  • pyrit/prompt_target/openai/openai_error_handling.py (3)
  • pyrit/prompt_target/openai/openai_response_target.py (2)
  • pyrit/prompt_target/prompt_shield_target.py (4)
  • pyrit/registry/components/target_registry.py (1)
  • pyrit/registry/registry_recipe_restore.py (1)
  • pyrit/registry/registry_recipe_rules.py (4)
  • pyrit/registry/registry_recipe_save.py (1)
  • pyrit/scenario/core/attack_technique_factory.py (1)
  • pyrit/scenario/scenarios/foundry/assert_policy.py (1)
  • pyrit/score/float_scale/local_violence_classifier_scorer.py (1)
  • pyrit/score/llm_scoring.py (1)
  • pyrit/score/observation/otel_execution_trace_source.py (1)
  • pyrit/score/scorer.py (2)
  • pyrit/score/scorer_prompt_validator.py (1)
  • pyrit/score/true_false/prompt_shield_scorer.py (1)

Status — who's doing what

(Maintained automatically; last updated 2026-11-23. Claim rules are below — first comment wins, no confirmation needed.)

ID Scope Claimed by Since PR State
UA-0 configuration (gates all others) Mohammad Saeed (@mss06s) 2026-11-18 #4407 merged — done
UA-1 executor/promptgen/gcg (4 files) Roman Lutz (@romanlutz) 2026-11-21 #4514 open
UA-2 rest of executor, scenario, converter, prompt_normalizer Christopher Pruijsen (@cpruijsen) 2026-11-20 #4544 merged — done
UA-3 prompt_target, embedding, auth Mehreen Munir (@clossal) 2026-11-19 #4520 merged — done
UA-4 backend Yani (@Steeve-Crypto) 2026-11-20 #4537 merged — done
UA-5 score a12san (@sainimal1ba-hue) 2026-11-20 #4519 merged — done
UA-6 models, registry Shawn Russ (@shawnrruss) 2026-11-19 #4523 open
UA-7 memory (minus memory_models.py), cli, common Aditya Malkar (@adimalkar) 2026-11-21 #4517 merged — done

How to contribute

  • Claim one chunk at a time, by its ID. Before you start, comment on this issue with the ID you are taking (for example UA-4).
  • First comment wins. If yours is the first comment claiming that ID, go ahead and open a PR; you do not need to wait for a reply. If someone has already claimed it, pick a different ID.
  • UA-0 is merged (MAINT: Enable the ty unsound-assignment rule with staged overrides (UA-0) #4407). PRs for UA-1 to UA-7 can be opened now. Rebase on current main first so your branch has the overrides.
  • Link this issue and the ID in your PR description.

Activity

  1. mss06s commented on Nov 18, 2026

    @mss06s
    Contributor

    I’ll take UA-0.

  2. clossal commented on Nov 19, 2026

    @clossal
    Contributor

    Hi, I'd like to take UA-3 (pyrit/prompt_target, pyrit/embedding, and pyrit/auth) if it's still available.

    I've reproduced all 13 unsound-assignment findings locally and started investigating the affected code and existing tests.

    Could you assign UA-3 to me? I'll keep the changes focused on the assigned scope and add regression tests where appropriate.

  3. shawnrruss commented on Nov 19, 2026

    @shawnrruss
    Contributor

    I’ll take UA-6 (pyrit/models and pyrit/registry). I’ll keep the changes scoped to that chunk and wait for UA-0 to merge before opening a PR.

  4. Steeve-Crypto commented on Nov 20, 2026

    @Steeve-Crypto
    Contributor

    I'll take UA-4 (pyrit/backend). I'll keep it scoped to that chunk and wait for UA-0 to merge before opening a PR.

  5. sainimal1ba-hue commented on Nov 20, 2026

    @sainimal1ba-hue
    Contributor

    I will take UA-5

  6. cpruijsen commented on Nov 20, 2026

    @cpruijsen
    Contributor

    I will take UA-2 (rest of pyrit/executor, plus pyrit/scenario, pyrit/converter and pyrit/prompt_normalizer). I will wait for UA-0 (#4407) to merge before I open a PR.

  7. clossal commented on Nov 20, 2026

    @clossal
    Contributor

    While investigating the UA-3 unsound-assignment findings in prompt_shield_target.py, I noticed a possible validation gap in _validate_response().

    The method checks that documentsAnalysis is nonempty, but doesn't verify that the number of returned analyses matches the number of submitted documents.

    I added a local regression test with two submitted documents and one returned analysis. The test fails with DID NOT RAISE ValueError, confirming that the current implementation accepts this mismatch.

    The existing tests also appear to follow a one-analysis-per-document structure.

    Would you prefer that I include a narrowly scoped validation fix and regression test in the UA-3 PR, or open a separate issue/PR and keep UA-3 focused strictly on typing?

  8. adimalkar commented on Nov 21, 2026

    @adimalkar
    Contributor

    I'll take UA-7 (pyrit/memory without memory_models.py, pyrit/cli and pyrit/common). I reproduced its 12 findings in 8 files on main (830f745) with ty 0.0.99. I'll keep the PR to that chunk and wait for UA-0 (#4407) to merge before opening it.

  9. romanlutz commented on Nov 21, 2026

    @romanlutz
    ContributorAuthor

    UA-0 is merged (#4407, thanks Mohammad Saeed (@mss06s)), so PRs for UA-1 to UA-7 can be opened now.

    Christopher Pruijsen (@cpruijsen) (UA-2), Mehreen Munir (@clossal) (UA-3), Yani (@Steeve-Crypto) (UA-4), a12san (@sainimal1ba-hue) (UA-5), Shawn Russ (@shawnrruss) (UA-6), Aditya Malkar (@adimalkar) (UA-7): you're unblocked.

    • Rebase on current main first so your branch has the overrides.
    • Your PR should fix the findings in your chunk and remove its files from the temporary [[tool.ty.overrides]] list in pyproject.toml.
    • To list your findings, delete your chunk's files from that list locally and run uv run --frozen --extra all ty check pyrit --output-format concise. The --error unsound-assignment flag alone no longer shows them, because the overrides take precedence.

    I updated the issue body to match main. Three chunks have more findings than it said before:

    • UA-3: 14 (was 13). openai_response_target.py now has 2.
    • UA-4: 10 (was 9). pyrit/backend/services/prompt_generation_service.py is new, with 1.
    • UA-6: 13 (was 11). registry_recipe_rules.py now has 4.

    UA-1 is still available. It covers the four files under pyrit/executor/promptgen/gcg (28 findings), not just attack_manager.py.

  10. romanlutz commented on Nov 21, 2026

    @romanlutz
    ContributorAuthor

    I'll take UA-1 (pyrit/executor/promptgen/gcg, 4 files, 28 findings).

  11. abhiramshaji-cyber commented on Nov 23, 2026

    @abhiramshaji-cyber
    Contributor

    Hi, hope you're doing well. I'd like to take the memory_models.py display_group_map item from 'Not covered by the chunks'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions