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

Do not require validate_assignment to use Field.frozen #7103

Merged
merged 8 commits into from Aug 15, 2023

Conversation

Viicos
Copy link
Contributor

@Viicos Viicos commented Aug 12, 2023

Change Summary

I find it a bit unusual to have the following working:

from pydantic import BaseModel, Field

class Model(BaseModel):
    a: int = 1
    model_config = {"frozen": True}

m = Model()
m.a = 2
#> Raises ValidationError

But not the following:

from pydantic import BaseModel, Field

class Model(BaseModel):
    a: int = Field(frozen=True)

m = Model(a=1)
m.a = 2
#> No ValidationError, unless model_config.validate_assignment is set to True

If you think the current behavior is as designed, I can revert my commit but I think a note on the Field.frozen docstring would be required.

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

@hramezani
Copy link
Member

It is intended behavior. The second example will work if config['validate_assignment'] = True. You can find it in the example in doc.

Probably we can improve the the docstring and documentation here

@Viicos
Copy link
Contributor Author

Viicos commented Aug 14, 2023

Then should the first example I gave follows the same idea (requiring validate_assignment=True)? As I feel this is a bit inconsistent

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

otherwise LGTM

pydantic/main.py Outdated Show resolved Hide resolved
@Viicos
Copy link
Contributor Author

Viicos commented Aug 14, 2023

Please review

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks so much for the contribution.

@samuelcolvin samuelcolvin merged commit bd6b723 into pydantic:main Aug 15, 2023
48 checks passed
@Viicos Viicos deleted the no-validate-assignment-frozen branch August 15, 2023 09:20
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.

None yet

3 participants