Skip to content

Commit

Permalink
Fix ComputedFieldInfo.wrapped_property pointer when a property sett…
Browse files Browse the repository at this point in the history
…er is assigned (pydantic#9892)
  • Loading branch information
tlambert03 authored Jul 18, 2024
1 parent 9cfb1f0 commit 7cfaeba
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pydantic/_internal/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ def __post_init__(self):

def _call_wrapped_attr(self, func: Callable[[Any], None], *, name: str) -> PydanticDescriptorProxy[ReturnType]:
self.wrapped = getattr(self.wrapped, name)(func)
if isinstance(self.wrapped, property):
# update ComputedFieldInfo.wrapped_property
from ..fields import ComputedFieldInfo

if isinstance(self.decorator_info, ComputedFieldInfo):
self.decorator_info.wrapped_property = self.wrapped
return self

def __get__(self, obj: object | None, obj_type: type[object] | None = None) -> PydanticDescriptorProxy[ReturnType]:
Expand Down
1 change: 1 addition & 0 deletions tests/test_computed_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def area(self, new_area: int):
assert s.model_dump() == {'side': 10.0, 'area': 100.0, 'area_string': '100.0 SQUARE UNITS'}
s.area = 64
assert s.model_dump() == {'side': 8.0, 'area': 64.0, 'area_string': '64.0 SQUARE UNITS'}
assert Square.model_computed_fields['area'].wrapped_property is Square.area


def test_computed_fields_del():
Expand Down

0 comments on commit 7cfaeba

Please sign in to comment.