Skip to content

Core

The generic fluent vocabulary and the expect dispatcher.

pyssertive.AssertableValue

AssertableValue(value: Any)

Fluent assertions over an arbitrary Python value.

Created directly or via the :func:expect factory. Methods return Self to allow chaining. Use :meth:each to apply matchers to every element of an iterable, and :meth:sequence to match elements positionally.

Source code in src/pyssertive/assertions.py
def __init__(self, value: Any) -> None:
    self._value = value

equals

equals(expected: Any) -> Self
Source code in src/pyssertive/assertions.py
def equals(self, expected: Any) -> Self:
    assert self._value == expected, f"Expected value to equal {expected!r}, got {self._value!r}"
    return self

does_not_equal

does_not_equal(unexpected: Any) -> Self
Source code in src/pyssertive/assertions.py
def does_not_equal(self, unexpected: Any) -> Self:
    assert self._value != unexpected, f"Expected value to not equal {unexpected!r}"
    return self

is_same_as

is_same_as(expected: Any) -> Self
Source code in src/pyssertive/assertions.py
def is_same_as(self, expected: Any) -> Self:
    assert self._value is expected, f"Expected value to be the same instance as {expected!r}"
    return self

is_not_same_as

is_not_same_as(unexpected: Any) -> Self
Source code in src/pyssertive/assertions.py
def is_not_same_as(self, unexpected: Any) -> Self:
    assert self._value is not unexpected, f"Expected value to not be the same instance as {unexpected!r}"
    return self

is_none

is_none() -> Self
Source code in src/pyssertive/assertions.py
def is_none(self) -> Self:
    assert self._value is None, f"Expected value to be None, got {self._value!r}"
    return self

is_not_none

is_not_none() -> Self
Source code in src/pyssertive/assertions.py
def is_not_none(self) -> Self:
    assert self._value is not None, "Expected value to not be None"
    return self

is_truthy

is_truthy() -> Self
Source code in src/pyssertive/assertions.py
def is_truthy(self) -> Self:
    assert self._value, f"Expected value to be truthy, got {self._value!r}"
    return self

is_falsy

is_falsy() -> Self
Source code in src/pyssertive/assertions.py
def is_falsy(self) -> Self:
    assert not self._value, f"Expected value to be falsy, got {self._value!r}"
    return self

is_true

is_true() -> Self
Source code in src/pyssertive/assertions.py
def is_true(self) -> Self:
    assert self._value is True, f"Expected value to be True, got {self._value!r}"
    return self

is_false

is_false() -> Self
Source code in src/pyssertive/assertions.py
def is_false(self) -> Self:
    assert self._value is False, f"Expected value to be False, got {self._value!r}"
    return self

is_instance_of

is_instance_of(cls: type | tuple[type, ...]) -> Self
Source code in src/pyssertive/assertions.py
def is_instance_of(self, cls: type | tuple[type, ...]) -> Self:
    type_names = " | ".join(t.__name__ for t in cls) if isinstance(cls, tuple) else cls.__name__
    assert isinstance(self._value, cls), (
        f"Expected value to be instance of {type_names}, got {type(self._value).__name__}"
    )
    return self

is_not_instance_of

is_not_instance_of(cls: type | tuple[type, ...]) -> Self
Source code in src/pyssertive/assertions.py
def is_not_instance_of(self, cls: type | tuple[type, ...]) -> Self:
    type_names = " | ".join(t.__name__ for t in cls) if isinstance(cls, tuple) else cls.__name__
    assert not isinstance(self._value, cls), f"Expected value to not be instance of {type_names}"
    return self

is_type

is_type(cls: type) -> Self
Source code in src/pyssertive/assertions.py
def is_type(self, cls: type) -> Self:
    assert type(self._value) is cls, (
        f"Expected value to be of exact type {cls.__name__}, got {type(self._value).__name__}"
    )
    return self

is_greater_than

is_greater_than(other: Any) -> Self
Source code in src/pyssertive/assertions.py
def is_greater_than(self, other: Any) -> Self:
    assert self._value > other, f"Expected value to be greater than {other!r}, got {self._value!r}"
    return self

is_less_than

is_less_than(other: Any) -> Self
Source code in src/pyssertive/assertions.py
def is_less_than(self, other: Any) -> Self:
    assert self._value < other, f"Expected value to be less than {other!r}, got {self._value!r}"
    return self

is_at_least

is_at_least(other: Any) -> Self
Source code in src/pyssertive/assertions.py
def is_at_least(self, other: Any) -> Self:
    assert self._value >= other, f"Expected value to be at least {other!r}, got {self._value!r}"
    return self

is_at_most

is_at_most(other: Any) -> Self
Source code in src/pyssertive/assertions.py
def is_at_most(self, other: Any) -> Self:
    assert self._value <= other, f"Expected value to be at most {other!r}, got {self._value!r}"
    return self

is_between

is_between(low: Any, high: Any) -> Self
Source code in src/pyssertive/assertions.py
def is_between(self, low: Any, high: Any) -> Self:
    assert low <= self._value <= high, (
        f"Expected value to be between {low!r} and {high!r} (inclusive), got {self._value!r}"
    )
    return self

has_count

has_count(expected: int) -> Self
Source code in src/pyssertive/assertions.py
def has_count(self, expected: int) -> Self:
    actual = len(self._value)
    assert actual == expected, f"Expected count of {expected}, got {actual}"
    return self

is_empty

is_empty() -> Self
Source code in src/pyssertive/assertions.py
def is_empty(self) -> Self:
    assert len(self._value) == 0, f"Expected value to be empty, got {self._value!r}"
    return self

is_not_empty

