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

Conversation

alexeyt101
Copy link
Contributor

@alexeyt101 alexeyt101 commented Mar 13, 2024

Change Summary

The PR aims to solve behavior when pydantic allows to define model_config as a model property with no default value. It leads to confusing behavior:

from pydantic import BaseModel


class MyModel(BaseModel):
    model_config: str


if __name__ == '__main__':
    m = MyModel(model_config='Test')
    print(m.model_config)
    """
    {}
    """

The MR offers to raise PydanticUserError in this case.

Related issue number

fix #8469

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @sydney-runkle

@alexeyt101
Copy link
Contributor Author

alexeyt101 commented Mar 13, 2024

please review @sydney-runkle

Copy link

codspeed-hq bot commented Mar 13, 2024

CodSpeed Performance Report

Merging #9004 will not alter performance

Comparing alexeyt101:issue-8469 (2149ee1) with main (325ddf6)

Summary

✅ 10 untouched benchmarks

Copy link
Member

@sydney-runkle sydney-runkle left a 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!

@@ -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 :)

@@ -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

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

Comment on lines 802 to 805
"""
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 :)

@alexeyt101 alexeyt101 closed this Mar 17, 2024
@alexeyt101 alexeyt101 reopened this Mar 17, 2024
@alexeyt101
Copy link
Contributor Author

Hi @sydney-runkle
I think we shouldn't raise the error if model_config is used as a model field in dataclass because it works clear and expected. I added a test that covers the case.

PS: I've closed the PR accidentally and hope it's not a problem :)

@sydney-runkle
Copy link
Member

I think we shouldn't raise the error if model_config is used as a model field in dataclass

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!

Copy link
Member

@sydney-runkle sydney-runkle left a 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!

Comment on lines 1092 to 1093
## `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.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
## `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.

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',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'"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.',

Comment on lines 812 to 820
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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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

Copy link
Contributor Author

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

Copy link
Member

@sydney-runkle sydney-runkle left a 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!

@sydney-runkle
Copy link
Member

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.

@sydney-runkle sydney-runkle merged commit 1cfb22e into pydantic:main Mar 17, 2024
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

It is possible to instantiate model with model_config field without exceptions
2 participants