"""This modules contains classes for creating conditional constraints."""fromtypingimportOptional,Sequence,UnionfromclickimportContext,Parameterfrom._coreimportConstraintfrom.conditionsimportAllSet,IsSet,Predicatefrom.exceptionsimportConstraintViolatedfrom.._utilimportmake_reprdefas_predicate(arg:Union[str,Sequence[str],Predicate])->Predicate:ifisinstance(arg,str):returnIsSet(arg)elifisinstance(arg,Predicate):returnargelifisinstance(arg,Sequence):returnAllSet(*arg)else:raiseTypeError("`arg` should be a string, a list of strings or a `Predicate`")
[docs]classIf(Constraint):""" Checks one constraint or another depending on the truth value of the condition. .. versionadded:: 0.8.0 you can now pass a sequence of parameter names as condition, which corresponds to the predicate ``AllSet(*param_names)``. :param condition: can be either an instance of ``Predicate`` or (more often) the name of a parameter or a list/tuple of parameters that must be all set for the condition to be true. :param then: a constraint checked if the condition is true. :param else_: an (optional) constraint checked if the condition is false. """def__init__(self,condition:Union[str,Sequence[str],Predicate],then:Constraint,else_:Optional[Constraint]=None,):self._condition=as_predicate(condition)self._then=thenself._else=else_
[docs]defhelp(self,ctx:Context)->str:condition=self._condition.description(ctx)then_help=self._then.help(ctx)else_help=self._else.help(ctx)ifself._elseelseNoneifnotself._else:returnf"{then_help} if {condition}"else:returnf"{then_help} if {condition}, otherwise {else_help}"