Example #1
0
def test_init_error_on_non_component():
    with pytest.raises(
            TypeError,
            match=
            "The class passed to `PartialComponent` must be a component class.",
    ):
        PartialComponent(2.71, a=3)

    with pytest.raises(
            TypeError,
            match=
            "The class passed to `PartialComponent` must be a component class.",
    ):
        PartialComponent(lambda x: x * 2, a=3)

    class Test:
        a: int

    with pytest.raises(
            TypeError,
            match=
            "The class passed to `PartialComponent` must be a component class.",
    ):
        PartialComponent(Test, a=3)

    @component
    class Test2:
        a: int = Field(5)

    with pytest.raises(
            TypeError,
            match=
            "`PartialComponent` must be passed component classes, not component instances.",
    ):
        PartialComponent(Test2(), a=3)
Example #2
0
def test_kwargs_accept_nested_partial_component(ExampleComponentClasses):
    Parent, Child1, _ = ExampleComponentClasses

    # This should succeed without error.
    partial = PartialComponent(Parent, child=PartialComponent(Child1, a=5))

    # Generate a component instance from the partial, and configure it.
    p = partial()
    configure(p, {})

    assert isinstance(p.child, Child1)
    assert p.child.a == 5
def test_init_error_no_kwargs(ExampleComponentClasses):
    _, _, Child2 = ExampleComponentClasses

    with pytest.raises(
        TypeError,
        match="`PartialComponent` must receive at least one keyword argument.",
    ):
        PartialComponent(Child2)
Example #4
0
def test_kwargs_accept_component_class(ExampleComponentClasses):
    Parent, Child1, _ = ExampleComponentClasses

    # This should succeed without error.
    partial = PartialComponent(Parent, child=Child1)

    # Generate a component instance from the partial, and configure it.
    p = partial()
    configure(p, {})

    assert isinstance(p.child, Child1)
    assert p.child.b == "foo"
    assert p.child.a == 10  # This tests that field value inheritence still works.
Example #5
0
    def __init__(
        self,
        default: Union[utils.Missing, F, PartialComponent[F]] = utils.missing,
        *,  # `allow_missing` must be a keyword argument.
        allow_missing: bool = False,
        **kwargs,
    ):
        if allow_missing and default is not utils.missing:
            raise ValueError(
                "If a `Field` has `allow_missing=True`, no default can be provided."
            )

        if default is utils.missing:
            if len(kwargs) > 0:
                raise TypeError(
                    "Keyword arguments can only be passed to `ComponentField` if "
                    "a default component class is also passed."
                )
        elif isinstance(default, PartialComponent) or utils.is_component_class(default):
            if len(kwargs) > 0:
                default = PartialComponent(default, **kwargs)
        elif utils.is_component_instance(default):
            raise TypeError(
                "The `default` passed to `ComponentField` must be a component class, "
                f"not a component instance. Received: {repr(default)}."
            )
        else:
            raise TypeError(
                "The `default` passed to `ComponentField` must be either a component "
                "class or a `PartialComponent`."
            )

        self.name = utils.missing
        self.allow_missing = allow_missing
        self.host_component_class = utils.missing
        self.type = utils.missing
        self._registered = False
        self._default = default
Example #6
0
 class A:
     foo: AbstractClass = ComponentField(
         PartialComponent(ConcreteComponent, a=5))