Skip to content

MAINT: Type the initializer info payloads as TypedDicts (P311-02) - #4542

Merged
Roman Lutz (romanlutz) merged 5 commits into
microsoft:mainfrom
DevShoaib78:feat/initializer-info-typeddicts
Nov 23, 2026
Merged

Roman Lutz (romanlutz) merged 5 commits into
microsoft:mainfrom
DevShoaib78:feat/initializer-info-typeddicts

Conversation

@DevShoaib78

Copy link
Copy Markdown
Contributor

Description

Case 2a of #4505: PyRITInitializer.get_info_async() and get_dynamic_default_values_info_async() now return TypedDicts instead of dict[str, Any], following the BuildInfo pattern from #4106.

  • InitializerInfo (functional syntax, since class is a keyword): description, class, default_values and global_variables are Required; supported_parameters and required_env_vars are NotRequired, as they are only added when the initializer declares some.
  • InitializerDefaultsInfo: both keys Required, typed list[str] | str. The str arm is the message returned when memory is not initialized or the sandbox run raises; the docstrings now say so.
  • InitializerParameterInfo types each supported_parameters entry (name, description, default).
  • A private _InitializationTracking types the dict yielded by _track_initialization_changes, which only ever holds lists. get_dynamic_default_values_info_async builds its return value from it, since the list-only TypedDict is not assignable to the list[str] | str one.
  • The three public TypedDicts are exported from pyrit.setup.initializers, next to PyRITInitializer, so callers can annotate with them.
  • Runtime values are unchanged. One visible difference: get_info_async now builds the dict with its Required keys first, so default_values and global_variables come before the optional keys when iterating (the configuration notebook prints info.items()). Happy to restore the old order if you prefer it; it needs either a cast or building the dict per case.
  • Tests in tests/unit/setup/test_pyrit_initializer.py pin the Required and NotRequired keys of all three TypedDicts, the full payload when every key is present, the required-only payload, and both str arms (no memory, and a raising sandbox run or lookup).

I left the # type: ignore on info = await TargetInitializer.get_info_async() in doc/code/setup/1_configuration.py. Checking that file with ty without it reports await outside of an asynchronous function, the same as the other top-level awaits there, so it covers the top-level await rather than the return type.

Case 2b (pyrit.output JSON payloads) is not part of this PR, so #4505 stays open.

get_info_async() and get_dynamic_default_values_info_async() now return InitializerInfo and InitializerDefaultsInfo instead of dict[str, Any], so the optional keys and the message-string arms are part of the signature. Case 2a of microsoft#4505.
Copilot AI balanced review requested due to automatic review settings November 21, 2026 17:25

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 test clears the process-wide memory singleton without restoring it, potentially leaving subsequent callers with uninitialized memory; a scoped patch is needed to avoid leaking state.

1 open issue
What changed in this PR

This PR gives PyRIT’s initializer metadata APIs explicit TypedDict contracts, implementing case 2a of #4505 and following the existing BuildInfo pattern.

Changes:

  • Defines required and optional payload fields, including list-or-error-string values and a private tracking type.
  • Exports the three public contracts through pyrit.setup.initializers.
  • Adds tests for complete and minimal payloads, key contracts, and failure responses.
File Description
tests/​unit/​setup/​test_pyrit_initializer.py Tests TypedDict contracts, optional-key omission, and message-string responses.
pyrit/​setup/​pyrit_initializer.py Adds typed payloads and return annotations; builds required keys before optional keys.
pyrit/​setup/​initializers/​__init__.py Adds lazy exports and type-checking imports for the public contracts.

Review effort: Balanced

The no-memory get_info_async test cleared CentralMemory for the rest of the process; it now patches get_memory_instance for the one call.
…fo_async

The committed output cell still showed supported_parameters before
default_values/global_variables; get_info_async now inserts the required
keys first, so a notebook regeneration would have produced this diff.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Nov 23, 2026
Merged via the queue into microsoft:main with commit 5b51e96 Nov 23, 2026
66 checks passed
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