File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2525 (`#875 <https://github.com/omni-us/jsonargparse/pull/875 >`__).
2626- ``add_class_arguments `` skip support for callable return class parameters
2727 (`#882 <https://github.com/omni-us/jsonargparse/pull/882 >`__).
28+ - ``FromConfigMixin `` is now an official public API feature
29+ (`#884 <https://github.com/omni-us/jsonargparse/pull/884 >`__).
2830
2931Fixed
3032^^^^^
Original file line number Diff line number Diff line change @@ -1465,6 +1465,28 @@ methods are:
14651465 is possible to use classes from third party libraries which is not possible
14661466 for developers to modify.
14671467
1468+ From config mixin
1469+ -----------------
1470+
1471+ For classes that should support direct instantiation from configuration values,
1472+ :class: `.FromConfigMixin ` adds a ``from_config `` class method. It can be useful
1473+ for small utilities that need to load constructor values from a dictionary or a
1474+ configuration file in one call.
1475+
1476+ .. doctest ::
1477+
1478+ >>> from jsonargparse import FromConfigMixin
1479+ >>> class Client (FromConfigMixin ):
1480+ ... def __init__ (self , host : str = " localhost" , port : int = 80 ):
1481+ ... self .host = host
1482+ ... self .port = port
1483+ >>> client = Client.from_config({" host" : " api.local" , " port" : 8080 })
1484+ >>> (client.host, client.port)
1485+ ('api.local', 8080)
1486+
1487+ For details on all supported behavior, see :class: `.FromConfigMixin ` in the API
1488+ reference.
1489+
14681490Docstring parsing
14691491-----------------
14701492
Original file line number Diff line number Diff line change 4747 _core ,
4848 _deprecated ,
4949 _formatters ,
50+ _from_config ,
5051 _jsonnet ,
5152 _jsonschema ,
5253 _link_arguments ,
6162__all__ += _cli .__all__
6263__all__ += _core .__all__
6364__all__ += _signatures .__all__
65+ __all__ += _from_config .__all__
6466__all__ += _link_arguments .__all__
6567__all__ += _subcommands .__all__
6668__all__ += _jsonschema .__all__
Original file line number Diff line number Diff line change @@ -32,9 +32,9 @@ class FromConfigMixin:
3232 based on a config file or dict.
3333
3434 Attributes:
35- `` __from_config_init_defaults__`` : Optional path to a config file for
35+ __from_config_init_defaults__: Optional path to a config file for
3636 overriding ``__init__`` defaults.
37- `` __from_config_parser_kwargs__`` : Additional kwargs to pass to the
37+ __from_config_parser_kwargs__: Additional kwargs to pass to the
3838 ArgumentParser used for parsing configs.
3939 """
4040
You can’t perform that action at this time.
0 commit comments