Sequence Types¶
IsListOrTuple ¶
IsListOrTuple(*items: Any, positions: Optional[Dict[int, Any]] = None, check_order: bool = True, length: LengthType = None)
Bases: DirtyEquals[T]
Check that some object is a list or tuple and optionally its values match some constraints.
IsListOrTuple
and its subclasses can be initialised in two ways:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*items |
Any
|
Positional members of an object to check. These must start from the zeroth position, but
(depending on the value of |
()
|
check_order |
bool
|
Whether to enforce the order of the items. |
True
|
length |
Union[int, Tuple[int, Union[int, Any]]]
|
length constraints, int or tuple matching the arguments
of |
None
|
or,
Parameters:
from dirty_equals import AnyThing, IsListOrTuple
assert [1, 2, 3] == IsListOrTuple(1, 2, 3)
assert (1, 3, 2) == IsListOrTuple(1, 2, 3, check_order=False)
assert [{'a': 1}, {'a': 2}] == (
IsListOrTuple({'a': 2}, {'a': 1}, check_order=False)
)
assert [1, 2, 3, 3] != IsListOrTuple(1, 2, 3, check_order=False)
assert [1, 2, 3, 4, 5] == IsListOrTuple(1, 2, 3, length=...)
assert [1, 2, 3, 4, 5] != IsListOrTuple(1, 2, 3, length=(8, 10))
assert ['a', 'b', 'c', 'd'] == (IsListOrTuple(positions={2: 'c', 3: 'd'}))
assert ['a', 'b', 'c', 'd'] == (
IsListOrTuple(positions={2: 'c', 3: 'd'}, length=4)
)
assert [1, 2, 3, 4] == IsListOrTuple(3, check_order=False, length=(0, ...))
assert [1, 2, 3] == IsListOrTuple(AnyThing, AnyThing, 3)
IsList ¶
IsList(*items: Any, positions: Optional[Dict[int, Any]] = None, check_order: bool = True, length: LengthType = None)
Bases: IsListOrTuple[List[Any]]
All the same functionality as IsListOrTuple
, but the compared value must be a list.
from dirty_equals import IsList
assert [1, 2, 3] == IsList(1, 2, 3)
assert [1, 2, 3] == IsList(positions={2: 3})
assert [1, 2, 3] == IsList(1, 2, 3, check_order=False)
assert [1, 2, 3, 4] == IsList(1, 2, 3, length=4)
assert [1, 2, 3, 4] == IsList(1, 2, 3, length=(4, 5))
assert [1, 2, 3, 4] == IsList(1, 2, 3, length=...)
assert (1, 2, 3) != IsList(1, 2, 3)
IsListOrTuple
and its subclasses can be initialised in two ways:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*items |
Any
|
Positional members of an object to check. These must start from the zeroth position, but
(depending on the value of |
()
|
check_order |
bool
|
Whether to enforce the order of the items. |
True
|
length |
Union[int, Tuple[int, Union[int, Any]]]
|
length constraints, int or tuple matching the arguments
of |
None
|
or,
Parameters:
from dirty_equals import AnyThing, IsListOrTuple
assert [1, 2, 3] == IsListOrTuple(1, 2, 3)
assert (1, 3, 2) == IsListOrTuple(1, 2, 3, check_order=False)
assert [{'a': 1}, {'a': 2}] == (
IsListOrTuple({'a': 2}, {'a': 1}, check_order=False)
)
assert [1, 2, 3, 3] != IsListOrTuple(1, 2, 3, check_order=False)
assert [1, 2, 3, 4, 5] == IsListOrTuple(1, 2, 3, length=...)
assert [1, 2, 3, 4, 5] != IsListOrTuple(1, 2, 3, length=(8, 10))
assert ['a', 'b', 'c', 'd'] == (IsListOrTuple(positions={2: 'c', 3: 'd'}))
assert ['a', 'b', 'c', 'd'] == (
IsListOrTuple(positions={2: 'c', 3: 'd'}, length=4)
)
assert [1, 2, 3, 4] == IsListOrTuple(3, check_order=False, length=(0, ...))
assert [1, 2, 3] == IsListOrTuple(AnyThing, AnyThing, 3)
IsTuple ¶
IsTuple(*items: Any, positions: Optional[Dict[int, Any]] = None, check_order: bool = True, length: LengthType = None)
Bases: IsListOrTuple[Tuple[Any, ...]]
All the same functionality as IsListOrTuple
, but the compared value must be a tuple.
from dirty_equals import IsTuple
assert (1, 2, 3) == IsTuple(1, 2, 3)
assert (1, 2, 3) == IsTuple(positions={2: 3})
assert (1, 2, 3) == IsTuple(1, 2, 3, check_order=False)
assert (1, 2, 3, 4) == IsTuple(1, 2, 3, length=4)
assert (1, 2, 3, 4) == IsTuple(1, 2, 3, length=(4, 5))
assert (1, 2, 3, 4) == IsTuple(1, 2, 3, length=...)
assert [1, 2, 3] != IsTuple(1, 2, 3)
IsListOrTuple
and its subclasses can be initialised in two ways:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*items |
Any
|
Positional members of an object to check. These must start from the zeroth position, but
(depending on the value of |
()
|
check_order |
bool
|
Whether to enforce the order of the items. |
True
|
length |
Union[int, Tuple[int, Union[int, Any]]]
|
length constraints, int or tuple matching the arguments
of |
None
|
or,
Parameters:
from dirty_equals import AnyThing, IsListOrTuple
assert [1, 2, 3] == IsListOrTuple(1, 2, 3)
assert (1, 3, 2) == IsListOrTuple(1, 2, 3, check_order=False)
assert [{'a': 1}, {'a': 2}] == (
IsListOrTuple({'a': 2}, {'a': 1}, check_order=False)
)
assert [1, 2, 3, 3] != IsListOrTuple(1, 2, 3, check_order=False)
assert [1, 2, 3, 4, 5] == IsListOrTuple(1, 2, 3, length=...)
assert [1, 2, 3, 4, 5] != IsListOrTuple(1, 2, 3, length=(8, 10))
assert ['a', 'b', 'c', 'd'] == (IsListOrTuple(positions={2: 'c', 3: 'd'}))
assert ['a', 'b', 'c', 'd'] == (
IsListOrTuple(positions={2: 'c', 3: 'd'}, length=4)
)
assert [1, 2, 3, 4] == IsListOrTuple(3, check_order=False, length=(0, ...))
assert [1, 2, 3] == IsListOrTuple(AnyThing, AnyThing, 3)
HasLen ¶
Bases: DirtyEquals[Sized]
Check that some has a given length, or length in a given range.
Parameters:
from dirty_equals import HasLen
assert [1, 2, 3] == HasLen(3)
assert '123' == HasLen(3, ...)
assert (1, 2, 3) == HasLen(3, 5)
assert (1, 2, 3) == HasLen(0, ...)
Contains ¶
Bases: DirtyEquals[Container[Any]]
Check that an object contains one or more values.
Parameters:
from dirty_equals import Contains
assert [1, 2, 3] == Contains(1)
assert [1, 2, 3] == Contains(1, 2)
assert (1, 2, 3) == Contains(1)
assert 'abc' == Contains('b')
assert {'a': 1, 'b': 2} == Contains('a')
assert [1, 2, 3] != Contains(10)