[docs]defunstructure_datetime_as_timestamp(v:datetime)->float:"""Unstructure a datetime into a UNIX timestamp. `datetime.timestamp` interprets naive datetimes as local time, which would make the result depend on the timezone of the machine unstructuring them. The matching structure hooks read timestamps back as UTC, so naive datetimes are treated as UTC here too, keeping both ends of the round-trip in agreement. """ifv.tzinfoisNone:v=v.replace(tzinfo=timezone.utc)returnv.timestamp()
T=TypeVar("T")P=ParamSpec("P")
[docs]defwrap(_:Callable[P,Any])->Callable[[Callable[...,T]],Callable[P,T]]:"""Wrap a `Converter` `__init__` in a type-safe way."""defimpl(x:Callable[...,T])->Callable[P,T]:returnxreturnimpl
[docs]defis_primitive_enum(type:Any,include_bare_enums:bool=False)->bool:"""Is this a string or int enum that can be passed through?"""returnis_subclass(type,Enum)and(is_subclass(type,(str,int))or(include_bare_enumsandtype.mro()[1:]==Enum.mro()))
[docs]defliterals_with_enums_unstructure_factory(typ:Any,converter:Converter)->UnstructureHook:"""An unstructure hook factory for literals containing enums. If all contained enums can be passed through (their unstructure hook is `identity`), the entire literal can also be passed through. """ifall(converter.get_unstructure_hook(type(arg))==identityforarginget_args(typ)):returnidentityreturnconverter.unstructure