Exemple #1
0
def check_and_set_subclass(instance, attribute, value, subclasses):
    """Validate subclass of passed value on supported type and set him for
    instance of some class.

    :param instance: object, for with necessary make check and sets value.
    :param attribute: name of attribute (string), at which write ``value``
                      when checking passed without any errors.
    :param value: passed value to validate.
    :param subclasses: class or a list/tuple with acceptable classes.
    """
    obj_type = get_object_type(value)
    if issubclass(obj_type, subclasses):
        setattr(instance, attribute, value)
    else:
        raise TypeError('Custom class must be inherited from the '
                        '{} class.'.format(to_str(subclasses)))
Exemple #2
0
def check_and_set_subclass(instance, attribute, value, subclasses):
    """
    Validate subclass of passed value on supported type and set him for
    instance of some class.

    :param instance: object, for with necessary make check and sets value.
    :param attribute: name of attribute (string), at which write ``value``
                      when checking passed without any errors.
    :param value: passed value to validate.
    :param subclasses: class or a list/tuple with acceptable classes.
    """
    obj_type = get_object_type(value)
    if issubclass(obj_type, subclasses):
        setattr(instance, attribute, value)
    else:
        raise TypeError('Custom class must be inherited from the '
                        '{} class.'.format(to_str(subclasses)))
Exemple #3
0
def test_get_object_type(value, expected):
    assert get_object_type(value) is expected