Skip to content
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

Merged
merged 4 commits into from Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/errors/usage_errors.md
Expand Up @@ -1089,4 +1089,18 @@ pydantic.errors.PydanticUserError: Dataclass field bar has init=False and init_v
"""
```

## `model_config` is used as a model property {#model-config-cannot-be-a-property}
Copy link
Member

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 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also done

This error is raised when `model_config` is used as as property.
```py
from pydantic import BaseModel, PydanticUserError

try:

class Model(BaseModel):
model_config: str

except PydanticUserError as exc_info:
assert exc_info.code == 'model-config-cannot-be-a-property'
```

{% endraw %}
7 changes: 7 additions & 0 deletions pydantic/_internal/_config.py
Expand Up @@ -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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same suggestion here, let's use model field instead of model property

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used model field

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')

Expand Down
1 change: 1 addition & 0 deletions pydantic/errors.py
Expand Up @@ -61,6 +61,7 @@
'unevaluable-type-annotation',
'dataclass-init-false-extra-allow',
'clashing-init-and-init-var',
'model-config-cannot-be-a-property',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use model-config-invalid-field-name instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

]


Expand Down
13 changes: 13 additions & 0 deletions tests/test_config.py
Expand Up @@ -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
"""
Copy link
Member

Choose a reason for hiding this comment

The 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 test_model_config_as_model_field_raises, the test speaks for itself :).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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'