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)
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
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()
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)
class ParentTask: a: int = Field(2) child: Child = ComponentField(Child) def run(self): print(self.a, self.child.x_Y_z)
class TestTask: base: Base = ComponentField() def run(self): print(self.base.name)
class Parent: a: int = Field(10) child: AbstractChild = ComponentField()
class A3: base: Tuple[float, float, float] = ComponentField(F3)
class Parent: child: Child = ComponentField()
class Child: x: int = ComponentField()
class Child2: a: int = Field() b: str = Field() c: List[float] = Field() d: int = Field(allow_missing=True) child_1: Child1 = ComponentField()
class Parent: b: str = Field("bar") child_1: Child1 = ComponentField(Child1) child_2: Child2 = ComponentField(Child2)
class Parent: a: int = Field() b: Tuple[int, float] = Field((5, -42.3)) child: Child = ComponentField()
class A: foo: AbstractClass = ComponentField()
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)
class Parent: child_1: Child = ComponentField(Child) child_2: Child = ComponentField(Child, a=-1) a: int = Field(5)
class A: foo: AbstractClass = ComponentField( PartialComponent(ConcreteComponent, a=5))
class Parent: child: Child = ComponentField(Child) x: int = ComponentField(IntFactory)
class Child: grand_child: GrandChild = ComponentField()
class GrandParent: c: float = Field(3.14) parent: Parent = ComponentField(Parent)
class Parent: child: Child = ComponentField(Child) grand_child: GrandChild = ComponentField(GrandChild)
class Parent: a: int = Field(5) child: Child = ComponentField(Child)
class Parent: x: int = Field(7) child: Child = ComponentField(Child)