Skip to content

Context-sensitive default objects #142

@binaryDiv

Description

@binaryDiv

It would be nice to have a special kind of default object class that can access context arguments, OR implement a way so that custom default classes can access context arguments.

That way you could have default values that depend on the validation context (including the context of the application, e.g. field defaults that are defined in config files).

(Pseudo) code example:

def get_app_config() -> dict:
    ...

class ConfigDefault(BaseDefault):
    _config_key: str

    def __init__(self, config_key: str):
        self._config_key = config_key

    @override
    def get_value(self) -> T:
        get_app_config()[self._config_key]

    @override
    def needs_factory(self) -> bool:
        return True

@validataclass
class ExampleClass:
    some_field: int = IntegerValidator(), ConfigDefault('some_field_default')

An open question would be how to get the typing right... Maybe just specify the type as an argument when constructing the default? It wouldn't really be used by the code though, just by the type checker. (It's annoying that Python cannot do something like Default[int](...) to specify the type argument when constructing an object.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    new featureNew feature that is not (only) a validator classquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions