Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_validator_bool_function():
def validate(t):
return len(t) == 3
validator = build_validator(validate)
assert validator.validate(Document("foo")) is None # should not raise
def test_validator_instance():
def validate(t):
return len(t) == 3
validator = Validator.from_callable(validate)
validator = build_validator(validator)
assert validator.validate(Document("foo")) is None # should not raise
def test_validator_bool_function_fails():
def validate(t):
return len(t) == 3
validator = build_validator(validate)
with pytest.raises(ValidationError) as e:
validator.validate(Document("fooooo"))
assert e.value.message == "invalid input"
if not choices:
raise ValueError("No choices is given, you should use Text question.")
merged_style = merge_styles([DEFAULT_STYLE, style])
def get_prompt_tokens() -> List[Tuple[Text, Text]]:
return [("class:qmark", qmark), ("class:question", " {} ".format(message))]
def get_meta_style(meta: Dict[Text, Any]):
if meta:
for key in meta:
meta[key] = HTML("").format(meta[key])
return meta
validator = build_validator(validate)
if completer is None:
# use the default completer
completer = WordCompleter(
choices,
ignore_case=ignore_case,
meta_information=get_meta_style(meta_information),
match_middle=match_middle,
)
p = PromptSession(
get_prompt_tokens,
style=merged_style,
completer=completer,
validator=validator,
complete_style=complete_style,
returning a boolean, or an class reference to a
subclass of the prompt toolkit Validator class.
qmark: Question prefix displayed in front of the question.
By default this is a `?`
style: A custom color and style for the question parts. You can
configure colors as well as font types for different elements.
Returns:
Question: Question instance, ready to be prompted (using `.ask()`).
"""
merged_style = merge_styles([DEFAULT_STYLE, style])
validator = build_validator(validate)
def get_prompt_tokens() -> List[Tuple[Text, Text]]:
return [("class:qmark", qmark), ("class:question", " {} ".format(message))]
p = PromptSession(
get_prompt_tokens, style=merged_style, validator=validator, **kwargs
)
p.default_buffer.reset(Document(default))
return Question(p.app)