Python dataclasses.field() Examples

The following are 30 code examples of dataclasses.field(). 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: test_config.py    From grpclib with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def test_validate():
    @dataclass
    class Config:
        foo: Optional[float] = field(
            default=None,
            metadata={
                'validate': _optional(_chain(_of_type(int, float), _positive)),
            },
        )

        def __post_init__(self):
            _validate(self)

    assert Config().foo is None
    assert Config(foo=0.123).foo == 0.123
    assert Config(foo=42).foo == 42

    with pytest.raises(ValueError, match='"foo" should be positive'):
        assert Config(foo=0)

    with pytest.raises(TypeError, match='"foo" should be of type'):
        assert Config(foo='a') 
Example #2
Source File: common_types.py    From gapic-generator-python with Apache License 2.0 6 votes vote down vote up
def message_factory(exp: str,
                    repeated_iter=itertools.repeat(False),
                    enum: Optional[wrappers.EnumType] = None) -> DummyMessage:
    # This mimics the structure of MessageType in the wrappers module:
    # A MessageType has a map from field names to Fields,
    # and a Field has an (optional) MessageType.
    # The 'exp' parameter is a dotted attribute expression
    # used to describe the field and type hierarchy,
    # e.g. "mollusc.cephalopod.coleoid"
    toks = exp.split(".")
    messages = [DummyMessage({}, tok.upper() + "_TYPE") for tok in toks]
    if enum:
        messages[-1] = enum

    for base, field, attr_name, repeated_field in zip(
        messages, messages[1:], toks[1:], repeated_iter
    ):
        base.fields[attr_name] = (DummyField(message=field, repeated=repeated_field)
                                  if isinstance(field, DummyMessage)
                                  else DummyField(enum=field))

    return messages[0] 
Example #3
Source File: wrappers.py    From gapic-generator-python with Apache License 2.0 6 votes vote down vote up
def recursive_field_types(self) -> Sequence[
        Union['MessageType', 'EnumType']
    ]:
        """Return all composite fields used in this proto's messages."""
        types: Set[Union['MessageType', 'EnumType']] = set()

        stack = [iter(self.fields.values())]
        while stack:
            fields_iter = stack.pop()
            for field in fields_iter:
                if field.message and field.type not in types:
                    stack.append(iter(field.message.fields.values()))
                if not field.is_primitive:
                    types.add(field.type)

        return tuple(types) 
Example #4
Source File: flows.py    From spins-b with GNU General Public License v3.0 6 votes vote down vote up
def constant_field(**kwargs):
    """Marks a flow field as constant.

    Constant flow fields are not permitted to change value once set, and
    consequently, the gradient for these fields do not exist.

    Args:
        kwargs: Keyword arguments to pass to `dataclasses.field`.

    Returns:
        A dataclasses field where `metadata` has entry `"constant_field": True`.
    """
    if "metadata" not in kwargs:
        kwargs["metadata"] = {}
    kwargs["metadata"].update({"constant_field": True})
    return dataclasses.field(**kwargs) 
Example #5
Source File: test_base.py    From dacite with MIT License 6 votes vote down vote up
def test_from_dict_with_wrong_type():
    @dataclass
    class X:
        s: str
        i: int

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"s": "test", "i": "wrong"})

    assert (
        str(exception_info.value)
        == 'wrong value type for field "i" - should be "int" instead of value "wrong" of type "str"'
    )
    assert exception_info.value.field_path == "i"
    assert exception_info.value.field_type == int
    assert exception_info.value.value == "wrong" 
Example #6
Source File: test_dataclass_example.py    From omegaconf with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_modifiers() -> None:
    conf: Modifiers = OmegaConf.structured(Modifiers)
    # regular fields cannot take None
    with pytest.raises(ValidationError):
        conf.num = None  # type: ignore

    # but Optional fields can
    conf.optional_num = None
    assert conf.optional_num is None

    # Accessing a missing field will trigger MissingMandatoryValue exception
    with pytest.raises(MissingMandatoryValue):
        # noinspection PyStatementEffect
        conf.another_num

    # but you can access it once it's been assigned
    conf.another_num = 42
    assert conf.another_num == 42 