is_not_empty() -> Self
Source code in src/pyssertive/assertions.py
def is_not_empty(self) -> Self:
    assert len(self._value) > 0, "Expected value to not be empty"
    return self

contains

contains(*items: Any) -> Self
Source code in src/pyssertive/assertions.py
def contains(self, *items: Any) -> Self:
    for item in items:
        assert item in self._value, f"Expected value to contain {item!r}"
    return self

does_not_contain

does_not_contain(*items: Any) -> Self
Source code in src/pyssertive/assertions.py
def does_not_contain(self, *items: Any) -> Self:
    for item in items:
        assert item not in self._value, f"Expected value to not contain {item!r}"
    return self

has_key

has_key(key: str) -> Self
Source code in src/pyssertive/assertions.py
def has_key(self, key: str) -> Self:
    assert isinstance(self._value, dict), f"Expected a dict, got {type(self._value).__name__}"
    assert key in self._value, f"Expected dict to have key {key!r}"
    return self

does_not_have_key

does_not_have_key(key: str) -> Self
Source code in src/pyssertive/assertions.py
def does_not_have_key(self, key: str) -> Self:
    assert isinstance(self._value, dict), f"Expected a dict, got {type(self._value).__name__}"
    assert key not in self._value, f"Expected dict to not have key {key!r}"
    return self

has_keys

has_keys(*keys: str) -> Self
Source code in src/pyssertive/assertions.py
def has_keys(self, *keys: str) -> Self:
    assert isinstance(self._value, dict), f"Expected a dict, got {type(self._value).__name__}"
    for key in keys:
        assert key in self._value, f"Expected dict to have key {key!r}"
    return self

has_attribute

has_attribute(name: str, value: Any = _MISSING) -> Self
Source code in src/pyssertive/assertions.py
def has_attribute(self, name: str, value: Any = _MISSING) -> Self:
    assert hasattr(self._value, name), f"Expected object to have attribute {name!r}"
    if value is not _MISSING:
        actual = getattr(self._value, name)
        assert actual == value, f"Expected attribute {name!r} to equal {value!r}, got {actual!r}"
    return self

does_not_have_attribute

does_not_have_attribute(name: str) -> Self
Source code in src/pyssertive/assertions.py
def does_not_have_attribute(self, name: str) -> Self:
    assert not hasattr(self._value, name), f"Expected object to not have attribute {name!r}"
    return self

matches

matches(pattern: str) -> Self
Source code in src/pyssertive/assertions.py
def matches(self, pattern: str) -> Self:
    assert re.search(pattern, self._value) is not None, f"Expected {self._value!r} to match pattern '{pattern}'"
    return self

does_not_match

does_not_match(pattern: str) -> Self
Source code in src/pyssertive/assertions.py
def does_not_match(self, pattern: str) -> Self:
    assert re.search(pattern, self._value) is None, f"Expected {self._value!r} to not match pattern '{pattern}'"
    return self

starts_with

starts_with(prefix: str) -> Self
Source code in src/pyssertive/assertions.py
def starts_with(self, prefix: str) -> Self:
    assert self._value.startswith(prefix), f"Expected {self._value!r} to start with {prefix!r}"
    return self

ends_with

ends_with(suffix: str) -> Self
Source code in src/pyssertive/assertions.py
def ends_with(self, suffix: str) -> Self:
    assert self._value.endswith(suffix), f"Expected {self._value!r} to end with {suffix!r}"
    return self

each

each() -> _Each
Source code in src/pyssertive/assertions.py
def each(self) -> _Each:
    assert hasattr(self._value, "__iter__"), f"Expected an iterable, got {type(self._value).__name__}"
    return _Each(self._value)

sequence

sequence(*expected: Any) -> Self
Source code in src/pyssertive/assertions.py
def sequence(self, *expected: Any) -> Self:
    assert hasattr(self._value, "__iter__"), f"Expected an iterable, got {type(self._value).__name__}"
    items = list(self._value)
    assert len(items) == len(expected), f"Expected sequence of length {len(expected)}, got {len(items)}"
    for item, exp in zip(items, expected, strict=True):
        sub = AssertableValue(item)
        if callable(exp):
            exp(sub)
        else:
            sub.equals(exp)
    return self

The expect callable is an instance of the dispatcher class below — expect(...), expect.json(...), expect.html(...), expect.mcp(...), and expect.arch(...):

pyssertive.assertions._Expect

__call__

__call__(value: Any) -> AssertableValue
Source code in src/pyssertive/assertions.py
def __call__(self, value: Any) -> AssertableValue:
    return AssertableValue(value)

json

json(data: Any) -> AssertableJson
Source code in src/pyssertive/assertions.py
def json(self, data: Any) -> AssertableJson:
    from pyssertive.formats.json import AssertableJson

    return AssertableJson(data)

html

html(markup: Any) -> AssertableHtml
Source code in src/pyssertive/assertions.py
def html(self, markup: Any) -> AssertableHtml:
    from pyssertive.formats.html import AssertableHtml

    return AssertableHtml(markup)

mcp

mcp(payload: Any) -> AssertableMCP
Source code in src/pyssertive/assertions.py
def mcp(self, payload: Any) -> AssertableMCP:
    from pyssertive.protocols.mcp import AssertableMCP

    return AssertableMCP(payload)

arch

arch(
    name: str,
    callback: Callable[
        [AssertableArch | AssertableMultiArch], object
    ]
    | None = None,
) -> AssertableArch | AssertableMultiArch
Source code in src/pyssertive/assertions.py
def arch(
    self,
    name: str,
    callback: Callable[[AssertableArch | AssertableMultiArch], object] | None = None,
) -> AssertableArch | AssertableMultiArch:
    from pyssertive.arch import assert_arch

    return assert_arch(name, callback)