Skip to content

Commit b7a1b33

Browse files
committed
config_read_mode and docstring_parse options can now be set using set_parsing_settings.
1 parent a530e3c commit b7a1b33

16 files changed

+180
-77
lines changed

CHANGELOG.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Changed
3333
- Untyped parameters with ``None`` default no longer skipped when
3434
``fail_untyped=True`` (`#697
3535
<https://github.com/omni-us/jsonargparse/pull/697>`__).
36+
- ``config_read_mode`` and ``docstring_parse`` options can now be set using
37+
``set_parsing_settings`` (`#711
38+
<https://github.com/omni-us/jsonargparse/pull/711>`__).
3639

3740
Fixed
3841
^^^^^
@@ -43,6 +46,15 @@ Fixed
4346
- List append nested in subclass not working (`#710
4447
<https://github.com/omni-us/jsonargparse/pull/710>`__).
4548

49+
Deprecated
50+
^^^^^^^^^^
51+
- ``get_config_read_mode`` and ``set_docstring_parse_options`` are deprecated
52+
and will be removed in v5.0.0, instead use ``set_parsing_settings`` (`#711
53+
<https://github.com/omni-us/jsonargparse/pull/711>`__).
54+
- ``get_config_read_mode`` is deprecated and will be removed in v5.0.0. There will
55+
be no replacement since this is considered internal (`#711
56+
<https://github.com/omni-us/jsonargparse/pull/711>`__).
57+
4658

4759
v4.38.0 (2025-03-26)
4860
--------------------

DOCUMENTATION.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,10 @@ triggered to check if it is accessible. To get the content of the parsed path,
701701
without needing to care if it is a local file or a URL, the
702702
:py:meth:`.Path.get_content` method Scan be used.
703703

704-
If you import ``from jsonargparse import set_config_read_mode`` and then run
705-
``set_config_read_mode(urls_enabled=True)`` or
706-
``set_config_read_mode(fsspec_enabled=True)``, the following functions and
707-
classes will also support loading from URLs:
704+
If you import ``from jsonargparse import set_parsing_settings`` and then run
705+
``set_parsing_settings(config_read_mode_urls_enabled=True)`` or
706+
``set_parsing_settings(config_read_mode_fsspec_enabled=True)``, the following
707+
functions and classes will also support loading from URLs:
708708
:py:meth:`.ArgumentParser.parse_path`, :py:meth:`.ArgumentParser.get_defaults`
709709
(``default_config_files`` argument), `action="config"`,
710710
:class:`.ActionJsonSchema`, :class:`.ActionJsonnet` and :class:`.ActionParser`.
@@ -1576,9 +1576,9 @@ single style, this is inefficient. A single style can be configured as follows:
15761576
.. testcode:: docstrings
15771577

15781578
from docstring_parser import DocstringStyle
1579-
from jsonargparse import set_docstring_parse_options
1579+
from jsonargparse import set_parsing_settings
15801580

1581-
set_docstring_parse_options(style=DocstringStyle.REST)
1581+
set_parsing_settings(docstring_parse_style=DocstringStyle.REST)
15821582