Example #7
Source File: base.py    From dffml with MIT License 6 votes vote down vote up
def config(cls, config, *above):
        """
        Create the BaseConfig required to instantiate this class by parsing the
        config dict.
        """
        if getattr(cls, "CONFIG", None) is None:
            raise AttributeError(
                f"{cls.__qualname__} requires CONFIG property or implementation of config() classmethod"
            )
        # Build the arguments to the CONFIG class
        kwargs: Dict[str, Any] = {}
        for field in dataclasses.fields(cls.CONFIG):
            kwargs[field.name] = got = cls.config_get(
                config, above, field.name
            )
            if inspect.isclass(got) and issubclass(got, BaseConfigurable):
                try:
                    kwargs[field.name] = got.withconfig(
                        config, *above, *cls.add_label()
                    )
                except MissingConfig:
                    kwargs[field.name] = got.withconfig(
                        config, *above, *cls.add_label()[:-1]
                    )
        return cls.CONFIG(**kwargs) 
Example #8
Source File: wrappers.py    From gapic-generator-python with Apache License 2.0 6 votes vote down vote up
def field_headers(self) -> Sequence[str]:
        """Return the field headers defined for this method."""
        http = self.options.Extensions[annotations_pb2.http]

        pattern = re.compile(r'\{([a-z][\w\d_.]+)=')

        potential_verbs = [
            http.get,
            http.put,
            http.post,
            http.delete,
            http.patch,
            http.custom.path,
        ]

        return next((tuple(pattern.findall(verb)) for verb in potential_verbs if verb), ()) 
Example #9
Source File: wrappers.py    From gapic-generator-python with Apache License 2.0 6 votes vote down vote up
def with_context(self, *, collisions: FrozenSet[str]) -> 'Field':
        """Return a derivative of this field with the provided context.

        This method is used to address naming collisions. The returned
        ``Field`` object aliases module names to avoid naming collisions
        in the file being written.
        """
        return dataclasses.replace(
            self,
            message=self.message.with_context(
                collisions=collisions,
                skip_fields=True,
            ) if self.message else None,
            enum=self.enum.with_context(collisions=collisions)
            if self.enum else None,
            meta=self.meta.with_context(collisions=collisions),
        ) 
Example #10
Source File: base.py    From dffml with MIT License 6 votes vote down vote up
def field(
    description: str,
    *args,
    action=None,
    required: bool = False,
    labeled: bool = False,
    metadata: Optional[dict] = None,
    **kwargs,
):
    """
    Creates an instance of :py:func:`dataclasses.field`. The first argument,
    ``description`` is the description of the field, and will be set as the
    ``"description"`` key in the metadata ``dict``.
    """
    if not metadata:
        metadata = {}
    metadata["description"] = description
    metadata["required"] = required
    metadata["labeled"] = labeled
    metadata["action"] = action
    return dataclasses.field(*args, metadata=metadata, **kwargs) 
Example #11
Source File: wrappers.py    From gapic-generator-python with Apache License 2.0 6 votes vote down vote up
def paged_result_field(self) -> Optional[Field]:
        """Return the response pagination field if the method is paginated."""
        # If the request field lacks any of the expected pagination fields,
        # then the method is not paginated.
        for page_field in ((self.input, int, 'page_size'),
                           (self.input, str, 'page_token'),
                           (self.output, str, 'next_page_token')):
            field = page_field[0].fields.get(page_field[2], None)
            if not field or field.type != page_field[1]:
                return None

        # Return the first repeated field.
        for field in self.output.fields.values():
            if field.repeated and field.message:
                return field

        # We found no repeated fields. Return None.
        return None 
