MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / get_defining_class

Function get_defining_class

cmd2/utils.py:707–729  ·  view source on GitHub ↗

Attempt to resolve the class that defined a method. Inspired by implementation published here: https://stackoverflow.com/a/25959545/1956611 :param meth: method to inspect :return: class type in which the supplied method was defined. None if it couldn't be resolved.

(meth: Callable[..., Any])

Source from the content-addressed store, hash-verified

705
706
707def get_defining_class(meth: Callable[..., Any]) -> type[Any] | None:
708 """Attempt to resolve the class that defined a method.
709
710 Inspired by implementation published here:
711 https://stackoverflow.com/a/25959545/1956611
712
713 :param meth: method to inspect
714 :return: class type in which the supplied method was defined. None if it couldn't be resolved.
715 """
716 if isinstance(meth, functools.partial):
717 return get_defining_class(meth.func)
718 if inspect.ismethod(meth) or (
719 inspect.isbuiltin(meth) and hasattr(meth, "__self__") and hasattr(meth.__self__, "__class__")
720 ):
721 for cls in inspect.getmro(meth.__self__.__class__):
722 if meth.__name__ in cls.__dict__:
723 return cls
724 meth = getattr(meth, "__func__", meth) # fallback to __qualname__ parsing
725 if inspect.isfunction(meth):
726 cls = getattr(inspect.getmodule(meth), meth.__qualname__.split(".<locals>", 1)[0].rsplit(".", 1)[0])
727 if isinstance(cls, type):
728 return cls
729 return cast(type, getattr(meth, "__objclass__", None)) # handle special descriptor objects
730
731
732class CustomCompletionSettings:

Callers 2

_get_command_categoryMethod · 0.85
_resolve_func_selfMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…