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

feat: support for partialmethod #665

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Added
- Experimental support for sub-classing ``ArgumentParser`` to customize
``add_argument`` (`#661
<https://github.com/omni-us/jsonargparse/pull/661>`__).

- Support for partialmethods (`#665
<https://github.com/omni-us/jsonargparse/pull/665>`__).

v4.36.0 (2025-01-17)
--------------------
Expand Down
8 changes: 7 additions & 1 deletion jsonargparse/_parameter_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from contextlib import contextmanager, suppress
from contextvars import ContextVar
from copy import deepcopy
from functools import partial
from functools import partial, partialmethod
from importlib import import_module
from types import MethodType
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union
Expand Down Expand Up @@ -102,6 +102,10 @@ def is_method(attr) -> bool:
)


def is_partial_method(attr) -> bool:
return isinstance(attr, partialmethod)


def is_property(attr) -> bool:
return isinstance(attr, property)

Expand Down Expand Up @@ -509,6 +513,8 @@ def get_component_and_parent(
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):
Expand Down
10 changes: 10 additions & 0 deletions jsonargparse_tests/test_parameter_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import inspect
import xml.dom
from calendar import Calendar
from functools import partialmethod
from random import shuffle
from typing import Any, Callable, Dict, List, Optional, Union
from unittest.mock import patch
Expand Down Expand Up @@ -33,6 +34,8 @@ def method_a(self, pma1: int, pma2: float, kma1: str = "x"):
kma1: help for kma1
"""

partial_method_a = partialmethod(method_a, pma1=1, pma2=0.5)


class ClassB(ClassA):
def __init__(self, pkb1: str, kb1: int = 3, kb2: str = "4", **kwargs):
Expand Down Expand Up @@ -899,6 +902,13 @@ def test_get_params_some_ignored():
assert_params(get_params(func_given_kwargs), ["p", "p1"], help=False)


def test_partialmethod():
ClassA.partial_method_a = partialmethod(ClassA.method_a, pma1=1, pma2=0.5)
assert_params(get_params(ClassA, "partial_method_a"), ["pma1", "pma2", "kma1"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking again at this test now I think there are things missing. The partial doesn't change the list of parameter names. But it does change the defaults. Are the defaults correctly inferred by a parser? If not, then it doesn't really work as expected. So this should be tested.

And another thing that could be considered missing is that a method could be set as default to a callable. For this --print_config should serialize this default into an import path string. And this import path when used in a config, should be correctly deserialize into the partial method object.

with source_unavailable():
assert_params(get_params(ClassA, "partial_method_a"), ["pma1", "pma2", "kma1"])


# unsupported cases


Expand Down
Loading