Skip to content

Predicates


between

between(
    inf_bound: Comparison,
    sup_bound: Comparison,
    exclude: bool = False,
) -> Predicate[Comparison]

Returns a between predicate.

Corresponding to inf_bound < x < sup_bound if exclude, otherwise inf_bound <= x <= sup_bound.

contains

contains(*items: T) -> Predicate[Container[T]]

Returns a predicate to verify if value contains all the items.

Examples:

>>> contain = contains("1", "2", "3")
>>> assert contain(["1", "2", "3", "4"])
>>> assert contain("456123")

Parameters:

Name Type Description Default
items T

items to check presence

()

Raises: ValueError: if no item has been passed

equals

equals(expected: T) -> Predicate[T]

Returns a predicate of equality with the provided value.

Examples:

>>> equal_maypy = equals("maypy")
>>> assert equal_maypy("maypy")
>>> assert not equal_maypy("mypy")

Parameters:

Name Type Description Default
expected T

expected equality

required

ge

ge(bound: Comparison) -> Predicate[Comparison]

Returns a predicate corresponding to x >= bound.

gt

gt(bound: Comparison) -> Predicate[Comparison]

Returns a predicate corresponding to x > bound.

is_blank_str

is_blank_str(val: str) -> bool

Checks if the string is either empty or blank.

Examples:

>>> assert is_blank_str("")
>>> assert is_blank_str("   ")
>>> assert not is_blank_str("maypy")

Parameters:

Name Type Description Default
val str

string to verify

required

is_empty

is_empty(val: Sized) -> bool

Checks if the element is empty.

is_falsy

is_falsy(val: T) -> bool

Check if value is falsy.

Examples:

>>> assert is_falsy(0)
>>> assert not is_falsy(12)

is_length

is_length(expected_len: int) -> Predicate[Sized]

Return a predicate that checks if the len of value equals to the expected length provided.

Examples:

>>> is_length_at_5 = is_length(5)
>>> assert is_length_at_5([1,3,3,4,5])
>>> assert not is_length_at_5({5})

is_truthy

is_truthy(val: T) -> bool

Check if value is truthy.

Examples:

>>> assert is_truthy("maypy")
>>> assert not is_truthy("")

le

le(bound: Comparison) -> Predicate[Comparison]

Returns a predicate corresponding to x <= bound.

lt

lt(bound: Comparison) -> Predicate[Comparison]

Returns a predicate corresponding to x < bound.

match_regex

match_regex(regex: Pattern[str]) -> Predicate[str]
match_regex(
    regex: str, flags: Union[RegexFlag, int] = 0
) -> Predicate[str]
match_regex(
    regex: Union[Pattern[str], str],
    flags: Union[RegexFlag, int] = 0,
) -> Predicate[str]

Returns a predicate that checks if value match the regex pattern provided.

Parameters:

Name Type Description Default
regex Union[Pattern[str], str]

regex to match (either a string or a Pattern)

required
flags Union[RegexFlag, int]

regex flags; should bot be passed with a pattern.

0

Raises:

Type Description
TypeError

when passing flags whereas a Pattern have been passed

neg

neg(predicate: Predicate[T]) -> Predicate[T]

Create a new predicate that is the negation of the provided.

If the predicate would yield True, the negated one would yield False, and vice versa.

Examples:

>>> assert maybe("maypy").filter(is_blank_str).is_empty()
>>> assert maybe("maypy").filter(neg(is_blank_str)).is_present()

Parameters:

Name Type Description Default
predicate Predicate[T]

preddicate to negate

required

Returns:

Type Description
Predicate[T]

Negate predicate of the provided

one_of

one_of(options: Container[T]) -> Predicate[T]

Returns a predicate to check if value is one of these options.

Examples:

>>> option = one_of(["foo", "bar"])
>>> assert option("foo")
>>> assert option("bar")
>>> assert not option("maypy")