Skip to content

Commit

Permalink
Add input types to chrono type casters (#931)
Browse files Browse the repository at this point in the history
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 authored Mar 2, 2025
1 parent a5f5bbf commit ea818ee
Showing 1 changed file with 14 additions and 2 deletions.
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

0 comments on commit ea818ee

Please sign in to comment.