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

Model computed_fields from a Model Type #8573

Closed
1 task done
SylvainGuieu opened this issue Jan 17, 2024 · 2 comments
Closed
1 task done

Model computed_fields from a Model Type #8573

SylvainGuieu opened this issue Jan 17, 2024 · 2 comments
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@SylvainGuieu
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Not sure if this is a bug or a feature.
So far (pydantic v2.5) , the model_computed_fields (a property) only work on model instances and not from model class.

I do not see obvious reason for that. See code below.
A solution would be to not use builtin python @property but a custom property with a re-writen __get__ method

Example Code

from pydantic import BaseModel, computed_field  

class M(BaseModel):
    x: float = 1.0 

    @computed_field
    @property 
    def tenx(self)->float:
       return self.x*10

>>> M.model_computed_fields
<property at 0x7fc4cbcf2860>
>>> M.model_computed_fields.__get__(None, M) # actually same as above 
<property at 0x7fc4cbcf2860>
>>> M.model_computed_fields.__get__(M, None) 
{'tenx': ComputedFieldInfo(wrapped_property=<property object at 0x7fc4cbc84360>, return_type=<class 'float'>, alias=None, alias_priority=None, title=None, description=None, examples=None, json_schema_extra=None, repr=True)}

Python, Pydantic & OS Version

pydantic version: 2.5.3
        pydantic-core version: 2.14.6
          pydantic-core build: profile=release pgo=false
                 install path: /Users/guieus/anaconda3/envs/hrmpm/lib/python3.9/site-packages/pydantic
               python version: 3.9.18 (main, Sep 11 2023, 08:38:23)  [Clang 14.0.6 ]
                     platform: macOS-10.16-x86_64-i386-64bit
             related packages: typing_extensions-4.9.0
@SylvainGuieu SylvainGuieu added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Jan 17, 2024
@SylvainGuieu SylvainGuieu changed the title Model computed field from a Model Type Model computed_fields from a Model Type Jan 17, 2024
@SylvainGuieu
Copy link
Author

I think one can actually add a property to the BaseModel Metaclass to fix it.
I did the following test, it does not seems to be a problem in python :

class MMeta(type):
     @property
     def model_computed_fields(cls):
          return 1 

class M(metaclass=MMeta):
     @property 
     def model_computed_fields(self):
           return 2  

assert M.model_computed_fields == 1
assert M().model_computed_fields == 2

@sydney-runkle
Copy link
Member

Fixed here:

#8437

Should be released with 2.6 soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

2 participants