Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_typed_dict_fields(typed_dict_type: Type[Dict[str, Any]]) -> None:
fields = {}
for name, type_ in typed_dict_type.__annotations__.items():
default = getattr(typed_dict_type, name, dataclasses.MISSING)
if isinstance(default, dataclasses.Field):
fields[name] = default
else:
fields[name] = dataclasses.field(default=default)
fields[name].name = name
fields[name].type = type_
fields[name]._field_type = dataclasses._FIELD # type: ignore
typed_dict_type.__dataclass_fields__ = fields # type: ignore
def _dataclass__str__(self):
fields = getattr(self, _FIELDS, None)
if fields is None:
return ""
fields = [f for f in fields.values() if f._field_type is _FIELD]
fields_str = ', '.join(f"{f.name}={getattr(self, f.name)}" for f in fields)
return f"{self.__class__.__qualname__}({fields_str})"
def wrapper(cls):
cls = stdlib_dataclass( # type: ignore
cls, init=init, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen
)
#
type_field = Field(
default=MISSING,
default_factory=MISSING,
init=False,
hash=None,
repr=False,
compare=False,
metadata=None,
)
type_field.name = "type_"
type_field._field_type = _FIELD
cls.__dataclass_fields__["type_"] = type_field
cls.type_ = class_type(cls)
cls = dataclass_json(cls)
return cls
def __str__(self):
fields = getattr(self, _FIELDS, None)
if fields is None:
return ""
fields = [f for f in fields.values() if f._field_type is _FIELD]
fields_str = ', '.join(f"{f.name}={getattr(self, f.name)}" for f in fields)
return f"{self.__class__.__qualname__}({fields_str})"