(data: object, format_: PropertyFormat, format_template: str | None)
| 400 | |
| 401 | |
| 402 | async def _async_format_data(data: object, format_: PropertyFormat, format_template: str | None) -> object: |
| 403 | if isinstance(data, (date, datetime)): |
| 404 | if format_ == "iso8601": |
| 405 | return data.isoformat() |
| 406 | |
| 407 | if format_ == "custom" and format_template is not None: |
| 408 | return data.strftime(format_template) |
| 409 | |
| 410 | if format_ == "base64" and is_base64_file_input(data): |
| 411 | binary: str | bytes | None = None |
| 412 | |
| 413 | if isinstance(data, pathlib.Path): |
| 414 | binary = await anyio.Path(data).read_bytes() |
| 415 | elif isinstance(data, io.IOBase): |
| 416 | binary = data.read() |
| 417 | |
| 418 | if isinstance(binary, str): # type: ignore[unreachable] |
| 419 | binary = binary.encode() |
| 420 | |
| 421 | if not isinstance(binary, bytes): |
| 422 | raise RuntimeError(f"Could not read bytes from {data}; Received {type(binary)}") |
| 423 | |
| 424 | return base64.b64encode(binary).decode("ascii") |
| 425 | |
| 426 | return data |
| 427 | |
| 428 | |
| 429 | async def _async_transform_typeddict( |
no test coverage detected