-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Handle a case when model_config is defined as a model property #9004
Changes from 3 commits
3af130e
218b6e6
f58744d
2149ee1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -116,6 +116,13 @@ def for_model(cls, bases: tuple[type[Any], ...], namespace: dict[str, Any], kwar | |||||
config_class_from_namespace = namespace.get('Config') | ||||||
config_dict_from_namespace = namespace.get('model_config') | ||||||
|
||||||
raw_annotations = namespace.get('__annotations__', {}) | ||||||
if raw_annotations.get('model_config') and not config_dict_from_namespace: | ||||||
raise PydanticUserError( | ||||||
'"model_config" cannot be used as a model field. Use "model_config" for model configuration', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
code='model-config-invalid-field-name', | ||||||
) | ||||||
|
||||||
if config_class_from_namespace and config_dict_from_namespace: | ||||||
raise PydanticUserError('"Config" and "model_config" cannot be used together', code='config-both') | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -796,3 +796,26 @@ def create_partial(model, optionals): | |||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
# AssertionError: assert ['a', 'b'] == ['b'] | ||||||||||||||||||||||||||||||||||||||
assert partial.model_json_schema()['required'] == ['b'] | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def test_model_config_as_model_field_raises(): | ||||||||||||||||||||||||||||||||||||||
with pytest.raises(PydanticUserError) as exc_info: | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
class MyModel(BaseModel): | ||||||||||||||||||||||||||||||||||||||
model_config: str | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
assert exc_info.value.code == 'model-config-invalid-field-name' | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def test_dataclass_allowes_model_config_as_model_field(): | ||||||||||||||||||||||||||||||||||||||
config_title = 'from_config' | ||||||||||||||||||||||||||||||||||||||
model_field_title = 'model_field' | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@pydantic_dataclass(config={'title': config_title}) | ||||||||||||||||||||||||||||||||||||||
class MyDataclass: | ||||||||||||||||||||||||||||||||||||||
model_config: dict | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
m = MyDataclass(model_config={'title': model_field_title}) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
assert m.model_config['title'] == model_field_title | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for you comments, I added all of it |
||||||||||||||||||||||||||||||||||||||
assert getattr(m, '__pydantic_config__')['title'] == config_title |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.