Example #12
Source File: test_config.py    From grpclib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_change_default():
    # all params should be optional
    @dataclass
    class Config:
        foo: Optional[float] = field(
            default=cast(None, _DEFAULT),
            metadata={
                'validate': _optional(_chain(_of_type(int, float), _positive)),
                'test-default': 1234,
            },
        )

        def __post_init__(self):
            _validate(self)

    assert _with_defaults(Config(foo=1), 'test-default').foo == 1 
Example #13
Source File: objecttype.py    From graphene with MIT License 6 votes vote down vote up
def __new__(cls, name_, bases, namespace, **options):
        # Note: it's safe to pass options as keyword arguments as they are still type-checked by ObjectTypeOptions.

        # We create this type, to then overload it with the dataclass attrs
        class InterObjectType:
            pass

        base_cls = super().__new__(
            cls, name_, (InterObjectType,) + bases, namespace, **options,
        )
        if base_cls._meta:
            fields = [
                (
                    key,
                    "typing.Any",
                    field(
                        default=field_value.default_value
                        if isinstance(field_value, Field)
                        else None
                    ),
                )
                for key, field_value in base_cls._meta.fields.items()
            ]
            dataclass = make_dataclass(name_, fields, bases=())
            InterObjectType.__init__ = dataclass.__init__
            InterObjectType.__eq__ = dataclass.__eq__
            InterObjectType.__repr__ = dataclass.__repr__
        return base_cls 
Example #14
Source File: test_dataclasses.py    From dacite with MIT License 5 votes vote down vote up
def test_get_default_value_for_field_with_default_factory():
    @dataclass
    class X:
        i: int = field(default_factory=lambda: 1)

    value = get_default_value_for_field(field=fields(X)[0])

    assert value == 1 
Example #15
Source File: test_dataclasses.py    From dacite with MIT License 5 votes vote down vote up
def test_create_instance_with_post_init_values():
    @dataclass
    class X:
        i: int
        j: int = field(init=False)

    instance = create_instance(data_class=X, init_values={"i": 1}, post_init_values={"j": 2})

    assert instance.i == 1
    assert instance.j == 2 
Example #16
Source File: test_dataclasses.py    From dacite with MIT License 5 votes vote down vote up
def test_get_default_value_for_field_with_default_value():
    @dataclass
    class X:
        i: int = 1

    value = get_default_value_for_field(field=fields(X)[0])

    assert value == 1 
Example #17
Source File: test_base.py    From dacite with MIT License 5 votes vote down vote up
def test_from_dict_with_post_init():
    @dataclass
    class X:
        s: str = field(init=False)

    x = X()
    x.s = "test"

    result = from_dict(X, {"s": "test"})

    assert result == x 
Example #18
Source File: test_dataclasses.py    From dacite with MIT License 5 votes vote down vote up
def test_get_default_value_for_optional_field():
    @dataclass
    class X:
        i: Optional[int]

    value = get_default_value_for_field(field=fields(X)[0])

    assert value is None 
Example #19
Source File: test_dataclasses.py    From dacite with MIT License 5 votes vote down vote up
def test_get_default_value_for_field_without_default_value():
    @dataclass
    class X:
        i: int

    with pytest.raises(DefaultValueNotFoundError):
        get_default_value_for_field(field=fields(X)[0]) 
Example #20
Source File: test_base.py    From dacite with MIT License 5 votes vote down vote up
def test_from_dict_with_nested_data_classes_and_default_factory():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        x: X = field(default_factory=lambda: X(i=42))

    result = from_dict(Y, {})

    assert result == Y(x=X(i=42)) 
Example #21
Source File: experiment.py    From axcell with Apache License 2.0 5 votes vote down vote up
def load(cls, path):
        # a new field added to the class should not change
        # the default behaviour of experiment, so that we
        # can load older experiments by setting missing fields
        # to their default values
        e = cls()
        path = Path(path)
        with open(path, "rt") as f:
            j = json.load(f)
        j["name"] = path.name
        e = e.new_experiment(**j)
        e._path = path
        return e 
