Added fix for serializing PIL images (#1619)

This commit is contained in:
wassaf shahzad 2023-08-18 11:17:42 -07:00 committed by GitHub
parent ed5727328e
commit 0be5d670ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View File

@ -381,7 +381,7 @@ def format_image_data(value: Type) -> str:
return f"data:image/png;base64,{base64_image}"
def format_state(value: Any) -> Dict:
def format_state(value: Any) -> Any:
"""Recursively format values in the given state.
Args:
@ -397,6 +397,10 @@ def format_state(value: Any) -> Dict:
if isinstance(value, dict):
return {k: format_state(v) for k, v in value.items()}
# Handle lists, sets, typles.
if isinstance(value, types.StateIterBases):
return [format_state(v) for v in value]
# Return state vars as is.
if isinstance(value, types.StateBases):
return value

View File

@ -14,6 +14,7 @@ GenericType = Union[Type, _GenericAlias]
# Valid state var types.
PrimitiveType = Union[int, float, bool, str, list, dict, set, tuple]
StateVar = Union[PrimitiveType, Base, None]
StateIterVar = Union[list, set, tuple]
def get_args(alias: _GenericAlias) -> Tuple[Type, ...]:
@ -198,3 +199,4 @@ def is_backend_variable(name: str) -> bool:
# Store this here for performance.
StateBases = get_base_class(StateVar)
StateIterBases = get_base_class(StateIterVar)