Tries to find the class that defined the specified method. Will not work for nested classes
    (locals).

    Args:
        fun: Function / Method

    Returns:
        Returns the class which defines the given method / function.

def get_class_that_defined_method(fun):
    """
    Tries to find the class that defined the specified method. Will not work for nested classes
    (locals).

    Args:
        fun: Function / Method

    Returns:
        Returns the class which defines the given method / function.
    """
    if inspect.ismethod(fun):
        for cls in inspect.getmro(fun.__self__.__class__):
            if cls.__dict__.get(fun.__name__) is fun:
                return cls
        fun = fun.__func__  # fallback to __qualname__ parsing
    if inspect.isfunction(fun):
        cls = getattr(inspect.getmodule(fun),
                      fun.__qualname__.split('.<locals>', 1)[0].rsplit('.', 1)[0], None)
        if isinstance(cls, type):
            return cls
    return getattr(fun, '__objclass__', None)