def __init__(self, component_class: Type[_ComponentType], **kwargs): if utils.is_component_instance(component_class): raise TypeError( "`PartialComponent` must be passed component classes, not component " f"instances. Received: {repr(component_class)}") if not utils.is_component_class(component_class): raise TypeError( "The class passed to `PartialComponent` must be a component class. " f"Received: {component_class}.") if len(kwargs) == 0: raise TypeError( "`PartialComponent` must receive at least one keyword argument." ) lazy_kwargs = {} for name, value in kwargs.items(): if name not in component_class.__component_fields__: raise TypeError( f"Keyword argument '{name}' passed to `PartialComponent` does " "not correspond to any field of component class " f"'{component_class.__name__}'.") if utils.is_immutable(value): lazy_kwargs[name] = utils.wrap_in_callable(value) else: if not inspect.isfunction(value): raise _kwargs_error if len(inspect.signature(value).parameters) == 0: lazy_kwargs[name] = value else: raise _kwargs_error self._component_class = component_class self._lazy_kwargs = lazy_kwargs
def __init__( self, default: Union[ utils.Missing, F, Callable[[], F], Callable[[C], F] ] = utils.missing, *, # `allow_missing` must be a keyword argument. allow_missing: bool = False, ): # Define here once to avoid having to define twice below. default_error = TypeError( "If `default` is passed to `Field`, it must be either:\n" "- An immutable value (int, float, bool, string, or None).\n" "- A function or lambda accepting no arguments or a single\n" " argument (`self`), and returning the default value.\n" f"Received: {default}." ) self.name = None self.allow_missing = allow_missing self.host_component_class = None self.type = None self._registered = False self._return_annotation = inspect.Signature.empty 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 or utils.is_immutable(default): self._default = default return if inspect.isfunction(default): signature = inspect.signature(default) if len(signature.parameters) > 1 or ( len(signature.parameters) == 1 and list(signature.parameters.values())[0].kind in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD) ): raise default_error self._default = default self._return_annotation = signature.return_annotation return raise default_error