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

Fix custom instantiators not working for nested dependency injection #608

Merged
merged 2 commits into from
Oct 29, 2024
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Fixed
<https://github.com/omni-us/jsonargparse/pull/597>`__).
- Callables that return class not considering previous values (`#603
<https://github.com/omni-us/jsonargparse/pull/603>`__).
- Custom instantiators not working for nested dependency injection (`#608
<https://github.com/omni-us/jsonargparse/pull/608>`__).

Changed
^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion jsonargparse/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __call__(self, class_type: Type[ClassType], *args, **kwargs) -> ClassType:
defaults_cache: ContextVar[Optional[Namespace]] = ContextVar("defaults_cache", default=None)
lenient_check: ContextVar[Union[bool, str]] = ContextVar("lenient_check", default=False)
load_value_mode: ContextVar[Optional[str]] = ContextVar("load_value_mode", default=None)
class_instantiators: ContextVar[Optional[InstantiatorsDictType]] = ContextVar("class_instantiators")
class_instantiators: ContextVar[Optional[InstantiatorsDictType]] = ContextVar("class_instantiators", default=None)
nested_links: ContextVar[List[dict]] = ContextVar("nested_links", default=[])


Expand Down
5 changes: 5 additions & 0 deletions jsonargparse/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from ._common import (
InstantiatorCallable,
InstantiatorsDictType,
class_instantiators,
Dismissed Show dismissed Hide dismissed
debug_mode_active,
is_dataclass_like,
lenient_check,
Expand Down Expand Up @@ -1139,6 +1140,10 @@
parent_instantiators = self.parent_parser._get_instantiators()
instantiators = instantiators.copy()
instantiators.update({k: v for k, v in parent_instantiators.items() if k not in instantiators})
context_instantiators = class_instantiators.get()
if context_instantiators:
instantiators = instantiators.copy()
instantiators.update({k: v for k, v in context_instantiators.items() if k not in instantiators})
return instantiators

def instantiate_classes(
Expand Down
15 changes: 15 additions & 0 deletions jsonargparse_tests/test_subclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,21 @@ def test_custom_instantiation_replace(parser):
assert list(parser._instantiators.values())[0] is second_instantiator


class CustomInstantiationNested:
def __init__(self, sub: CustomInstantiationBase):
self.sub = sub


def test_custom_instantiation_nested(parser):
parser.add_argument("--cls", type=CustomInstantiationNested)
parser.add_instantiator(instantiator("nested"), CustomInstantiationBase, subclasses=True)
cfg = parser.parse_args(["--cls=CustomInstantiationNested", "--cls.sub=CustomInstantiationSub"])
init = parser.instantiate_classes(cfg)
assert isinstance(init.cls, CustomInstantiationNested)
assert isinstance(init.cls.sub, CustomInstantiationSub)
assert init.cls.sub.call == "nested"


# environment tests


Expand Down
Loading