Optional¶
This page documents the Optional checker.
- class argscheck.optional.Optional(*args, default_value=<MISSING>, default_factory=<MISSING>, sentinel=None, **kwargs)¶
Check if x is None or something else, similarly to typing.Optional.
- Parameters
args – Tuple[CheckerLike] – Specifies what x may be (other than None).
default_value – Optional[Any] – If x is None, it will be replaced by default_value.
default_factory – Optional[Callable] – if x is None, it will be replaced by a value freshly returned from default_factory(). This is useful for setting default values that are of mutable types.
sentinel – Optional[Any] – x is sentinel will be used to determine if the x is missing, instead of x is None.
- Example
from argscheck import check, Optional # Check if a list, set or None, replace None with a fresh list checker = Optional(list, set, default_factory=list) check(checker, [1, 2, 3]) # Passes, [1, 2, 3] is returned check(checker, {1, 2, 3}) # Passes, {1, 2, 3} is returned check(checker, None) # Passes, [] is returned check(checker, "string") # Fails, a TypeError is raised