Skip to content

Timedelta string with hour value greater than 23 is not supported (breaking change) #8673

@gusutabopb

Description

@gusutabopb

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

In Pydantic v1, timedelta strings in the format HH:MM:SS with HH greater than 23 worked fine (i.e. the example code below works). This format is also supported by pandas.Timedelta by the way.

In Pydantic v2, the example code below fails with the following exception:

ValidationError: 1 validation error for MyModel
td
  Input should be a valid timedelta, hour value is outside expected range of 0-23 [type=time_delta_parsing, input_value='26:30:00', input_type=str]
    For further information visit https://errors.pydantic.dev/2.6/v/time_delta_parsing

As a workaround I have to change string to:

  • 26:30:00 -> PT26H30M or 1 day 02:30
  • 24:00:00 -> PT24H or 1 day 00:00

IMO, ideally HH:MM:SS with HH greater than 23 should be supported in Pydantic v2. Or at the very least, this should be documented as a breaking change in the Migration Guide.

Example Code

import datetime
import pydantic
from pydantic import BaseModel

class MyModel(BaseModel):
    td: datetime.timedelta

print(f"{pydantic.__version__=}")
assert MyModel(td="26:30:00").td == datetime.timedelta(days=1, seconds=9000)
assert MyModel(td="24:00:00").td == datetime.timedelta(days=1)

Python, Pydantic & OS Version

pydantic version: 2.6.0
        pydantic-core version: 2.16.1
          pydantic-core build: profile=release pgo=true
                 install path: /home/gbezerra/ipynb/.venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.3 (main, Jun  7 2023, 13:51:08) [GCC 11.3.0]
                     platform: Linux-5.19.0-35-generic-x86_64-with-glibc2.35
             related packages: email-validator-2.0.0 typing_extensions-4.6.3 fastapi-0.83.0
                       commit: unknown

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions