Skip to content

Commit 9bb0715

Browse files
authored
Make FromConfigMixin public and include in documentation (#884)
1 parent 324dd45 commit 9bb0715

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Added
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

2931
Fixed
3032
^^^^^

DOCUMENTATION.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
14681490
Docstring parsing
14691491
-----------------
14701492

jsonargparse/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
_core,
4848
_deprecated,
4949
_formatters,
50+
_from_config,
5051
_jsonnet,
5152
_jsonschema,
5253
_link_arguments,
@@ -61,6 +62,7 @@
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__

jsonargparse/_from_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)