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
Conversation
please review @sydney-runkle |
CodSpeed Performance ReportMerging #9004 will not alter performanceComparing Summary
|
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.
Hi @alexeyt101,
Great work thus far! I've requested a few changes, mostly related to changing the name of the type of the error being raised.
Additionally, I wanted to bring up the question of what might happen if you use model_config
as a field name in a pydantic dataclass
? I haven't tested this out yet, but it'd be good to add a test for. I'm not sure whether or not we should error in that case...
Ping me when you've made the changes, I'd be happy to review again :). Thanks for your great work!
pydantic/errors.py
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use model-config-invalid-field-name
instead
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.
Done :)
docs/errors/usage_errors.md
Outdated
@@ -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} |
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
pydantic/_internal/_config.py
Outdated
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 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
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.
used model field
tests/test_config.py
Outdated
""" | ||
Test: raises PydandicUserError when 'model_config' is used | ||
as 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.
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 :).
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.
I agree, thanks :)
Hi @sydney-runkle PS: I've closed the PR accidentally and hope it's not a problem :) |
I'm fine with this for now. If it comes up in the future, we can readdress. I haven't seen any complaints thus far! |
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.
Requested a few more docs changes. Thanks!
docs/errors/usage_errors.md
Outdated
## `model_config` is used as a model field {#model-config-invalid-field-name} | ||
This error is raised when `model_config` is used as as 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.
## `model_config` is used as a model field {#model-config-invalid-field-name} | |
This error is raised when `model_config` is used as as property. | |
## `model_config` is used as a model field {#model-config-invalid-field-name} | |
This error is raised when `model_config` is used as the name of a field. |
pydantic/_internal/_config.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
'"model_config" cannot be used as a model field. Use "model_config" for model configuration', | |
'`model_config` cannot be used as a model field name. Use `model_config` for model configuration.', |
tests/test_config.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
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 | |
field_title = 'from_field' | |
@pydantic_dataclass(config={'title': config_title}) | |
class MyDataclass: | |
model_config: dict | |
m = MyDataclass(model_config={'title': field_title}) | |
assert m.model_config['title'] == field_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.
Thanks for you comments, I added all of it
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.
Looks great, thanks for your awesome work on this!
Just as a side note, I'm not too worried about case 2 from the original issue, as it's discouraged to use that style of config specification in V2. |
Change Summary
The PR aims to solve behavior when
pydantic
allows to definemodel_config
as a model property with no default value. It leads to confusing behavior:The MR offers to raise
PydanticUserError
in this case.Related issue number
fix #8469
Checklist
Selected Reviewer: @sydney-runkle