File: type_utils.py

package info (click to toggle)
crazy-complete 0.3.7-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 13,342; sh: 995; makefile: 68
file content (40 lines) | stat: -rw-r--r-- 1,031 bytes parent folder | download
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)