Skip to content

Commit

Permalink
Fix regression that prevented **kwargs parameter resolving when an Op…
Browse files Browse the repository at this point in the history
…tional[Callable] type is used (#476)
  • Loading branch information
mauvilsa authored Mar 19, 2024
1 parent bb83fdc commit 08cc9d8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ The semantic versioning only considers the public API as described in
paths are considered internals and can change in minor and patch releases.


v4.27.7 (2024-03-??)
--------------------

Fixed
^^^^^
- Regression from `14456c2
<https://github.com/omni-us/jsonargparse/commit/14456c21ff7a11ba420f010d2b21bcfdb14977a2>`__
that prevented ``**kwargs`` parameter resolving when an ``Optional[Callable]``
type is used (`#473
<https://github.com/omni-us/jsonargparse/issues/473>`__).


v4.27.6 (2024-03-15)
--------------------

Expand Down
6 changes: 5 additions & 1 deletion jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,11 @@ def get_callable_return_type(typehint):

def get_subclass_types(typehint, callable_return=True):
subclass_types = None
if callable_return and ActionTypeHint.is_callable_typehint(typehint, all_subtypes=False) and typehint.__args__:
if (
callable_return
and ActionTypeHint.is_callable_typehint(typehint, all_subtypes=False)
and getattr(typehint, "__args__", None)
):
typehint = get_optional_arg(typehint)
typehint = typehint.__args__[-1]
if ActionTypeHint.is_subclass_typehint(typehint, all_subtypes=False):
Expand Down
14 changes: 13 additions & 1 deletion jsonargparse_tests/test_parameter_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import xml.dom
from calendar import Calendar
from random import shuffle
from typing import Any, Callable, Dict, List, Union
from typing import Any, Callable, Dict, List, Optional, Union
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -541,6 +541,14 @@ def conditional_calls(**kwargs):
cond_3(**kwargs)


def function_optional_callable(p1: Optional[Callable] = None, **kw):
"""
Args:
p1: help for p1
"""
function_no_args_no_kwargs(**kw)


def assert_params(params, expected, origins={}):
assert expected == [p.name for p in params]
docs = [f"help for {p.name}" for p in params] if docstring_parser_support else [None] * len(params)
Expand Down Expand Up @@ -864,6 +872,10 @@ def test_conditional_calls_kwargs():
assert get_params(conditional_calls) == []


def test_get_params_optional_callable():
assert_params(get_params(function_optional_callable), ["p1", "pk1", "k2"])


# unsupported cases


Expand Down

0 comments on commit 08cc9d8

Please sign in to comment.