18 lines
585 B
Python
18 lines
585 B
Python
__all__ = ('ResponseType', 'cast_to_response')
|
|
|
|
from typing import Any, TypeAlias
|
|
|
|
ResponseType: TypeAlias = list['ResponseType'] | dict[str, 'ResponseType'] | float | int | bool | str | None
|
|
|
|
|
|
def cast_to_response(target: Any) -> ResponseType:
|
|
match target:
|
|
case str() | int() | float() | bool() | None:
|
|
return target
|
|
case list() | tuple():
|
|
return list(map(cast_to_response, target))
|
|
case dict():
|
|
return {str(key): cast_to_response(value) for key, value in target.items()}
|
|
case _:
|
|
return str(target)
|