15831583
The second option that can be configured is the support for `attribute
15841584
docstrings <https://peps.python.org/pep-0257/#what-is-a-docstring>`__ (i.e.
@@ -1589,9 +1589,9 @@ that don't have attribute docstrings. To enable, do as follows:
15891589
.. testcode:: docstrings
15901590

15911591
from dataclasses import dataclass
1592-
from jsonargparse import set_docstring_parse_options
1592+
from jsonargparse import set_parsing_settings
15931593

1594-
set_docstring_parse_options(attribute_docstrings=True)
1594+
set_parsing_settings(docstring_parse_attribute_docstrings=True)
15951595

15961596

15971597
@dataclass
@@ -1606,8 +1606,8 @@ that don't have attribute docstrings. To enable, do as follows:
16061606

16071607
.. testcleanup:: docstrings
16081608

1609-
set_docstring_parse_options(style=DocstringStyle.GOOGLE)
1610-
set_docstring_parse_options(attribute_docstrings=False)
1609+
set_parsing_settings(docstring_parse_style=DocstringStyle.GOOGLE)
1610+
set_parsing_settings(docstring_parse_attribute_docstrings=False)
16111611

16121612
Customization of arguments
16131613
--------------------------

jsonargparse/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@
6464
__all__ += _namespace.__all__
6565
__all__ += _formatters.__all__
6666
__all__ += _optionals.__all__
67+
__all__ += _common.__all__
6768
__all__ += _loaders_dumpers.__all__
6869
__all__ += _util.__all__
69-
__all__ += _common.__all__
7070
__all__ += _deprecated.__all__
7171

7272

jsonargparse/_actions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ._common import Action, is_subclass, parser_context
1313
from ._loaders_dumpers import get_loader_exceptions, load_value
1414
from ._namespace import Namespace, NSKeyError, split_key, split_key_root
15-
from ._optionals import get_config_read_mode
15+
from ._optionals import _get_config_read_mode
1616
from ._type_checking import ActionsContainer, ArgumentParser
1717
from ._util import (
1818
Path,
@@ -194,7 +194,7 @@ def apply_config(parser, cfg, dest, value) -> None:
194194
with _ActionSubCommands.not_single_subcommand(), previous_config_context(cfg), skip_apply_links():
195195
kwargs = {"env": False, "defaults": False, "_skip_validation": True, "_fail_no_subcommand": False}
196196
try:
197-
cfg_path: Optional[Path] = Path(value, mode=get_config_read_mode())
197+
cfg_path: Optional[Path] = Path(value, mode=_get_config_read_mode())
198198
except TypeError as ex_path:
199199
try:
200200
if isinstance(load_value(value), str):

jsonargparse/_common.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from contextlib import contextmanager
77
from contextvars import ContextVar
88
from typing import ( # type: ignore[attr-defined]
9+
TYPE_CHECKING,
910
Dict,
1011
Generic,
1112
List,
@@ -20,6 +21,8 @@
2021

2122
from ._namespace import Namespace
2223
from ._optionals import (
24+
_set_config_read_mode,
25+
_set_docstring_parse_options,
2326
capture_typing_extension_shadows,
2427
get_alias_target,
2528
get_annotated_base_type,
@@ -31,6 +34,9 @@
3134
)
3235
from ._type_checking import ActionsContainer, ArgumentParser
3336

37+
if TYPE_CHECKING:
38+
import docstring_parser
39+
3440
__all__ = [
3541
"LoggerProperty",
3642
"null_logger",
@@ -98,6 +104,10 @@ def parser_context(**kwargs):
98104
def set_parsing_settings(
99105
*,
100106
validate_defaults: Optional[bool] = None,
107+
config_read_mode_urls_enabled: Optional[bool] = None,
108+
config_read_mode_fsspec_enabled: Optional[bool] = None,
109+
docstring_parse_style: Optional["docstring_parser.DocstringStyle"] = None,
110+
docstring_parse_attribute_docstrings: Optional[bool] = None,
101111
parse_optionals_as_positionals: Optional[bool] = None,
102112
) -> None:
103113
"""
@@ -107,17 +117,37 @@ def set_parsing_settings(
107117
validate_defaults: Whether default values must be valid according to the
108118
argument type. The default is False, meaning no default validation,
109119
like in argparse.
120+
config_read_mode_urls_enabled: Whether to read config files from URLs
121+
using requests package. Default is False.
122+
config_read_mode_fsspec_enabled: Whether to read config files from
123+
fsspec supported file systems. Default is False.
124+
docstring_parse_style: The docstring style to expect. Default is
125+
DocstringStyle.AUTO.
126+
docstring_parse_attribute_docstrings: Whether to parse attribute
127+
docstrings (slower). Default is False.
110128
parse_optionals_as_positionals: [EXPERIMENTAL] If True, the parser will
111129
take extra positional command line arguments as values for optional
112130
arguments. This means that optional arguments can be given by name
113131
--key=value as usual, but also as positional. The extra positionals
114132
are applied to optionals in the order that they were added to the
115133
parser. By default, this is False.
116134
"""
135+
# validate_defaults
117136
if isinstance(validate_defaults, bool):
118137
parsing_settings["validate_defaults"] = validate_defaults
119138
elif validate_defaults is not None:
120139
raise ValueError(f"validate_defaults must be a boolean, but got {validate_defaults}.")
140+
# config_read_mode
141+
if config_read_mode_urls_enabled is not None:
142+
_set_config_read_mode(urls_enabled=config_read_mode_urls_enabled)
143+
if config_read_mode_fsspec_enabled is not None:
144+
_set_config_read_mode(fsspec_enabled=config_read_mode_fsspec_enabled)
145+
# docstring_parse
146+
if docstring_parse_style is not None:
147+
_set_docstring_parse_options(style=docstring_parse_style)
148+
if docstring_parse_attribute_docstrings is not None:
149+
_set_docstring_parse_options(attribute_docstrings=docstring_parse_attribute_docstrings)
150+
# parse_optionals_as_positionals
121151
if isinstance(parse_optionals_as_positionals, bool):
122152
parsing_settings["parse_optionals_as_positionals"] = parse_optionals_as_positionals
123153
elif parse_optionals_as_positionals is not None:

jsonargparse/_core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
strip_meta,
7878
)
7979
from ._optionals import (
80+
_get_config_read_mode,
8081
fsspec_support,
81-
get_config_read_mode,
8282
import_fsspec,
8383
import_jsonnet,
8484
pyyaml_available,
@@ -619,7 +619,7 @@ def parse_path(
619619
Raises:
620620
ArgumentError: If the parsing fails error and exit_on_error=True.
621621
"""
622-
fpath = Path(cfg_path, mode=get_config_read_mode())
622+
fpath = Path(cfg_path, mode=_get_config_read_mode())
623623
with change_to_path_dir(fpath):
624624
cfg_str = fpath.get_content()
625625
parsed_cfg = self.parse_string(
@@ -971,7 +971,7 @@ def _get_default_config_files(self) -> List[Tuple[Optional[str], Path]]:
971971

972972
if len(default_config_files) > 0:
973973
with suppress(TypeError):
974-
return [(k, Path(v, mode=get_config_read_mode())) for k, v in default_config_files]
974+
return [(k, Path(v, mode=_get_config_read_mode())) for k, v in default_config_files]
975975
return []
976976

977977
def get_default(self, dest: str) -> Any:

jsonargparse/_deprecated.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"ActionPath",
2323
"ActionPathList",
2424
"ParserError",
25+
"get_config_read_mode",
26+
"set_docstring_parse_options",
27+
"set_config_read_mode",
2528
"set_url_support",
2629
"usage_and_exit_error_handler",
2730
]
@@ -350,16 +353,66 @@ def _check_type(self, value):
350353
"""
351354
set_url_support was deprecated in v3.12.0 and will be removed in v5.0.0.
352355
Optional config read modes should now be set using function
353-
set_config_read_mode.
356+
set_parsing_settings.
354357
"""
355358
)
356359
def set_url_support(enabled: bool):
357360
"""Enables/disables URL support for config read mode."""
358-
from ._optionals import get_config_read_mode, set_config_read_mode
361+
from ._optionals import _get_config_read_mode, _set_config_read_mode
359362

360-
set_config_read_mode(
363+
_set_config_read_mode(
361364
urls_enabled=enabled,
362-
fsspec_enabled=True if "s" in get_config_read_mode() else False,
365+
fsspec_enabled=True if "s" in _get_config_read_mode() else False,
366+
)
367+
368+
369+
@deprecated(
370+
"""
371+
set_config_read_mode was deprecated in v4.39.0 and will be removed in
372+
v5.0.0. Optional config read modes should now be set using function
373+
set_parsing_settings.
374+
"""
375+
)
376+
def set_config_read_mode(
377+
urls_enabled: bool = False,
378+
fsspec_enabled: bool = False,
379+
):
380+
"""Enables/disables optional config read modes."""
381+
from ._optionals import _set_config_read_mode
382+
383+
_set_config_read_mode(
384+
urls_enabled=urls_enabled,
385+
fsspec_enabled=fsspec_enabled,
386+
)
387+
388+
389+
@deprecated(
390+
"""
391+
get_config_read_mode was deprecated in v4.39.0 and will be removed in
392+
v5.0.0. The config read mode is internal and thus shouldn't be used.
393+
"""
394+
)
395+
def get_config_read_mode() -> str:
396+
"""Returns the current config reading mode."""
397+
from ._optionals import _get_config_read_mode
398+
399+
return _get_config_read_mode()
400+
401+
402+
@deprecated(
403+
"""
404+
set_docstring_parse_options was deprecated in v4.39.0 and will be removed in
405+
v5.0.0. Docstring parse options should now be set using function
406+
set_parsing_settings.
407+
"""
408+
)
409+
def set_docstring_parse_options(style=None, attribute_docstrings: Optional[bool] = None):
410+
"""Sets options for docstring parsing."""
411+
from ._optionals import _set_docstring_parse_options
412+
413+
_set_docstring_parse_options(
414+
style=style,
415+
attribute_docstrings=attribute_docstrings,
363416
)
364417

365418

jsonargparse/_jsonnet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from ._jsonschema import ActionJsonSchema
88
from ._loaders_dumpers import get_loader_exceptions, load_value
99
from ._optionals import (
10-
get_config_read_mode,
10+
_get_config_read_mode,
1111
get_jsonschema_exceptions,
1212
import_jsonnet,
1313
import_jsonschema,
@@ -157,7 +157,7 @@ def parse(
157157
fname = "snippet"
158158
snippet = jsonnet
159159
try:
160-
fpath = Path(jsonnet, mode=get_config_read_mode())
160+
fpath = Path(jsonnet, mode=_get_config_read_mode())
161161
except TypeError:
162162
pass
163163
else:

jsonargparse/_optionals.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
from typing import Optional, Union
99

1010
__all__ = [
11-
"get_config_read_mode",
12-
"set_config_read_mode",
13-
"set_docstring_parse_options",
11+
"_get_config_read_mode",
1412
]
1513

1614

@@ -165,7 +163,7 @@ def import_reconplogger(importer):
165163
return reconplogger
166164

167165

168-
def set_config_read_mode(
166+
def _set_config_read_mode(
169167
urls_enabled: bool = False,
170168
fsspec_enabled: bool = False,
171169
):
@@ -183,7 +181,7 @@ def set_config_read_mode(
183181
def update_mode(flag, enabled):
184182
global _config_read_mode
185183
if enabled:
186-
imports[flag]("set_config_read_mode")
184+
imports[flag]("_set_config_read_mode")
187185
if flag not in _config_read_mode:
188186
_config_read_mode = _config_read_mode.replace("f", "f" + flag)
189187
else:
@@ -193,20 +191,20 @@ def update_mode(flag, enabled):
193191
update_mode("s", fsspec_enabled)
194192

195193

196-
def get_config_read_mode() -> str:
194+
def _get_config_read_mode() -> str:
197195
"""Returns the current config reading mode."""
198196
return _config_read_mode
199197

200198

201-
def set_docstring_parse_options(style=None, attribute_docstrings: Optional[bool] = None):
199+
def _set_docstring_parse_options(style=None, attribute_docstrings: Optional[bool] = None):
202200
"""Sets options for docstring parsing.
203201
204202
Args:
205203
style (docstring_parser.DocstringStyle): The docstring style to expect.
206204
attribute_docstrings: Whether to parse attribute docstrings (slower).
207205
"""
208206
global _docstring_parse_options
209-
dp = import_docstring_parser("set_docstring_parse_options")
207+
dp = import_docstring_parser("_set_docstring_parse_options")
210208
if style is not None:
211209
if not isinstance(style, dp.DocstringStyle):
212210
raise ValueError(f"Expected style to be of type {dp.DocstringStyle}.")

jsonargparse/_typehints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,9 @@ def adapt_typehints(
870870
list_path = None
871871
if enable_path and type(val) is str:
872872
with suppress(TypeError):
873-
from ._optionals import get_config_read_mode
873+
from ._optionals import _get_config_read_mode
874874

875-
list_path = Path(val, mode=get_config_read_mode())
875+
list_path = Path(val, mode=_get_config_read_mode())
876876
val = list_path.get_content().splitlines()
877877
if isinstance(val, NestedArg) and subtypehints is not None:
878878
val = (prev_val[:-1] if isinstance(prev_val, list) else []) + [val]

jsonargparse/_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
from ._deprecated import PathDeprecations
4040
from ._loaders_dumpers import json_compact_dump, load_value
4141
from ._optionals import (
42+
_get_config_read_mode,
4243
fsspec_support,
43-
get_config_read_mode,
4444
import_fsspec,
4545
import_requests,
4646
url_support,
@@ -135,7 +135,7 @@ def parse_value_or_config(
135135
cfg_path = None
136136
if enable_path and type(value) is str and value != "-":
137137
try:
138-
cfg_path = Path(value, mode=get_config_read_mode())
138+
cfg_path = Path(value, mode=_get_config_read_mode())
139139
except TypeError:
140140
pass
141141
else:

0 commit comments

Comments
 (0)