Skip to content

MAINT Return Self from WebSearchConfig.from_metadata (P311-01) - #3626

Open
Julio César Suástegui (juliosuas) wants to merge 1 commit into
microsoft:mainfrom
juliosuas:p311-01-websearchconfig-from-metadata
Open

Julio César Suástegui (juliosuas) wants to merge 1 commit into
microsoft:mainfrom
juliosuas:p311-01-websearchconfig-from-metadata

Conversation

@juliosuas

Copy link
Copy Markdown
Contributor

Description

WebSearchConfig.from_metadata is annotated as returning WebSearchConfig, but both of its paths build the calling class: cls() when no policy is present, and cls.model_validate(...) when one is. This annotates it with typing.Self, so a subclass gets its own type back from the type checker. Runtime behavior is unchanged; only the return annotation and its docstring line change.

This follows the same pattern as #3529 (HarmDefinition.from_yaml) and #3577 (JsonResponseConfig.from_metadata), and covers the remaining WebSearchConfig.from_metadata case in P311-01.

Related: #3166

Tests and Documentation

Added tests in tests/unit/models/test_web_search_config.py:

  • subclass runtime checks and assert_type for both paths (absent policy and explicit policy)
  • modes, read-only Mapping input, and that the input metadata is not mutated
  • frozen / extra-forbid validation and invalid payloads
  • metadata and JSON round trips

Ran on the rebased branch (current main):

  • ruff check and ruff format --check on the two touched files
  • ty check on pyrit and the two touched files
  • pytest tests/unit/models/test_web_search_config.py (37 passed)

The wider pytest tests/unit/models (2527 passed) and tests/unit/prompt_target suites were run just before the last rebase onto main, which only brought dependency bumps and an unrelated Docker change.

No documentation change is needed, since this is a typing-only change.

AI-assisted; reviewed and tested by me.

Annotate WebSearchConfig.from_metadata with Self. Both paths (absent policy via
cls() and explicit policy via cls.model_validate) already construct the calling
class; add subclass runtime and assert_type coverage plus coverage for modes,
read-only Mapping input, metadata non-mutation, frozen/extra-forbid validation,
invalid payloads and metadata/JSON round trips. Refs microsoft#3166.

This branch has not been deployed

No deployments
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.

2 participants