Skip to content

Commit ea818ee

Browse files
authored
Add input types to chrono type casters (#931)
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
1 parent a5f5bbf commit ea818ee

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

include/nanobind/stl/chrono.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ template <typename type> class duration_caster {
9999
return pack_timedelta(dd.count(), ss.count(), us.count());
100100
}
101101

102-
NB_TYPE_CASTER(type, const_name("datetime.timedelta"))
102+
#if PY_VERSION_HEX < 0x03090000
103+
NB_TYPE_CASTER(type, io_name("typing.Union[datetime.timedelta, float]",
104+
"datetime.timedelta"))
105+
#else
106+
NB_TYPE_CASTER(type, io_name("datetime.timedelta | float",
107+
"datetime.timedelta"))
108+
#endif
103109
};
104110

105111
template <class... Args>
@@ -208,7 +214,13 @@ class type_caster<std::chrono::time_point<std::chrono::system_clock, Duration>>
208214
localtime.tm_sec,
209215
(int) us.count());
210216
}
211-
NB_TYPE_CASTER(type, const_name("datetime.datetime"))
217+
#if PY_VERSION_HEX < 0x03090000
218+
NB_TYPE_CASTER(type, io_name("typing.Union[datetime.datetime, datetime.date, datetime.time]",
219+
"datetime.datetime"))
220+
#else
221+
NB_TYPE_CASTER(type, io_name("datetime.datetime | datetime.date | datetime.time",
222+
"datetime.datetime"))
223+
#endif
212224
};
213225

214226
// Other clocks that are not the system clock are not measured as

0 commit comments

Comments
 (0)