Silence pydantic's benign "schema field shadows BaseModel" startup warning#217
Merged
Merged
Conversation
…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).
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UserWarning: Field name "schema" in "..." shadows an attribute in parent "BaseModel"warnings from pydantic on startup / firsttools/list.mcpSDK'sfunc_metadata.pydynamically builds a pydantic model from each tool's return-type dataclass to publish itsoutputSchema. Pydantic'sBaseModelstill carries a deprecated v1-compat.schema()method, and any dataclass field literally namedschema(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 timeTool.output_schema(acached_property) is accessed, i.e. the firsttools/listcall, which is why it shows up as soon as a client connects.warnings.filterwarnings(...)at package import time (src/mcpg/__init__.py), matching only this exact pydantic message pattern (not a blanketUserWarningsilence). Also added the same pattern topyproject.toml's pytestfilterwarnings, since pytest resetswarnings.filtersper test and the runtime filter alone doesn't survive into test collection — without it, the same warning was showing up in every test that touchesTool.output_schema(31+ occurrences intest_tool_surface_snapshot.pyalone).Test plan
test_output_schema_build_emits_no_pydantic_schema_field_shadow_warningsintests/contract/test_tool_output_schemas.py, which builds the full maximal-flag tool surface fresh inside its owncatch_warningsblock (pytest resets filters per test, so this re-applies the exact same patternmcpgexports) and asserts zero shadow warnings.FastMCPserver +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