Example #22
Source File: wrappers.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def field_types(self) -> Sequence[Union['MessageType', 'EnumType']]:
        answer = tuple(
            field.type
            for field in self.fields.values()
            if field.message or field.enum
        )

        return answer 
Example #23
Source File: wrappers.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def type(self) -> Union['MessageType', 'EnumType', 'PrimitiveType']:
        """Return the type of this field."""
        # If this is a message or enum, return the appropriate thing.
        if self.type_name and self.message:
            return self.message
        if self.type_name and self.enum:
            return self.enum

        # This is a primitive. Return the corresponding Python type.
        # The enum values used here are defined in:
        #   Repository: https://github.com/google/protobuf/
        #   Path: src/google/protobuf/descriptor.proto
        #
        # The values are used here because the code would be excessively
        # verbose otherwise, and this is guaranteed never to change.
        #
        # 10, 11, and 14 are intentionally missing. They correspond to
        # group (unused), message (covered above), and enum (covered above).
        if self.field_pb.type in (1, 2):
            return PrimitiveType.build(float)
        if self.field_pb.type in (3, 4, 5, 6, 7, 13, 15, 16, 17, 18):
            return PrimitiveType.build(int)
        if self.field_pb.type == 8:
            return PrimitiveType.build(bool)
        if self.field_pb.type == 9:
            return PrimitiveType.build(str)
        if self.field_pb.type == 12:
            return PrimitiveType.build(bytes)

        # This should never happen.
        raise TypeError(f'Unrecognized protobuf type: {self.field_pb.type}. '
                        'This code should not be reachable; please file a bug.') 
Example #24
Source File: wrappers.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def required(self) -> bool:
        """Return True if this is a required field, False otherwise.

        Returns:
            bool: Whether this field is required.
        """
        return (field_behavior_pb2.FieldBehavior.Value('REQUIRED') in
                self.options.Extensions[field_behavior_pb2.field_behavior]) 
Example #25
Source File: wrappers.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def map(self) -> bool:
        """Return True if this field is a map, False otherwise."""
        return bool(self.repeated and self.message and self.message.map) 
Example #26
Source File: wrappers.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def is_primitive(self) -> bool:
        """Return True if the field is a primitive, False otherwise."""
        return isinstance(self.type, PrimitiveType) 
Example #27
Source File: samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def flattenable_fields(self) -> FrozenSet[str]:
        return frozenset(
            field.name for field in self.method.flattened_fields.values()
        ) 
Example #28
Source File: samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def __init__(self, method: wrappers.Method, api_schema=None):
        # The response ($resp) variable is special and guaranteed to exist.
        self.method = method
        self.request_type_ = method.input
        response_type = method.output
        if method.paged_result_field:
            response_type = method.paged_result_field
        elif method.lro:
            response_type = method.lro.response_type

        self.api_schema_ = api_schema

        # This is a shameless hack to work around the design of wrappers.Field
        MockField = namedtuple("MockField", ["message", "repeated"])

        # TODO: pass var_defs_ around during response verification
        #       instead of assigning/restoring.
        self.var_defs_: ChainMap[str, wrappers.Field] = chainmap(
            # When validating expressions we need to store the Field,
            # not just the message type, because there are additional data we need:
            # whether a name refers to a repeated value (or a map),
            # and whether it's an enum or a message or a primitive type.
            # The method call response isn't a field, so construct an artificial
            # field that wraps the response.
            {
                "$resp": MockField(response_type, False)  # type: ignore
            }
        ) 
Example #29
Source File: flows.py    From spins-b with GNU General Public License v3.0 5 votes vote down vote up
def np_zero_field(n: int):
    """Creates a field that defaults to a numpy array with zeros.

    Args:
        n: Number of elements in array.

    Returns:
        Dataclass field that produces an array with `n` zeros.
    """
    return dataclasses.field(default_factory=lambda: np.zeros(n)) 
Example #30
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"]}))