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

Parsing a timedelta from a float using TypeAdapter truncates decimals #9267

Open
1 task done
eltechnic0 opened this issue Apr 17, 2024 · 1 comment
Open
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@eltechnic0
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Parsing a float number into a timedelta truncates decimals (1.5 => timedelta(seconds=1)).

I would expect to get decimals up to microseconds, as supported by timedelta.

Example Code

# %%
from dataclasses import dataclass
from pydantic import TypeAdapter
from datetime import datetime, timedelta, timezone
from typing import Union

@dataclass
class Dummy:
    start_time: datetime
    duration: timedelta
    # duration: Union[timedelta, float]
    # duration: float
# %%
# item_data = {'start_time': datetime.now(timezone.utc).isoformat(), 'duration': timedelta(seconds=0.5)}
item_data = {'start_time': datetime.now(timezone.utc).isoformat(), 'duration': 0.5}
items = TypeAdapter(Dummy).validate_python(item_data)
print(items)

# prints:
# Dummy(start_time=datetime.datetime(...), duration=datetime.timedelta(0))

Python, Pydantic & OS Version

pydantic version: 2.7.0
        pydantic-core version: 2.18.1
          pydantic-core build: profile=release pgo=true
                 install path: /eng/ggambon/.local/lib/python3.9/site-packages/pydantic
               python version: 3.9.18 (main, Jan 26 2024, 05:26:53)  [GCC 9.3.1 20200408 (Red Hat 9.3.1-2)]
                     platform: Linux-3.10.0-1062.12.1.el7.x86_64-x86_64-with-glibc2.17
             related packages: typing_extensions-4.11.0
                       commit: unknown
@eltechnic0 eltechnic0 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 17, 2024
@sydney-runkle
Copy link
Member

@eltechnic0,

Hmm, I'm not able to reproduce on 2.7:

from dataclasses import dataclass
from pydantic import TypeAdapter
from datetime import datetime, timedelta, timezone
from typing import Union

@dataclass
class Dummy:
    start_time: datetime
    duration: timedelta
    # duration: Union[timedelta, float]
    # duration: float

# item_data = {'start_time': datetime.now(timezone.utc).isoformat(), 'duration': timedelta(seconds=0.5)}
item_data = {'start_time': datetime.now(timezone.utc).isoformat(), 'duration': 1.5}
items = TypeAdapter(Dummy).validate_python(item_data)
print(items)
#> Dummy(start_time=datetime.datetime(2024, 4, 18, 13, 34, 25, 433385, tzinfo=TzInfo(UTC)), duration=datetime.timedelta(seconds=1, microseconds=500000))

Closing as resolved, feel free to ping me if I've missed something!

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