import json
import pickle
import yaml
import yaml  # @Reimport
from typing import Callable


def make_unserializer(method: str) -> Callable:
    """ returns a function that unserialize data with the  given method
    
    This is the inverse function of :func:`make_serializer`.
    
    Args:
        method (str): An identifier determining the unserializer that will be
            returned
    
    Returns:
        callable: A function that serializes objects    
    
    """
    if callable(method):
        return method

    if method is None:
        return lambda s: s

    if method == 'json':
        return lambda s: json.loads(s.decode('utf-8'))

    if method == 'pickle':
        return lambda s: pickle.loads(s)

    if method == 'yaml':
        return yaml.full_load
    
    if method == 'yaml_unsafe':
        return yaml.unsafe_load  # type: ignore
    
    raise ValueError('Unknown serialization method `%s`' % method)