How to use the dataclasses._FIELD function in dataclasses

To help you get started, we’ve selected a few dataclasses examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dutradda / jsondaora / jsondaora / decorator.py View on Github external
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
github icon-project / loopchain / loopchain / blockchain / blocks / block.py View on Github external
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})"
github raiden-network / raiden / raiden / storage / serialization / dataclass.py View on Github external
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
github icon-project / loopchain / loopchain / blockchain / transactions / transaction.py View on Github external
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})"