- 
 
- 
  Notifications
 You must be signed in to change notification settings 
- Fork 784
🐛 Make generics work with SQLModel as base class #1275
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
 
  Open
 
 
 
 
 
 +89
 
 
 −0
 
 
 
 
 
 
 
 
  Open
 Changes from all commits
 Commits
 
 
 Show all changes
 
 
 6 commits
 
 
 Select commit
 Hold shift + click to select a range
 
 272181e
 
 fix: make generics work with SQLModel as base class
 
 
 ryangalamb b22c8d2
 
 test: add test case for json schema
 
 
 ryangalamb 360065b
 
 test: only run new test on pydantic v2
 
 
 ryangalamb 1dd3d83
 
 Merge branch 'main' into rjmill/fix-generic
 
 
 svlandeg 77acc53
 
 Merge branch 'main' into rjmill/fix-generic
 
 
 svlandeg 25f6f99
 
 Merge branch 'main' into rjmill/fix-generic
 
 
 YuriiMotov 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
 
 
 
 
 
 
 
 
 83 changes: 83 additions & 0 deletions
 
 
 
 tests/test_pydantic/test_generic.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,83 @@ | ||
| from typing import Generic, List, Optional, TypeVar | ||
|  | ||
| import pydantic | ||
| import pytest | ||
| from sqlmodel import SQLModel | ||
|  | ||
| from tests.conftest import needs_pydanticv2 | ||
|  | ||
| # Example adapted from | ||
| # https://docs.pydantic.dev/2.10/concepts/models/#generic-models | ||
| DataT = TypeVar("DataT") | ||
|  | ||
|  | ||
| class DataModel(SQLModel): | ||
| numbers: List[int] | ||
| people: List[str] | ||
|  | ||
|  | ||
| class Response(SQLModel, Generic[DataT]): | ||
| data: Optional[DataT] = None | ||
|  | ||
|  | ||
| @needs_pydanticv2 | ||
| @pytest.mark.parametrize( | ||
| ["data_type", "data_value"], | ||
| [ | ||
| (int, 1), | ||
| (str, "value"), | ||
| (DataModel, DataModel(numbers=[1, 2, 3], people=[])), | ||
| (DataModel, {"numbers": [1, 2, 3], "people": []}), | ||
| ], | ||
| ) | ||
| def test_valid_generics(data_type, data_value): | ||
| # Should be able to create a model without an error. | ||
| response = Response[data_type](data=data_value) | ||
| assert Response[data_type](**response.model_dump()) == response | ||
|  | ||
|  | ||
| @needs_pydanticv2 | ||
| @pytest.mark.parametrize( | ||
| ["data_type", "data_value", "error_loc", "error_type"], | ||
| [ | ||
| ( | ||
| str, | ||
| 1, | ||
| ("data",), | ||
| "string_type", | ||
| ), | ||
| ( | ||
| int, | ||
| "some-string", | ||
| ("data",), | ||
| "int_parsing", | ||
| ), | ||
| ( | ||
| DataModel, | ||
| "some-string", | ||
| ("data",), | ||
| "model_attributes_type", | ||
| ), | ||
| ( | ||
| DataModel, | ||
| {"numbers": [1, 2, "unexpected string"], "people": []}, | ||
| ("data", "numbers", 2), | ||
| "int_parsing", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_invalid_generics(data_type, data_value, error_loc, error_type): | ||
| with pytest.raises(pydantic.ValidationError) as raised: | ||
| Response[data_type](data=data_value) | ||
| [error_dict] = raised.value.errors() | ||
| assert error_dict["loc"] == error_loc | ||
| assert error_dict["type"] == error_type | ||
|  | ||
|  | ||
| @needs_pydanticv2 | ||
| def test_generic_json_schema(): | ||
| schema = Response[DataModel].model_json_schema() | ||
| # Should have referenced the schema in $defs | ||
| assert schema["properties"]["data"]["anyOf"][0] == {"$ref": "#/$defs/DataModel"} | ||
| # Schema in $defs should match DataModel's schema. | ||
| assert schema["$defs"]["DataModel"] == DataModel.model_json_schema() | 
 
 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.