Skip to content

Replace nested partial_instance function with functools.partial for picklability #727

Description

@euxhenh

Description

The current implementation of adapt_class_type uses a locally defined function partial_instance as a wrapper:

https://github.com/omni-us/jsonargparse/blob/dcd0a5ae70d0ef5c7c053436375bcad9aa462c84/jsonargparse/_typehints.py#L1428-L1431

This causes a few issues with pickle serialization, as locally defined functions are not picklable.

Proposed Fix

Replace the custom function with a functools.partial, which is picklable:

return functools.partial(
    instantiator_fn,
    val_class,
    **{**init_args, **dict_kwargs},
)

From what I see this should maintain the same behavior but ensures compatibility with serialization libraries. Can you think of any reason why not to support this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions