Coverage for /home/ubuntu/cv-depot/python/cv_depot/core/enforce.py: 100%
7 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-08 20:26 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-08 20:26 +0000
1from typing import Iterable # noqa F401
3from lunchbox.enforce import Enforce
4# ------------------------------------------------------------------------------
7def enforce_homogenous_type(iterable, name='Iterable'):
8 # type: (Iterable, str) -> None
9 '''
10 Ensures that iterable only contains only one type of object.
12 Args:
13 items (iterable): Iterable.
14 name (str, optional): First word in error message. Default: Iterable.
16 Raises:
17 EnforceError: If iterable contains more than one type of object.
18 '''
19 types = [x.__class__.__name__ for x in iterable]
20 types = sorted(list(set(types)))
21 msg = f'{name} may only contain one type of object. Found types: {types}.'
22 Enforce(len(types), '==', 1, message=msg)