Combine uniquness and length checks

This commit is contained in:
Julian Stirling 2025-07-02 13:25:07 +01:00
parent 35409ee490
commit 8a28a17461
2 changed files with 40 additions and 16 deletions

View file

@ -1,3 +1,26 @@
"""
This directory contains utitlities that help with testing and debugging
This sub-package contains utilities that help with testing and debugging.
At the top level are some very basic testing functions, more specific testing
is provides by modules inside the package.
"""
from typing import Protocol, Iterable
from collections.abc import Hashable
class SizedIterableHashable(Iterable[Hashable], Protocol):
"""A protocol for sized iterable of hashable objects"""
def __len__(self) -> int: ...
def assert_unique_of_length(data: SizedIterableHashable, length: int) -> None:
"""Assert that a list (or other iterable) has unique contents of a given length.
:param data: A list or other sized iterable of hashable objects. To be checked
for unique contents and length.
:param length: The expected length of data
"""
assert len(data) == len(set(data))
assert len(data) == length