You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While BaseModel.__setattr__ checks self.model_config.get('frozen', None), __delattr__ doesn't. I haven't actually run into this in practice, it was just something I noticed while poking around, but it seems like a simple omission that users wouldn't expect.
Immutability in Python is never strict. If developers are determined/stupid they can always modify a so-called "immutable" object.
but this seems like an easy mistake to make without being determined/stupid. This also means that hashing can break in a confusing way as demonstrated in the example code, which isn't the case in the example in the docs where there's a mutable non-hashable field.
Example Code
frompydanticimportBaseModelclassM(BaseModel, frozen=True):
a: intm=M(a=1)
s= {m}
print(mins) # Truedelm.a# should probably raise an errorprint(mins) # Falses= {m}
print(mins) # True
Thanks for filing this issue. I agree - this is problematic behavior and something we should fix 🐛 . I'd be happy to open a PR to address this, but you're also more than welcome to open one (I can review if so). 😄
Great questions. I think it'd be great to model the raised error after those raised in __setattr__ in the case of a frozen model. I think frozen_instance should be fine for the type. Regarding the input, I'd lean towards just None.
Initial Checks
Description
While
BaseModel.__setattr__
checksself.model_config.get('frozen', None)
,__delattr__
doesn't. I haven't actually run into this in practice, it was just something I noticed while poking around, but it seems like a simple omission that users wouldn't expect.I also note that https://docs.pydantic.dev/latest/concepts/models/#faux-immutability says:
but this seems like an easy mistake to make without being determined/stupid. This also means that hashing can break in a confusing way as demonstrated in the example code, which isn't the case in the example in the docs where there's a mutable non-hashable field.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: