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])
| 705 | |
| 706 | |
| 707 | def 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 | |
| 732 | class CustomCompletionSettings: |
no outgoing calls
no test coverage detected
searching dependent graphs…