Skip to content

Bug: Data loss in pendulum_dt.Duration when using model_copy(deep=True) #329

@tealgreen0503

Description

@tealgreen0503

Bug description

When using model_copy(deep=True) on a Pydantic model that includes a pydantic_extra_types.pendulum_dt.Duration field, the value of the copied Duration object becomes incorrect. It appears that parts of the duration (e.g., the 'weeks' component) are lost during the deep copy process, leading to data corruption.

To Reproduce

Here is a minimal, reproducible example to demonstrate the issue:

from pydantic import BaseModel
from pydantic_extra_types.pendulum_dt import Duration

class DurationModel(BaseModel):
    duration: Duration

# Initialize a model with a duration of 10 days.
# pendulum.Duration(days=10) is represented as 1 week and 3 days.
original_model = DurationModel(duration=Duration(days=10))
print(f"Original model: {original_model}")

# Create a deep copy of the model.
copied_model = original_model.model_copy(deep=True)
print(f"Copied model:   {copied_model}")

# Verify the duration values
print(f"Original duration: {original_model.duration.in_days()} days")
print(f"Copied duration:   {copied_model.duration.in_days()} days")

Actual behavior

The weeks component of the Duration object is lost after the deep copy. The total duration is incorrectly reduced.

Original model: duration=Duration(weeks=1, days=3)
Copied model:   duration=Duration(days=3)
Original duration: 10 days
Copied duration:   3 days

Expected behavior

The copied_model should be an exact deep copy of the original_model, preserving the full duration. The expected output is:

Original model: duration=Duration(weeks=1, days=3)
Copied model:   duration=Duration(weeks=1, days=3)
Original duration: 10 days
Copied duration:   10 days

Environment

  • Python version: 3.11
  • pydantic version: 2.11.0
  • pydantic_extra_types version: 2.10.5

This issue seems to stem from how the Duration type is handled during Pydantic's deep copy mechanism.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions