import datetime import dateutil from apistar.typesystem import TypeSystemError class Datetime(datetime.datetime): native_type = datetime.datetime def __new__(cls, *args, **kwargs) -> datetime: if args and isinstance(args[0], cls.native_type): return args[0] if args and isinstance(args[0], str): try: return dateutil.parser.parse(args[0]) except ValueError: raise TypeSystemError(cls=cls, code='type') from None return cls.native_type(*args, **kwargs)