1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2025-2026 Benjamin Abendroth <braph93@gmx.de>
'''Type utility functions.'''
from types import NoneType
from .errors import CrazyTypeError
def is_dict_type(obj):
'''Return if `obj` is a dictionary type.'''
return hasattr(obj, 'items')
def is_list_type(obj):
'''Return if `obj` is list or tuple.'''
return isinstance(obj, (list, tuple))
def validate_type(value, types, parameter_name):
'''Check if `value` is one of `types`.'''
if not isinstance(value, types):
types_strings = []
for t in types:
types_strings.append({
str: 'str',
int: 'int',
float: 'float',
list: 'list',
tuple: 'tuple',
dict: 'dict',
bool: 'bool',
NoneType: 'None'}[t])
types_string = '|'.join(types_strings)
raise CrazyTypeError(parameter_name, types_string, value)
|