We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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 float number into a timedelta truncates decimals (1.5 => timedelta(seconds=1)).
timedelta
1.5
timedelta(seconds=1)
I would expect to get decimals up to microseconds, as supported by timedelta.
# %% 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))
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
The text was updated successfully, but these errors were encountered:
@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!
Sorry, something went wrong.
No branches or pull requests
Initial Checks
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
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: