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

Computed_fields outputs a property object instead of the value #7458

Closed
1 task done
Galsor opened this issue Sep 15, 2023 · 4 comments
Closed
1 task done

Computed_fields outputs a property object instead of the value #7458

Galsor opened this issue Sep 15, 2023 · 4 comments
Assignees

Comments

@Galsor
Copy link

Galsor commented Sep 15, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Hello,

When a model inherits from another model, overriding one of its fields with a computed_field, the overridden computed field returns a property object instead of the value produced by the function.

# This works fine
class Plane(BaseModel):
 
@computed_field
def name(self) -> str:
   return 'A310'

Plane().name
>>> 'A310' 

# This doesn't
class Aircraft(BaseModel):
   name: str

class Plane(Aircraft):
   category: str = 'A'
   code: str = '310'

   @computed_field
   def name(self) -> str:
      return self.category + self.code

Plane().name
>>> '<property object at 0x7ff1ed10b8d0>'

I am running pydantic==2.3.0 (pydantic_core==2.6.3)

Example Code

from pydantic import BaseModel, computed_field

class Aircraft(BaseModel):
   name: str

class Plane(Aircraft):
   category: str = 'A'
   code: str = '310'

   @computed_field
   def name(self) -> str:
      return self.category + self.code

Python, Pydantic & OS Version

pydantic version: 2.3.0
        pydantic-core version: 2.6.3
          pydantic-core build: profile=release pgo=true
                 install path: /workspaces/project/.venv/lib/python3.10/site-packages/pydantic
               python version: 3.10.8 (main, Aug 19 2023, 00:31:12) [GCC 9.4.0]
                     platform: Linux-5.15.0-1041-azure-x86_64-with-glibc2.31
     optional deps. installed: ['typing-extensions']
@Galsor Galsor added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Sep 15, 2023
@sydney-runkle
Copy link
Member

Hi,

Thanks for your question. We recently addressed this issue in this PR, and these changes should get released with the next version. The changes make it such that an error is raised when a computed_field is defined in a subclass such that it overrides a field from a superclass.

We decided to raise an error here because this behavior is both prohibited by dataclasses and discouraged by mypy.

If you're looking for a workaround, using a model validator with mode="after" as shown in this example could help.

Let me know if you have any more questions! 😄

@sydney-runkle sydney-runkle removed unconfirmed Bug not yet confirmed as valid/applicable bug V2 Bug related to Pydantic V2 labels Sep 15, 2023
@Galsor
Copy link
Author

Galsor commented Sep 15, 2023

Ok...
I am interested to understand this choice of forbidding this behavior. Is there any discussion or resources somewhere to justify this choice?

@sydney-runkle
Copy link
Member

Sure thing - we've been discussing this decision in #7250.

@sydney-runkle
Copy link
Member

Closing in favor of continuing discussion on #7250.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants