pydantic-changedetect icon indicating copy to clipboard operation
pydantic-changedetect copied to clipboard

computed_field

Open rosscdh-tpg opened this issue 1 year ago • 1 comments

Hi there, thanks for the great useful library.

A small issue I encounter when using computed fields is that they are evaluated for changes as well (when in effect they should not)?

Or am I missing something?

i.e.

    @computed_field(return_type=dict)
    def meta(self):
        return dict(
            detail_url=self.get_object_detail_path(id=self.get_detail_view_pk()),
        )

on save i get a

(Pdb) self.model_has_changed
*** KeyError: 'meta'

at line changedetect.py:92-93

        for field_name, model_field in self_compat.model_fields.items():
            field_value = self.__dict__[field_name]

perhaps

 field_value = self.__dict__.get(field_name, None)

rosscdh-tpg avatar May 02 '24 00:05 rosscdh-tpg

There currently is no code to handle computed fields. I will look into this :)

ddanier avatar May 06 '24 07:05 ddanier

I just created #34 to fix this issue. Sadly I cannot reproduce the issue, see https://github.com/team23/pydantic-changedetect/pull/34 for details. Can you point me into the right direction?

ddanier avatar May 29 '24 16:05 ddanier

perhaps something to do with the type=dict?

my monkeypatch looks like

        for field_name, model_field in self_compat.model_fields.items():
            #
            # NOTE! this here is the fix
            # raised: https://github.com/team23/pydantic-changedetect/issues/33
            #
            field_value = self.__dict__.get(field_name, None)

rosscdh-tpg avatar Jun 05 '24 03:06 rosscdh-tpg

@ddanier working with @rosscdh-tpg , I think we have a bug on our side not on your library. Doing last round of testing but if confirmed you can close #34 as no fix will be required. We will revert back to you shortly

sandrogattuso avatar Jun 13 '24 09:06 sandrogattuso

Thanks for the update. I will close this issue then.

ddanier avatar Jun 14 '24 06:06 ddanier

yep, we had a classmethod that was updating the definition of the class by mistake, this caused the computed field to be transformed in a Decorator on subsequent instantiated models. Sorry for the inconvenience

sandrogattuso avatar Jun 14 '24 12:06 sandrogattuso