-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(pydantic): skip non-dict models in list_models #2564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Bartok9
wants to merge
2
commits into
Canner:main
from
Bartok9:fix/pydantic-list-models-non-dict-20260722
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
88 changes: 88 additions & 0 deletions
88
sdk/wren-pydantic/tests/unit/test_run_list_models_guard.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| """_run_list_models skips non-dict / incomplete model rows.""" | ||
| import ast | ||
| import importlib.util | ||
| import sys | ||
| import types | ||
| from pathlib import Path | ||
| from unittest.mock import MagicMock | ||
|
|
||
| ROOT = Path(__file__).resolve().parents[2] | ||
| SRC = ROOT / "src" | ||
|
|
||
| # Stub heavy deps before loading _tools | ||
| sys.path.insert(0, str(SRC)) | ||
|
|
||
| # Provide minimal ModelSummary used by _tools | ||
| models_mod = types.ModuleType("wren_pydantic._models") | ||
|
|
||
|
|
||
| class ModelSummary: | ||
| def __init__(self, name, column_count, description=None): | ||
| self.name = name | ||
| self.column_count = column_count | ||
| self.description = description | ||
|
|
||
|
|
||
| models_mod.ModelSummary = ModelSummary | ||
| sys.modules["wren_pydantic._models"] = models_mod | ||
|
|
||
| # Other imports in _tools — stub at package boundary by executing only the function | ||
| # Extract function source with ast and exec in isolation is heavy; load module with stubs. | ||
|
|
||
| for name in [ | ||
| "wren", | ||
| "wren.model", | ||
| "wren.model.error", | ||
| "wren.engine", | ||
| "pydantic_ai", | ||
| "pydantic_ai.exceptions", | ||
| "wren_pydantic", | ||
| "wren_pydantic._errors", | ||
| "wren_pydantic._toolkit", | ||
| ]: | ||
| sys.modules.setdefault(name, types.ModuleType(name)) | ||
|
|
||
| sys.modules["wren.model.error"].WrenError = type("WrenError", (Exception,), {}) | ||
| sys.modules["wren_pydantic._errors"].should_propagate = lambda e: False | ||
| sys.modules["wren_pydantic._errors"].to_model_retry = lambda e: e | ||
|
|
||
| # _tools imports many symbols — read file and exec just _run_list_models after injecting names | ||
| src = (SRC / "wren_pydantic" / "_tools.py").read_text() | ||
| # Pull the function body by line markers | ||
| start = src.index("def _run_list_models") | ||
| # until next top-level def at same indent | ||
| rest = src[start:] | ||
| lines = rest.splitlines(True) | ||
| body = [lines[0]] | ||
| for line in lines[1:]: | ||
| if line.startswith("def ") or line.startswith("async def ") or line.startswith("class "): | ||
| break | ||
| body.append(line) | ||
| code = "".join(body) | ||
| ns = { | ||
| "ModelSummary": ModelSummary, | ||
| "WrenError": sys.modules["wren.model.error"].WrenError, | ||
| "should_propagate": sys.modules["wren_pydantic._errors"].should_propagate, | ||
| "to_model_retry": sys.modules["wren_pydantic._errors"].to_model_retry, | ||
| "list": list, | ||
| } | ||
| exec(code, ns) | ||
| _run_list_models = ns["_run_list_models"] | ||
|
|
||
|
|
||
| def test_skips_bad_models(): | ||
| toolkit = MagicMock() | ||
| toolkit._mdl_source.load_manifest.return_value = { | ||
| "models": [ | ||
| None, | ||
| "x", | ||
| {"columns": []}, # no name | ||
| {"name": "ok", "columns": None, "properties": "nope"}, | ||
| {"name": "t", "columns": [{"n": 1}], "properties": {"description": "d"}}, | ||
| ] | ||
| } | ||
| out = _run_list_models(toolkit) | ||
| assert [m.name for m in out] == ["ok", "t"] | ||
| assert out[0].column_count == 0 | ||
| assert out[1].column_count == 1 | ||
| assert out[1].description == "d" | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.