Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for partialmethods and partial types #664

Open
MiguelMonteiro opened this issue Jan 30, 2025 · 0 comments · May be fixed by #665
Open

Add support for partialmethods and partial types #664

MiguelMonteiro opened this issue Jan 30, 2025 · 0 comments · May be fixed by #665
Labels
enhancement New feature or request

Comments

@MiguelMonteiro
Copy link
Contributor

🚀 Feature request

Add support for objects created via functools.partial and functools.partialmethod

Motivation

Sometimes dynamically created classes and functions via partialmethod and partial are useful to prevent repeating code.

Pitch

I was able to implement this locally by changing the following:
Implement a function that checks if the method of class partial or partialmethod

from functools import partial, partialmethod
def is_partial_method(method):
    return isinstance(method, partialmethod) or isinstance(method, partial)

add another code path in get_component_and_parent in file _parameter_resolver.py:

def get_component_and_parent(
    function_or_class: Union[Callable, Type],
    method_or_property: Optional[Union[str, Callable]] = None,
):
    if is_subclass(function_or_class, ClassFromFunctionBase) and method_or_property in {None, "__init__"}:
        function_or_class = function_or_class.wrapped_function  # type: ignore[union-attr]
        if isinstance(function_or_class, MethodType):
            method_or_property = function_or_class.__name__
            function_or_class = function_or_class.__self__  # type: ignore[assignment]
        else:
            method_or_property = None
    elif inspect.isclass(get_generic_origin(function_or_class)) and method_or_property is None:
        method_or_property = "__init__"
    elif method_or_property and not isinstance(method_or_property, str):
        method_or_property = method_or_property.__name__
    parent = component = None
    if method_or_property:
        attr = inspect.getattr_static(get_generic_origin(function_or_class), method_or_property)
        if is_staticmethod(attr):
            component = getattr(function_or_class, method_or_property)
            return component, parent, method_or_property
        parent = function_or_class
        if has_dunder_new_method(function_or_class, method_or_property):
            component = getattr(function_or_class, "__new__")
        elif is_method(attr):
            component = attr
        elif is_partial_method(attr):
            component = getattr(function_or_class, method_or_property)
        elif is_property(attr):
            component = attr.fget
        elif isinstance(attr, classmethod):
            component = getattr(function_or_class, method_or_property)
        elif attr is not object.__init__:
            raise ValueError(
                f"Invalid or unsupported input: class={function_or_class}, method_or_property={method_or_property}"
            )
    else:
        if not callable(function_or_class):
            raise ValueError(f"Non-callable input: function={function_or_class}")
        component = function_or_class
    return component, parent, method_or_property

Just need to open and MR but thought I would create an issue first

Alternatives

@MiguelMonteiro MiguelMonteiro added the enhancement New feature or request label Jan 30, 2025
@MiguelMonteiro MiguelMonteiro linked a pull request Jan 30, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant