Skip to content

Commit 384a1ea

Browse files
authored
Fixes and improvements to API docs (#832)
1 parent a3fa83a commit 384a1ea

File tree

17 files changed

+147
-143
lines changed

17 files changed

+147
-143
lines changed

DOCUMENTATION.rst

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Then in a shell you could run:
9191
>>> auto_cli(Main, args=["--max_prize=1000", "person", "Lucky"]) # doctest: +ELLIPSIS
9292
'Lucky won ...€!'
9393

94-
If the given class does not have any methods, there will be no sub-commands and
94+
If the given class does not have any methods, there will be no subcommands and
9595
:func:`.auto_cli` will return an instance of the class. For example:
9696

9797
.. testcode::
@@ -129,11 +129,11 @@ run via :ref:`sub-commands` similar to the single class example above, i.e.
129129
``example.py function [arguments]`` where ``function`` is the name of the
130130
function to execute. If multiple classes or a mixture of functions and classes
131131
is given to :func:`.auto_cli`, to execute a method of a class, two levels of
132-
:ref:`sub-commands` are required. The first sub-command would be the name of the
132+
:ref:`sub-commands` are required. The first subcommand would be the name of the
133133
class and the second the name of the method, i.e. ``example.py class
134134
[init_arguments] method [arguments]``.
135135

136-
Arbitrary levels of sub-commands with custom names can be defined by providing a
136+
Arbitrary levels of subcommands with custom names can be defined by providing a
137137
``dict``. For example:
138138

139139
.. testcode::
@@ -2560,7 +2560,7 @@ Experimental ``omegaconf+`` mode
25602560
An experimental ``omegaconf+`` parser mode is available, which addresses the
25612561
limitations of the ``omegaconf`` mode mentioned earlier. Instead of applying
25622562
OmegaConf resolvers to each YAML config individually, the resolving is performed
2563-
once at the end of the parsing process. As a result, in nested sub-configs,
2563+
once at the end of the parsing process. As a result, in nested subconfigs,
25642564
references to nodes must be either relative or parser-level absolute to function
25652565
correctly. Alternatively, you can
25662566
``set_parsing_settings(omegaconf_absolute_to_relative_paths=True)`` to enable
@@ -2569,7 +2569,7 @@ that this automatic conversion does not work for every possible case.
25692569

25702570
Based on community feedback, this mode may become the default ``omegaconf`` mode
25712571
in version 5.0.0. This change would introduce a breaking modification, as
2572-
absolute node references would no longer work in nested sub-configs.
2572+
absolute node references would no longer work in nested subconfigs.
25732573

25742574

25752575
.. _environment-variables:
@@ -2625,19 +2625,19 @@ variables.
26252625

26262626
.. _sub-commands:
26272627

2628-
Sub-commands
2629-
============
2628+
Subcommands
2629+
===========
26302630

26312631
A way to define parsers in a modular way is what in argparse is known as
2632-
`sub-commands <https://docs.python.org/3/library/argparse.html#sub-commands>`__.
2633-
However, to promote modularity, in jsonargparse sub-commands work a bit
2634-
different than in argparse. To add sub-commands to a parser, the
2632+
`subcommands <https://docs.python.org/3/library/argparse.html#subcommands>`__.
2633+
However, to promote modularity, in jsonargparse subcommands work a bit
2634+
different than in argparse. To add subcommands to a parser, the
26352635
:py:meth:`.ArgumentParser.add_subcommands` method is used. Then an existing
2636-
parser is added as a sub-command using :func:`.add_subcommand`. In a parsed
2637-
config object the sub-command will be stored in the ``subcommand`` entry (or
2638-
whatever ``dest`` was set to), and the values of the sub-command will be in an
2639-
entry with the same name as the respective sub-command. An example of defining a
2640-
parser with sub-commands is the following:
2636+
parser is added as a subcommand using :func:`.add_subcommand`. In a parsed
2637+
config object the subcommand will be stored in the ``subcommand`` entry (or
2638+
whatever ``dest`` was set to), and the values of the subcommand will be in an
2639+
entry with the same name as the respective subcommand. An example of defining a
2640+
parser with subcommands is the following:
26412641

26422642
.. testcode::
26432643

@@ -2680,29 +2680,29 @@ valid YAML would be:
26802680
Parsing of environment variables works similar to :class:`.ActionParser`. For
26812681
the example parser above, all environment variables for ``subcomm1`` would have
26822682
as prefix ``APP_SUBCOMM1_`` and likewise for ``subcomm2`` as prefix
2683-
``APP_SUBCOMM2_``. The sub-command to use could be chosen by setting environment
2683+
``APP_SUBCOMM2_``. The subcommand to use could be chosen by setting environment
26842684
variable ``APP_SUBCOMMAND``.
26852685

2686-
It is possible to have multiple levels of sub-commands. With multiple levels
2687-
there is one basic requirement: the sub-commands must be added in the order of
2686+
It is possible to have multiple levels of subcommands. With multiple levels
2687+
there is one basic requirement: the subcommands must be added in the order of
26882688
the levels. This is, first call :func:`add_subcommands` and
26892689
:func:`add_subcommand` for the first level. Only after do the same for the
26902690
second level, and so on.
26912691

26922692

26932693
.. _json-schemas:
26942694

2695-
Json schemas
2695+
JSON Schemas
26962696
============
26972697

26982698
The :class:`.ActionJsonSchema` class is provided to allow parsing and validation
26992699
of values using a JSON Schema. This class requires the `jsonschema
27002700
<https://pypi.org/project/jsonschema/>`__ Python package. Though note that
2701-
jsonschema is not a requirement of the minimal jsonargparse install. To enable
2702-
this functionality install with the ``jsonschema`` extra as explained in section
2703-
:ref:`installation`.
2701+
``jsonschema`` is not a requirement of the minimal jsonargparse install. To
2702+
enable this functionality install with the ``jsonschema`` extra as explained in
2703+
section :ref:`installation`.
27042704

2705-
Check out the `jsonschema documentation
2705+
Check out the `JSON Schema documentation
27062706
<https://python-jsonschema.readthedocs.io/>`__ to learn how to write a schema.
27072707
The current version of jsonargparse uses Draft7Validator. Parsing an argument
27082708
using a JSON Schema is done like in the following example:

jsonargparse/_actions.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def __init__(self, message: str = "option unavailable", **kwargs):
442442
"""Initializer for ActionFail instance.
443443
444444
Args:
445-
message: Text for the error to show. Use `%(option)s`/`%(value)s` to include the option and/or value.
445+
message: Text for the error to show. Use ``%(option)s``/``%(value)s`` to include the option and/or value.
446446
"""
447447
if len(kwargs) == 0:
448448
self._message = message
@@ -464,7 +464,7 @@ def __call__(self, *args, **kwargs):
464464

465465

466466
class ActionYesNo(Action):
467-
"""Paired options --[yes_prefix]opt, --[no_prefix]opt to set True or False respectively."""
467+
"""Paired options ``--[yes_prefix]opt``, ``--[no_prefix]opt`` to set ``True`` or ``False`` respectively."""
468468

469469
def __init__(self, yes_prefix: str = "", no_prefix: str = "no_", **kwargs):
470470
"""Initializer for ActionYesNo instance.
@@ -547,12 +547,12 @@ class ActionParser:
547547

548548
def __init__(
549549
self,
550-
parser: Optional[ArgumentParser] = None,
550+
parser: ArgumentParser,
551551
):
552552
"""Initializer for ActionParser instance.
553553
554554
Args:
555-
parser (Optional[ArgumentParser]): A parser to parse the option with.
555+
parser: A parser to parse the option with.
556556
557557
Raises:
558558
ValueError: If the parser parameter is invalid.
@@ -643,7 +643,7 @@ def add_parser(self, name, **kwargs):
643643
raise NotImplementedError("In jsonargparse subcommands are added using the add_subcommand method.")
644644

645645
def add_subcommand(self, name, parser, **kwargs):
646-
"""Adds a parser as a sub-command parser.
646+
"""Adds a parser as a subcommand parser.
647647
648648
In contrast to `argparse.ArgumentParser.add_subparsers
649649
<https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_subparsers>`_
@@ -680,7 +680,7 @@ def add_subcommand(self, name, parser, **kwargs):
680680
return parser
681681

682682
def __call__(self, parser, namespace, values, option_string=None):
683-
"""Adds sub-command dest and parses sub-command arguments."""
683+
"""Adds subcommand dest and parses subcommand arguments."""
684684
subcommand = values[0]
685685
arg_strings = values[1:]
686686

jsonargparse/_cli.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ def auto_cli(
3939
):
4040
"""Simple creation of command line interfaces.
4141
42-
Previously CLI, renamed to follow the standard of functions in lowercase.
42+
Previously called ``jsonargparse.CLI``, renamed to follow the standard of functions in lowercase.
4343
4444
Creates an argument parser from one or more functions/classes, parses
4545
arguments and runs one of the functions or class methods depending on what
46-
was parsed. If the 'components' parameter is not given, then the components
46+
was parsed. If the ``components`` parameter is not given, then the components
4747
will be all the locals in the context and defined in the same module as from
48-
where auto_cli is called.
48+
where ``auto_cli`` is called.
4949
5050
Args:
5151
components: One or more functions/classes to include in the command line interface.
52-
args: List of arguments to parse or None to use sys.argv.
52+
args: List of arguments to parse or ``None`` to use ``sys.argv``.
5353
config_help: Help string for config file option in help.
5454
set_defaults: Dictionary of values to override components defaults.
5555
as_positional: Whether to add required parameters as positional arguments.
5656
fail_untyped: Whether to raise exception if a required parameter does not have a type.
57-
parser_class: The ArgumentParser class to use.
57+
parser_class: The :class:`ArgumentParser` subclass to use.
5858
**kwargs: Used to instantiate :class:`.ArgumentParser`.
5959
6060
Returns:
@@ -130,7 +130,7 @@ def auto_cli(
130130

131131

132132
def auto_parser(*args, **kwargs) -> ArgumentParser:
133-
"""Same as auto_cli, but returns the parser, so doesn't parse arguments or run.
133+
"""Same as :func:`.auto_cli`, but returns the parser, doesn't parse arguments or run.
134134
135135
This is a shorthand for ``capture_parser(lambda: auto_cli(*args, **kwargs))``.
136136
"""

jsonargparse/_common.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,25 @@ def set_parsing_settings(
120120
121121
Args:
122122
validate_defaults: Whether default values must be valid according to the
123-
argument type. The default is False, meaning no default validation,
124-
like in argparse.
123+
argument type. The default is ``False``, meaning no default
124+
validation, like in argparse.
125125
config_read_mode_urls_enabled: Whether to read config files from URLs
126-
using requests package. Default is False.
126+
using requests package. Default is ``False``.
127127
config_read_mode_fsspec_enabled: Whether to read config files from
128-
fsspec supported file systems. Default is False.
128+
fsspec supported file systems. Default is ``False``.
129129
docstring_parse_style: The docstring style to expect. Default is
130-
DocstringStyle.AUTO.
130+
``DocstringStyle.AUTO``.
131131
docstring_parse_attribute_docstrings: Whether to parse attribute
132-
docstrings (slower). Default is False.
133-
parse_optionals_as_positionals: If True, the parser will take extra
132+
docstrings (slower). Default is ``False``.
133+
parse_optionals_as_positionals: If ``True``, the parser will take extra
134134
positional command line arguments as values for optional arguments.
135-
This means that optional arguments can be given by name --key=value
136-
as usual, but also as positional. The extra positionals are applied
137-
to optionals in the order that they were added to the parser. By
138-
default, this is False.
135+
This means that optional arguments can be given by name
136+
``--key=value`` as usual, but also as positional. The extra
137+
positionals are applied to optionals in the order that they were
138+
added to the parser. By default, this is ``False``.
139139
stubs_resolver_allow_py_files: Whether the stubs resolver should search
140140
in ``.py`` files in addition to ``.pyi`` files.
141-
omegaconf_absolute_to_relative_paths: If True, when loading configs
141+
omegaconf_absolute_to_relative_paths: If ``True``, when loading configs
142142
with ``omegaconf+`` parser mode, absolute interpolation paths are
143143
converted to relative. This is only intended for backward
144144
compatibility with ``omegaconf`` parser mode.
@@ -433,7 +433,6 @@ class LoggerProperty:
433433
"""Class designed to be inherited by other classes to add a logger property."""
434434

435435
def __init__(self, *args, logger: Union[bool, str, dict, logging.Logger] = False, **kwargs):
436-
"""Initializer for LoggerProperty class."""
437436
self.logger = logger
438437
super().__init__(*args, **kwargs)
439438

0 commit comments

Comments
 (0)