コード例 #1
0
def get_class(classname, all=False):
    """Retrieve a class from the registry.

    :param str classname: Name of the class to be retrieved.
    :param bool all: If ``True``, return all classes registered using
        the given class name.
    :raise: :exc:`~marshmallow.exceptions.RegistryError` if the class
        cannot be found or if there are multiple entries for the given
        class name and ``all`` is not ``True``.
    :return: The class matching the name provided if previously
        registered, or potentially a list of classes if ``all`` is
        ``True``.

    """
    try:
        classes = _registry[classname]
    except KeyError:
        raise RegistryError(
            'Class with name {0!r} was not found. You may need '
            'to import the class.'.format(classname))
    if len(classes) > 1:
        if all:
            return _registry[classname]
        raise RegistryError('Multiple classes with name {0!r} '
                            'were found. Please use the full, '
                            'module-qualified path.'.format(classname))
    else:
        return _registry[classname][0]
コード例 #2
0
def get_class(
    classname: str, all: bool = False
) -> typing.Union[typing.List["SchemaType"], "SchemaType"]:
    """Retrieve a class from the registry.

    :raises: marshmallow.exceptions.RegistryError if the class cannot be found
        or if there are multiple entries for the given class name.
    """
    try:
        classes = _registry[classname]
    except KeyError as error:
        raise RegistryError(
            "Class with name {!r} was not found. You may need "
            "to import the class.".format(classname)
        ) from error
    if len(classes) > 1:
        if all:
            return _registry[classname]
        raise RegistryError(
            "Multiple classes with name {!r} "
            "were found. Please use the full, "
            "module-qualified path.".format(classname)
        )
    else:
        return _registry[classname][0]
コード例 #3
0
def get_class(classname, all=False):
    """Retrieve a class from the registry.

    :raises: marshmallow.exceptions.RegistryError if the class cannot be found
        or if there are multiple entries for the given class name.
    """
    try:
        classes = _registry[classname]
    except KeyError:
        raise RegistryError('Class with name {!r} was not found. You may need '
                            'to import the class.'.format(classname))
    if len(classes) > 1:
        if all:
            return _registry[classname]
        raise RegistryError('Multiple classes with name {!r} '
                            'were found. Please use the full, '
                            'module-qualified path.'.format(classname))
    else:
        return _registry[classname][0]