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 1 commit
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 property. 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. same suggestion here, let's use 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. used |
||
code='model-config-cannot-be-a-property', | ||
) | ||
|
||
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 |
---|---|---|
|
@@ -61,6 +61,7 @@ | |
'unevaluable-type-annotation', | ||
'dataclass-init-false-extra-allow', | ||
'clashing-init-and-init-var', | ||
'model-config-cannot-be-a-property', | ||
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. Let's use 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. Done :) |
||
] | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -796,3 +796,16 @@ def create_partial(model, optionals): | |
|
||
# AssertionError: assert ['a', 'b'] == ['b'] | ||
assert partial.model_json_schema()['required'] == ['b'] | ||
|
||
|
||
def test_model_config_use_as_property(): | ||
""" | ||
Test: raises PydandicUserError when 'model_config' is used | ||
as a property | ||
""" | ||
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. Though I appreciate the extra documentation, I think if you just rename this test to something like 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. I agree, thanks :) |
||
with pytest.raises(PydanticUserError) as exc_info: | ||
|
||
class MyModel(BaseModel): | ||
model_config: str | ||
|
||
assert exc_info.value.code == 'model-config-cannot-be-a-property' |
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.
Below I suggested that we use
model-config-invalid-field-name
. Could you please change these docs to reflect that as well? I think we should use the term "model field" rather than property, just to keep things consistent 👍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.
Also done