Skip to content

Silence pydantic's benign "schema field shadows BaseModel" startup warning#217

Merged
devopam merged 1 commit into
mainfrom
claude/silence-pydantic-schema-warning
Jul 2, 2026
Merged

Silence pydantic's benign "schema field shadows BaseModel" startup warning#217
devopam merged 1 commit into
mainfrom
claude/silence-pydantic-schema-warning

Conversation

@devopam

@devopam devopam commented Jul 2, 2026

Copy link
Copy Markdown
Owner

Summary

  • Running MCPg surfaced a wall of UserWarning: Field name "schema" in "..." shadows an attribute in parent "BaseModel" warnings from pydantic on startup / first tools/list.
  • Root cause: the mcp SDK's func_metadata.py dynamically builds a pydantic model from each tool's return-type dataclass to publish its outputSchema. Pydantic's BaseModel still carries a deprecated v1-compat .schema() method, and any dataclass field literally named schema (the natural name for "which Postgres schema", used across ~180 tool-return dataclasses in this codebase) collides with it. It's lazy — the warning only fires the first time Tool.output_schema (a cached_property) is accessed, i.e. the first tools/list call, which is why it shows up as soon as a client connects.
  • Renaming the field isn't a reasonable fix here — it would change the JSON key in every affected tool's response, a breaking change to the public tool contract, just to quiet a cosmetic startup warning from a deprecated pydantic v1 shim nobody uses.
  • Fix: suppress the specific, known-benign message via a scoped warnings.filterwarnings(...) at package import time (src/mcpg/__init__.py), matching only this exact pydantic message pattern (not a blanket UserWarning silence). Also added the same pattern to pyproject.toml's pytest filterwarnings, since pytest resets warnings.filters per test and the runtime filter alone doesn't survive into test collection — without it, the same warning was showing up in every test that touches Tool.output_schema (31+ occurrences in test_tool_surface_snapshot.py alone).

Test plan

  • Added test_output_schema_build_emits_no_pydantic_schema_field_shadow_warnings in tests/contract/test_tool_output_schemas.py, which builds the full maximal-flag tool surface fresh inside its own catch_warnings block (pytest resets filters per test, so this re-applies the exact same pattern mcpg exports) and asserts zero shadow warnings.
  • Reproduced the warning directly against a real FastMCP server + register_tools() + list_tools() before the fix (18 occurrences), confirmed 0 after.
  • uv run pytest -q -m "not integration" — 2624 passed, only one unrelated pre-existing warning (StarletteDeprecationWarning) left in the summary.
  • uv run ruff format --check . / uv run ruff check . / uv run mypy src/mcpg — all clean.

Generated by Claude Code

…rning

Every tool-return dataclass with a `schema` field (the natural name for
"which Postgres schema", used ~180 times across the tool surface) trips
a pydantic UserWarning the moment a client calls tools/list: the mcp SDK
dynamically builds a pydantic model from each dataclass to publish its
output schema, and pydantic's BaseModel still carries a deprecated v1
`.schema()` method that any field named `schema` collides with.

Renaming the field would break the JSON shape of every affected tool's
output, so this suppresses the specific, known-benign warning instead —
both at runtime (mcpg/__init__.py, so it doesn't print when the server
actually runs) and in pytest's own warning capture (pyproject.toml,
since pytest resets warnings.filters per test and would otherwise still
show it in every test that touches Tool.output_schema).

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @devopam, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@gemini-code-assist-2 gemini-code-assist-2 Bot 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.

Code Review

This pull request suppresses Pydantic deprecation warnings regarding fields (like schema) that shadow attributes in the parent BaseModel. It adds a warning filter in src/mcpg/__init__.py, configures pytest in pyproject.toml to ignore these warnings during test collection, and introduces a contract test to verify that no unexpected shadow warnings are leaked during tool output schema generation. I have no feedback to provide.

@devopam devopam merged commit 233fe09 into main Jul 2, 2026
19 checks passed
@devopam devopam deleted the claude/silence-pydantic-schema-warning branch July 2, 2026 12:02
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