from typing import Union
from typing import Type
from typing import Any
from typing import MutableMapping
import inspect


def check_attributes(
    item: Union[object, Type[Any]], 
    attributes: MutableMapping[str, Type[Any]]) -> bool:
    """Returns whether 'attributes' exist in 'item' and are the right type.
    
    Args:
        item (Union[object, Type[Any]]): class or instance to examine.
        attributes (MutableMapping[str, Type[Any]]): dict where keys are the
            attribute names and values are the expected types of whose named
            attributes.
            
    Returns
        bool: whether all of the 'attributes' exist in 'item' and are of the
            proper type.
            
    """
    matched = True
    if inspect.isclass:
        for attribute, value in attributes.items():
            if value is not None:
                try:
                    testing = getattr(item, attribute)
                    testing = item.__annotations__[testing]
                except AttributeError:
                    return False
                try:
                    if not issubclass(testing, value):
                        return False
                except TypeError:
                    pass
    else:
        for attribute, value in attributes.items():
            if value is not None:
                try:
                    testing = getattr(item, attribute)
                except AttributeError:
                    return False
                try:
                    if not isinstance(testing, value):
                        return False
                except TypeError:
                    pass
    return matched