Skip to content

Commit 04445cb

Browse files
committed
Removed None initialization into response
1 parent d425fe1 commit 04445cb

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

runware/utils.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import mimetypes
1313
import inspect
1414
from typing import Any, Dict, List, Union, Optional, TypeVar, Type, Coroutine, get_type_hints, get_origin, get_args
15-
from dataclasses import fields, is_dataclass
15+
from dataclasses import MISSING, fields, is_dataclass
1616
from enum import Enum
1717
from .types import (
1818
Environment,
@@ -884,6 +884,21 @@ def handle_polling_done(task):
884884
pass
885885

886886

887+
def _compact_dataclass_repr(self: Any) -> str:
888+
889+
parts = []
890+
for f in fields(self):
891+
value = getattr(self, f.name)
892+
if (
893+
value is None
894+
or (isinstance(value, str) and not value.strip())
895+
or (f.name == "additional_fields" and not value)
896+
):
897+
continue
898+
parts.append(f"{f.name}={value!r}")
899+
return f"{type(self).__name__}({', '.join(parts)})"
900+
901+
887902
def instantiateDataclass(dataclass_type: Type[Any], data: dict) -> Any:
888903
"""
889904
Instantiates a dataclass object from a dictionary, filtering out any unknown attributes.
@@ -898,11 +913,7 @@ def instantiateDataclass(dataclass_type: Type[Any], data: dict) -> Any:
898913
filtered_data = {}
899914

900915
for k, v in data.items():
901-
if k not in valid_fields:
902-
continue
903-
904-
if v is None:
905-
filtered_data[k] = None
916+
if k not in valid_fields or v is None or (isinstance(v, str) and not v.strip()):
906917
continue
907918

908919
field_type = hints.get(k)
@@ -975,7 +986,19 @@ def instantiateDataclass(dataclass_type: Type[Any], data: dict) -> Any:
975986
filtered_data[k] = field_type(v)
976987
else:
977988
filtered_data[k] = v
978-
989+
990+
for f in fields(dataclass_type):
991+
if (
992+
f.name in filtered_data
993+
or f.default is not MISSING
994+
or f.default_factory is not MISSING
995+
):
996+
continue
997+
if hints.get(f.name) is str:
998+
filtered_data[f.name] = ""
999+
1000+
if is_dataclass(dataclass_type) and dataclass_type.__repr__ is not _compact_dataclass_repr:
1001+
dataclass_type.__repr__ = _compact_dataclass_repr
9791002
return dataclass_type(**filtered_data)
9801003

9811004

0 commit comments

Comments
 (0)