Skip to content

Conversation

Viicos
Copy link
Member

@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
Member 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-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author labels Aug 14, 2023
@Viicos
Copy link
Member Author

Viicos commented Aug 14, 2023

Please review

@pydantic-hooky pydantic-hooky bot added ready for review and removed awaiting author revision awaiting changes from the PR author labels Aug 14, 2023
@pydantic-hooky pydantic-hooky bot assigned samuelcolvin and unassigned Viicos Aug 14, 2023
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
@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.

3 participants