Skip to content

MAINT: remaining assert_never exhaustive-dispatch guards — inventory and batches (P311-03, from #3166) #4506

Open

Description

@romanlutz

Broken out from the Python 3.11 adoption tracker #3166 (item P311-03). Eleven focused assert_never PRs are merged (history in #3166); a maintainer-side sweep of every enum/Literal dispatch on current main found 19 remaining closed-set sites. Claim one batch at a time by comment; the first commenter on a batch may open its PR.

House pattern (established by the merged PRs): a TYPE_CHECKING-only assert_never guard that preserves any existing runtime fallback, plus an enum-extension type-regression test. Some sites first need an implicit else made explicit, or member.value == "x" comparisons changed to member is Enum.X so narrowing works.

Batch A — Literal-based converters and normalizer (5 files)

  • pyrit/message_normalizer/message_normalizer.py:120-132 — SystemMessageBehavior Literal, 3/3 handled, runtime raise commented "should never happen". Cleanest case.
  • pyrit/converter/base64_converter.py:75-92 — EncodingFunc Literal, 8/8 then raise; also annotate self._encoding_func (line 42).
  • pyrit/converter/bin_ascii_converter.py:64-69, 98-104 — 3-value Literal, two dispatch sites; also annotate self._encoding_func (line 59).
  • pyrit/converter/pinyin_converter.py:167-170 — PinyinMode, "full" is an implicit no-op; make it explicit before guarding.
  • pyrit/converter/unicode_confusable_converter.py:101-106 — 3-value source_package Literal; annotate self._source_package (line 66).

Batch B — score (4 files)

  • pyrit/score/float_scale/plagiarism_scorer.py:253-271 — PlagiarismMetric, 3/3 but compared via .value == (doesn't narrow); switch to is comparisons.
  • pyrit/score/response_handler.py:124-129 — CategoryConflictPolicy, REJECT_CONFLICT implicit; make explicit.
  • pyrit/score/true_false/wildguard_parser.py:49-56 — WildGuardLabel.description, implicit final return.
  • pyrit/score/true_false/regex/package_hallucination_scorer.py:489-504 — PackageEcosystem (7 members), JAVASCRIPT implicit no-op.

Batch C — executor and prompt_target (4 files, 7 sites)

  • pyrit/executor/attack/compound/sequential_attack.py:470-478, 480-500 — SequenceCompletionPolicy (5 members), two sites with commented fallthroughs; make membership explicit.
  • pyrit/executor/promptgen/assert_generator.py:408-448, 725-738, 1029-1034 — AssertGenerationStage ×2 (second site must explicitly reject SEED_GROUPS, which is invalid there) and AssertSamplingMethod.
  • pyrit/prompt_target/openai/openai_response_target.py:592-605 — WebSearchMode, DISABLED implicit.
  • pyrit/auth/openai_auth.py:47-75 — AuthMode Literal, "api_key" is the long implicit fallthrough; guard with if auth_mode != "api_key": assert_never(auth_mode).

Batch D — memory, cli, scenario, datasets (6 files, 7 sites)

  • pyrit/memory/attack_analytics_query.py:773-837 — AttackAnalyticsDimensionName (9 members), MODEL implicit; note self._ensure_identifiers() runs between branches, so place the guard just before the MODEL block.
  • pyrit/memory/memory_models.py:1973-2060 — SeedEntry.get_seed over DB-backed SeedType; type-only guard, keep the runtime fallthrough.
  • pyrit/memory/dataset_mutation.py:115-140 — seed_replacement over SeedType, objective implicit.
  • pyrit/cli/_target_credentials.py:48-53, 71-76 — CredentialSourceKind, two sites with implicit members.
  • pyrit/scenario/core/_attack_constructor_compatibility.py:95-108 — ScorerOverridePolicy, SKIP implicit no-op.
  • pyrit/datasets/seed_datasets/remote/medsafetybench_dataset.py:58-80 — 4-value subset_name Literal, 4/4 then raise.

Optional Tier-2 riders (only if a batch PR passes nearby)

FindingExportFormat in pyrit/backend/services/finding_export.py:97 and get_assert_failure_criterion in pyrit/score/true_false/assert_policy_scorer.py:560. The other ~13 binary/ternary sites surveyed are not worth guards.

Out of scope (deliberately open or partial — do not guard)

Partial AttackOutcome dispatches in attacks (deliberate "everything else" buckets), provider protocol event routers (realtime, websocket Copilot, OpenAI response types), OutputFormat functions that support subsets by design, dict-lookup dispatch, parameters typed str or Enum | str (can't narrow), and predicate-style checks. The sweep's full rejection list is recorded in the tracker notes.

Estimated size: ~25-40 lines per site including the extension-regression test; each batch lands at roughly 150-300 lines.

Activity

  1. InesZenkri commented on Nov 21, 2026

    @InesZenkri
    Contributor

    Roman Lutz (@romanlutz) I'd like to take Batch A, adding exhaustive-dispatch guards and regression tests.

  2. DevShoaib78 commented on Nov 21, 2026

    @DevShoaib78
    Contributor

    I would like to take Batch B (score): PlagiarismMetric (switching the .value == checks to is), CategoryConflictPolicy (explicit REJECT_CONFLICT), WildGuardLabel.description and PackageEcosystem (explicit JAVASCRIPT), with the TYPE_CHECKING assert_never guards and the enum-extension regression tests.

  3. Sandro850 commented on Nov 22, 2026

    @Sandro850

    Hi! I'd like to take Batch D (memory, CLI, scenario, and datasets).

    I'll follow the existing TYPE_CHECKING / assert_never pattern, preserve the current runtime behavior, and add focused type-regression tests for the seven dispatch sites.

    I'll also run the relevant tests and make ty before submitting the PR.

    Thanks!

  4. mcjim1689 commented on Nov 23, 2026

    @mcjim1689
    Contributor

    Hi, I'd like to take one slice of this (#4506): Batch C - executor and prompt_target (4 files, 7 sites). (AI-assisted; reviewed and tested before submitting.)

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