Python dataclasses.dataclass() Examples

The following are 30 code examples of dataclasses.dataclass(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module dataclasses , or try the search function .
Example #1
Source File: dynamics.py    From whynot with MIT License 6 votes vote down vote up
def __init__(self, config_class, time, **kwargs):
        """Specify an intervention in the dynamical system.

        Parameters
        ----------
            time: int
                Time of the intervention.
            config_class:
                The Config class, a child class of dataclass.
            kwargs: dict
                Only valid keyword arguments are parameters of the config class.

        """
        self.time = time
        config_args = set(f.name for f in dataclasses.fields(config_class))
        for arg in kwargs:
            if arg not in config_args:
                raise TypeError(f"__init__() got an unexpected keyword argument {arg}!")
        self.updates = kwargs 
Example #2
Source File: test_streamable.py    From chia-blockchain with Apache License 2.0 6 votes vote down vote up
def test_variablesize(self):
        @dataclass(frozen=True)
        @streamable
        class TestClass2(Streamable):
            a: uint32
            b: uint32
            c: bytes

        a = TestClass2(uint32(1), uint32(2), b"3")
        bytes(a)

        @dataclass(frozen=True)
        @streamable
        class TestClass3(Streamable):
            a: int

        b = TestClass3(1)
        try:
            bytes(b)
            assert False
        except NotImplementedError:
            pass 
Example #3
Source File: test_streamable.py    From chia-blockchain with Apache License 2.0 6 votes vote down vote up
def test_recursive_json(self):
        @dataclass(frozen=True)
        @streamable
        class TestClass1(Streamable):
            a: List[uint32]

        @dataclass(frozen=True)
        @streamable
        class TestClass2(Streamable):
            a: uint32
            b: List[Optional[List[TestClass1]]]
            c: bytes32

        tc1_a = TestClass1([uint32(1), uint32(2)])
        tc1_b = TestClass1([uint32(4), uint32(5)])
        tc1_c = TestClass1([uint32(7), uint32(8)])

        tc2 = TestClass2(
            uint32(5), [[tc1_a], [tc1_b, tc1_c], None], bytes32(bytes([1] * 32))
        )
        assert TestClass2.from_json_dict(tc2.to_json_dict()) == tc2 
Example #4
Source File: report.py    From professional-services with Apache License 2.0 6 votes vote down vote up
def __init__(self, config, step, timestamp, client=None, delete=False):

        # Init dataclass fields from SLO config and Error Budget Policy
        self.__set_fields(**config,
                          **step,
                          lambdas={
                              'slo_target': float,
                              'alerting_burn_rate_threshold': int
                          })

        # Set other fields
        self.window = int(step['measurement_window_seconds'])
        self.timestamp = int(timestamp)
        self.timestamp_human = utils.get_human_time(timestamp)

        # Get backend results
        result = self.run_backend(config, client=client, delete=delete)
        if result:
            self.build(step, result) 
Example #5
Source File: report.py    From professional-services with Apache License 2.0 6 votes vote down vote up
def __set_fields(self, lambdas={}, **kwargs):
        """Set all fields in dataclasses from configs passed and apply function
        on values whose key match one in the dictionaries.

        Args:
            lambdas (dict): Dict {key: function} to apply a function on certain
            kwargs (dict): Dict of key / values to set in dataclass.
        """
        names = set(f.name for f in fields(self))
        for name in names:
            if name not in kwargs:
                continue
            value = kwargs[name]
            if name in lambdas.keys():
                value = lambdas[name](value)
            setattr(self, name, value) 
Example #6
Source File: test_dynamics.py    From whynot with MIT License 6 votes vote down vote up
def test_basestate():
    """Test the basic state object."""

    @dataclasses.dataclass
    class State(wn.dynamics.BaseState):
        state1: float = 0
        state2: float = 1
        state3: float = 3

    assert State.num_variables() == 3
    assert State.variable_names() == ["state1", "state2", "state3"]

    state2 = [2]
    state = State(state2=state2)
    assert state.num_variables() == 3

    values = state.values()
    assert values[0] == 0
    assert values[1] == [2]
    assert values[2] == 3

    # Ensure values are shallow copied
    assert values[1] is state2 
Example #7
Source File: test_dynamics.py    From whynot with MIT License 6 votes vote down vote up
def test_baseconfig():
    """Test the basic configuration object."""

    @dataclasses.dataclass
    class Config(wn.dynamics.BaseConfig):
        param1: float = 0
        param2: float = 23

    config = Config()

    assert config.parameter_names() == ["param1", "param2"]

    intervention = wn.dynamics.BaseIntervention(Config, 1970, param1=19)

    assert config.param1 == 0
    updated = config.update(intervention)
    assert config.param1 == 0
    assert config.param2 == 23
    assert updated.param1 == 19
    assert updated.param2 == 23 
Example #8
Source File: __init__.py    From dataclasses-jsonschema with MIT License 6 votes vote down vote up
def to_dict(self, omit_none: bool = True, validate: bool = False, validate_enums: bool = True) -> JsonDict:
        """Converts the dataclass instance to a JSON encodable dict, with optional JSON schema validation.

        If omit_none (default True) is specified, any items with value None are removed
        """
        data = {}
        for f in self._get_fields():
            value = getattr(self, f.field.name)
            try:
                value = self._encode_field(f.field.type, value, omit_none)
            except UnknownEnumValueError as e:
                warnings.warn(str(e))

            if omit_none and value is None:
                continue
            if value is NULL:
                value = None
            data[f.mapped_name] = value

        if self.__discriminator_name is not None:
            data[self.__discriminator_name] = self.__class__.__name__

        if validate:
            self._validate(data, validate_enums)
        return data 
Example #9
Source File: test_kernel.py    From cjworkbench with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_validate_works_with_dataclasses(self):
        """
        Test we can compile @dataclass

        @dataclass inspects `sys.modules`, so the module needs to be in
        `sys.modules` when @dataclass is run.
        """
        mod = _compile(
            "foo",
            textwrap.dedent(
                """
                from __future__ import annotations
                from dataclasses import dataclass

                def render(table, params):
                    return table

                @dataclass
                class A:
                    y: int
                """
            ).encode("utf-8"),
        )
        self.kernel.validate(mod)  # do not raise 
Example #10
Source File: types.py    From cjworkbench with GNU Affero General Public License v3.0 5 votes vote down vote up
def coerce_list(
        cls, error_or_errors: Optional[mtypes.RenderErrors]
    ) -> List[ProcessResultError]:
        """Convert a single error or a list of errors as returned by module to a list of members of this dataclass.
        
        Raises ValueError, if some element of the list cannot be coerced to a member of this dataclass
        """
        if error_or_errors is None or (
            isinstance(error_or_errors, str) and not error_or_errors
        ):
            return []
        elif isinstance(error_or_errors, list):
            return [cls.coerce(error) for error in error_or_errors]
        else:
            return [cls.coerce(error_or_errors)] 
Example #11
Source File: base.py    From dffml with MIT License 5 votes vote down vote up
def make_config(cls_name: str, fields, *args, namespace=None, **kwargs):
    """
    Function to create a dataclass
    """
    if namespace is None:
        namespace = {}
    namespace.setdefault("_fromdict", classmethod(_fromdict))
    namespace.setdefault(
        "_replace",
        lambda self, *args, **kwargs: dataclasses.replace(
            self, *args, **kwargs
        ),
    )
    namespace.setdefault("_asdict", config_asdict)
    kwargs["eq"] = True
    kwargs["init"] = True
    # Ensure non-default arguments always come before default arguments
    fields_non_default = []
    fields_default = []
    for name, cls, field in fields:
        if (
            field.default is not dataclasses.MISSING
            or field.default_factory is not dataclasses.MISSING
        ):
            fields_default.append((name, cls, field))
        else:
            fields_non_default.append((name, cls, field))
    fields = fields_non_default + fields_default
    # Create dataclass
    return dataclasses.make_dataclass(
        cls_name, fields, *args, namespace=namespace, **kwargs
    ) 
Example #12
Source File: test_json_serialize.py    From ReAgent with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_json_serialize_nested(self):
        @dataclasses.dataclass
        class Test1:
            x: int

        @dataclasses.dataclass
        class Test2:
            x: typing.List[Test1]
            y: typing.Dict[str, Test1]

        t = Test2(x=[Test1(x=3), Test1(x=4)], y={"1": Test1(x=5), "2": Test1(x=6)})
        self.assertEqual(t, json_to_object(object_to_json(t), Test2)) 
Example #13
Source File: types.py    From cjworkbench with GNU Affero General Public License v3.0 5 votes vote down vote up
def coerce(cls, value: mtypes.RenderError) -> ProcessResultError:
        """Convert an error as returned by module to a member of this dataclass.
        
        Raises ValueError, if the value cannot be converted to a member of this dataclass
        """
        if not value:
            raise ValueError("Error cannot be empty")
        elif isinstance(value, (str, tuple)):
            return cls(I18nMessage.coerce(value))
        elif isinstance(value, dict):
            try:
                message = I18nMessage.coerce(value["message"])
            except KeyError:
                raise ValueError("Missing 'message' in %s" % value)

            try:
                quick_fixes = [QuickFix.coerce(qf) for qf in value["quickFixes"]]
            except KeyError:
                raise ValueError("Missing 'quickFixes' in %s" % value)

            return cls(message, quick_fixes)
        else:
            raise ValueError(
                "Values of type %s cannot be coerced to module errors"
                % type(value).__name__
            ) 
Example #14
Source File: test_type_checking.py    From chia-blockchain with Apache License 2.0 5 votes vote down vote up
def test_StrictDataClassBad(self):
        @dataclass(frozen=True)
        @strictdataclass
        class TestClass2:
            a: int
            b = 0

        assert TestClass2(25)
        try:
            TestClass2(1, 2)  # type: ignore
            assert False
        except TypeError:
            pass 
Example #15
Source File: logged_dataset.py    From ReAgent with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __getitem__(self, idx) -> dataclass:
        """
        Args:
            idx: index of the sample

        Returns:
            tuple of features, action, and reward at idx
        """
        pass 
Example #16
Source File: test_type_checking.py    From chia-blockchain with Apache License 2.0 5 votes vote down vote up
def test_StrictDataClass(self):
        @dataclass(frozen=True)
        @strictdataclass
        class TestClass1:
            a: int
            b: str

        good: TestClass1 = TestClass1(24, "!@12")
        assert TestClass1.__name__ == "TestClass1"
        assert good
        assert good.a == 24
        assert good.b == "!@12"
        good2 = TestClass1(52, bytes([1, 2, 3]))  # type: ignore
        assert good2.b == str(bytes([1, 2, 3])) 
Example #17
Source File: field.py    From pygraphy with MIT License 5 votes vote down vote up
def __new__(cls, name, bases, attrs):
        attrs['__fields__'] = {}
        attrs['__description__'] = None
        attrs['__validated__'] = False
        cls = dataclasses.dataclass(super().__new__(cls, name, bases, attrs))
        sign = inspect.signature(cls)
        cls.__description__ = inspect.getdoc(cls)
        for name, t in sign.parameters.items():
            cls.__fields__[to_snake_case(name)] = Field(
                name=to_snake_case(name), _ftype=t.annotation, description=None, _obj=cls
            )
        return cls 
Example #18
Source File: core_test.py    From CrossHair with MIT License 5 votes vote down vote up
def test_expr_name_resolution(self):
        '''
        dataclass() generates several methods. It can be tricky to ensure
        that invariants for these methods can resolve names in the 
        correct namespace.
        '''
        self.assertEqual(*check_messages(analyze_class(ReferenceHoldingClass), state=MessageType.CONFIRMED)) 
Example #19
Source File: types.py    From cjworkbench with GNU Affero General Public License v3.0 5 votes vote down vote up
def coerce(cls, value: mtypes.Message) -> I18nMessage:
        """ Convert an internationalized message as returned from modules to an object of this dataclass.
        
        Raises:
        - ValueError, if the value is a list of the wrong length or if the value is of a non-supported type
        
        """
        if isinstance(value, str):
            return cls.TODO_i18n(value)
        elif isinstance(value, tuple):
            if len(value) < 2 or len(value) > 3:
                raise ValueError(
                    "This tuple cannot be coerced to I18nMessage: %s" % value
                )
            if not isinstance(value[0], str):
                raise ValueError(
                    "Message ID must be string, got %s" % type(value[0]).__name__
                )
            if not isinstance(value[1], dict):
                raise ValueError(
                    "Message arguments must be a dict, got %s" % type(value[1]).__name__
                )
            if len(value) == 3:
                source = value[2]
                if source not in ["module", "cjwmodule", "cjwparse", None]:
                    raise ValueError("Invalid i18n message source %r" % source)
            else:
                source = None
            return cls(value[0], value[1], source)
        else:
            raise ValueError(
                "%s is of type %s, which cannot be coerced to I18nMessage"
                % (value, type(value).__name__)
            ) 
Example #20
Source File: test_streamable.py    From chia-blockchain with Apache License 2.0 5 votes vote down vote up
def test_basic(self):
        @dataclass(frozen=True)
        @streamable
        class TestClass(Streamable):
            a: uint32
            b: uint32
            c: List[uint32]
            d: List[List[uint32]]
            e: Optional[uint32]
            f: Optional[uint32]

        a = TestClass(24, 352, [1, 2, 4], [[1, 2, 3], [3, 4]], 728, None)  # type: ignore

        b: bytes = bytes(a)
        assert a == TestClass.from_bytes(b) 
Example #21
Source File: data.py    From dffml with MIT License 5 votes vote down vote up
def export(obj):
    """
    Convert a value from a Python object into something that is just primitives,
    so things that could be printed withough formatting issues or sent to
    :py:func:`json.dumps`.

    It works on :py:func:`typing.NamedTuple`, :py:func:`dataclasses.dataclass`,
    and anything that implements an ``export`` method that returns a dict.

    Examples
    --------

    >>> from dffml import export
    >>> from typing import NamedTuple
    >>>
    >>> class MyType(NamedTuple):
    ...     data: int
    >>>
    >>> export(MyType(data=42))
    {'data': 42}
    >>>
    >>> class MyBiggerType:
    ...     def export(self):
    ...         return {"feed": "face", "sub": MyType(data=42)}
    >>>
    >>> export(MyBiggerType())
    {'feed': 'face', 'sub': {'data': 42}}
    """
    return export_dict(value=obj)["value"] 
Example #22
Source File: test_dataclass_example.py    From omegaconf with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_merge_example() -> None:
    @dataclass
    class Server:
        port: int = MISSING

    @dataclass
    class Log:
        file: str = MISSING
        rotation: int = MISSING

    @dataclass
    class MyConfig:
        server: Server = Server()
        log: Log = Log()
        users: List[str] = field(default_factory=list)
        numbers: List[int] = field(default_factory=list)

    schema = OmegaConf.structured(MyConfig)
    with pytest.raises(ValidationError):
        OmegaConf.merge(schema, OmegaConf.create({"log": {"rotation": "foo"}}))

    with pytest.raises(ValidationError):
        cfg = schema.copy()
        cfg.numbers.append("fo")

    with pytest.raises(ValidationError):
        OmegaConf.merge(schema, OmegaConf.create({"numbers": ["foo"]})) 
Example #23
Source File: base.py    From dffml with MIT License 5 votes vote down vote up
def config(cls):
    """
    Decorator to create a dataclass
    """
    datacls = dataclasses.dataclass(eq=True, init=True)(cls)
    datacls._fromdict = classmethod(_fromdict)
    datacls._replace = lambda self, *args, **kwargs: dataclasses.replace(
        self, *args, **kwargs
    )
    datacls._asdict = config_asdict
    return datacls 
Example #24
Source File: __init__.py    From dataclasses-jsonschema with MIT License 5 votes vote down vote up
def from_dict(cls: Type[T], data: JsonDict, validate=True, validate_enums: bool = True) -> T:
        """Returns a dataclass instance with all nested classes converted from the dict given"""
        if cls is JsonSchemaMixin:
            raise NotImplementedError

        if cls.__discriminator_name is not None and cls.__discriminator_name in data:
            if data[cls.__discriminator_name] != cls.__name__:
                for subclass in cls.__subclasses__():
                    if subclass.__name__ == data[cls.__discriminator_name]:
                        return subclass.from_dict(data, validate)

        init_values: Dict[str, Any] = {}
        non_init_values: Dict[str, Any] = {}
        if validate:
            cls._validate(data, validate_enums)

        for f in cls._get_fields():
            values = init_values if f.field.init else non_init_values
            if f.mapped_name in data or (
                    f.field.default == MISSING and f.field.default_factory == MISSING):  # type: ignore
                try:
                    values[f.field.name] = cls._decode_field(f.field.name, f.field.type, data.get(f.mapped_name))
                except ValueError:
                    if is_enum(f.field.type):
                        values[f.field.name] = data.get(f.mapped_name)
                    else:
                        raise

        # Need to ignore the type error here, since mypy doesn't know that subclasses are dataclasses
        instance = cls(**init_values)  # type: ignore
        for field_name, value in non_init_values.items():
            setattr(instance, field_name, value)
        return instance 
Example #25
Source File: test_custom_types.py    From tmppy with Apache License 2.0 5 votes vote down vote up
def test_custom_type_default_argument_error():
    @dataclass
    class MyType:
        x: bool = True  # error: Dataclass field defaults are not supported. 
Example #26
Source File: test_custom_types.py    From tmppy with Apache License 2.0 5 votes vote down vote up
def test_custom_type_with_no_fields_error():
    @dataclass
    class MyType:
        pass  # error: Dataclasses can contain only typed field assignments \(and no other statements\). 
Example #27
Source File: test_custom_types.py    From tmppy with Apache License 2.0 5 votes vote down vote up
def test_field_with_no_type_annotation_error():
    @dataclass
    class MyType:
        x = 0  # error: Dataclasses can contain only typed field assignments \(and no other statements\). 
Example #28
Source File: test_custom_types.py    From tmppy with Apache License 2.0 5 votes vote down vote up
def test_custom_class_with_method_error():
    @dataclass
    class MyType:
        def f(self):  # error: Dataclasses can contain only typed field assignments \(and no other statements\).
            return True 
Example #29
Source File: test_custom_types.py    From tmppy with Apache License 2.0 5 votes vote down vote up
def test_custom_class_with_no_dataclass_annotation_error():
    class MyType:  # error: Custom classes must either inherit from Exception or be decorated with @dataclass.
        pass 
Example #30
Source File: value_object.py    From python-clean-architecture with MIT License 5 votes vote down vote up
def __init_subclass__(cls, **kwargs):
        return dataclasses.dataclass(cls, init=True, frozen=True, repr=False)