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

Add input types to chrono type casters #931

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add input types to chrono type casters
The type casters in `stl/chrono.h` accept various types in their
`from_python` calls, but they currently just use a name for their
output type.

This uses `io_name` to differentiate, adding the various acceptable
types that the casters permit at runtime
  • Loading branch information
tjstum committed Feb 25, 2025
commit 34ef68601960aa6b8076c82d5471d85d4337a4d1
16 changes: 14 additions & 2 deletions include/nanobind/stl/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ template <typename type> class duration_caster {
return pack_timedelta(dd.count(), ss.count(), us.count());
}

NB_TYPE_CASTER(type, const_name("datetime.timedelta"))
#if PY_VERSION_HEX < 0x03090000
NB_TYPE_CASTER(type, io_name("typing.Union[datetime.timedelta, float]",
"datetime.timedelta"))
#else
NB_TYPE_CASTER(type, io_name("datetime.timedelta | float",
"datetime.timedelta"))
#endif
};

template <class... Args>
Expand Down Expand Up @@ -208,7 +214,13 @@ class type_caster<std::chrono::time_point<std::chrono::system_clock, Duration>>
localtime.tm_sec,
(int) us.count());
}
NB_TYPE_CASTER(type, const_name("datetime.datetime"))
#if PY_VERSION_HEX < 0x03090000
NB_TYPE_CASTER(type, io_name("typing.Union[datetime.datetime, datetime.date, datetime.time]",
"datetime.datetime"))
#else
NB_TYPE_CASTER(type, io_name("datetime.datetime | datetime.date | datetime.time",
"datetime.datetime"))
#endif
};

// Other clocks that are not the system clock are not measured as
Expand Down
Loading