Пример #1
0
def test_component_field_allow_missing():
    # This should succeed because we don't set a default value...
    f = ComponentField(allow_missing=True)
    assert f.allow_missing

    # ...but this should fail because there's a default provided
    with pytest.raises(ValueError):
        ComponentField(3.14, allow_missing=True)
Пример #2
0
def test_component_field_component_instance_default():
    with pytest.raises(
            TypeError,
            match=
            "The `default` passed to `ComponentField` must be a component class, not a component instance.",
    ):
        ComponentField(ConcreteComponent())
    class Factory:
        child: Child = ComponentField(Child)

        x: int = Field(5)

        def build(self) -> int:
            return self.child.x
Пример #4
0
class Experiment:
    """
    A wrapper around a Keras experiment. Subclasses must implement their
    training loop in `run`.
    """

    # Nested components
    dataset: Dataset = ComponentField()
    preprocessing: Preprocessing = ComponentField()
    model: keras.models.Model = ComponentField()

    # Parameters
    epochs: int = Field()
    batch_size: int = Field()
    loss: Optional[Union[keras.losses.Loss, str]] = Field()
    optimizer: Union[keras.optimizers.Optimizer, str] = Field()
Пример #5
0
def test_component_field_kwargs():
    with pytest.raises(TypeError,
                       match="Keyword arguments can only be passed"):
        ComponentField(a=1, b=2, c=3)

    class A:
        foo: AbstractClass = ComponentField(ConcreteComponent, a=5)

    assert A.foo.has_default  # type: ignore
    default_value = A.foo.get_default(A())  # type: ignore
    assert isinstance(default_value, ConcreteComponent)
    assert default_value.a == 5
 class Parent:
     child: AbstractChild = ComponentField()
 class GrandParent:
     a: int = Field()
     b: str = Field()
     parent: Parent = ComponentField(Parent)
Пример #8
0
    class ParentTask:
        a: int = Field(2)
        child: Child = ComponentField(Child)

        def run(self):
            print(self.a, self.child.x_Y_z)
Пример #9
0
    class TestTask:
        base: Base = ComponentField()

        def run(self):
            print(self.base.name)
Пример #10
0
 class Parent:
     a: int = Field(10)
     child: AbstractChild = ComponentField()
 class A3:
     base: Tuple[float, float, float] = ComponentField(F3)
Пример #12
0
 class Parent:
     child: Child = ComponentField()
 class Child:
     x: int = ComponentField()
Пример #14
0
 class Child2:
     a: int = Field()
     b: str = Field()
     c: List[float] = Field()
     d: int = Field(allow_missing=True)
     child_1: Child1 = ComponentField()
Пример #15
0
 class Parent:
     b: str = Field("bar")
     child_1: Child1 = ComponentField(Child1)
     child_2: Child2 = ComponentField(Child2)
Пример #16
0
 class Parent:
     a: int = Field()
     b: Tuple[int, float] = Field((5, -42.3))
     child: Child = ComponentField()
Пример #17
0
 class A:
     foo: AbstractClass = ComponentField()
Пример #18
0
 class A:
     foo: AbstractClass = ComponentField(ConcreteComponent, a=5)
 class A1:
     base: Base = ComponentField(F1)
 class Parent:
     b: str = Field("foo")
     child: Child = ComponentField(Child)
 class A2:
     base: Base = ComponentField(F2)
 class Parent:
     child: Base = ComponentField()
     child_allow_missing: Base = ComponentField(allow_missing=True)
Пример #23
0
 class Parent:
     child_1: Child = ComponentField(Child)
     child_2: Child = ComponentField(Child, a=-1)
     a: int = Field(5)
Пример #24
0
 class A:
     foo: AbstractClass = ComponentField(
         PartialComponent(ConcreteComponent, a=5))
 class Parent:
     child: Child = ComponentField(Child)
     x: int = ComponentField(IntFactory)
Пример #26
0
 class Child:
     grand_child: GrandChild = ComponentField()
 class GrandParent:
     c: float = Field(3.14)
     parent: Parent = ComponentField(Parent)
Пример #28
0
 class Parent:
     child: Child = ComponentField(Child)
     grand_child: GrandChild = ComponentField(GrandChild)
 class Parent:
     a: int = Field(5)
     child: Child = ComponentField(Child)
Пример #30
0
 class Parent:
     x: int = Field(7)
     child: Child = ComponentField(Child)