88from ._core import ArgumentParser
99from ._loaders_dumpers import get_loader_exceptions , load_value
1010from ._optionals import _get_config_read_mode
11+ from ._paths import change_to_path_dir
1112from ._required import clear_required , iter_required_keys
1213from ._typehints import is_subclass_spec , resolve_class_path_by_name
13- from ._util import import_object
14+ from ._util import import_object , load_config_path_context
1415
1516__all__ = ["FromConfigMixin" ]
1617
@@ -30,7 +31,9 @@ class FromConfigMixin:
3031 defaults.
3132
3233 2. Adds a ``from_config`` ``@classmethod``, that instantiates the class
33- based on a config file or dict.
34+ based on a config file or dict. If
35+ ``config_read_mode_fsspec_enabled=True`` is set, then config paths
36+ can be URLs.
3437
3538 Attributes:
3639 __from_config_init_defaults__: Optional path to a config file for
@@ -61,12 +64,17 @@ def from_config(cls: type[T], config: str | PathLike | dict) -> T:
6164def _parse_class_kwargs_from_config (cls : type [T ], config : str | PathLike | dict , ** kwargs ) -> tuple [dict , type [T ]]:
6265 """Parse the init kwargs for ``cls`` from a config file or dict."""
6366 parser = ArgumentParser (exit_on_error = False , ** kwargs )
67+ cfg_path = None
6468 if not isinstance (config , dict ):
6569 from .typing import Path
6670
6771 cfg_path = Path (config , mode = _get_config_read_mode ())
68- cfg_str = cfg_path .read_text ()
69- with parser_context (load_value_mode = parser .parser_mode ):
72+ with (
73+ load_config_path_context (cfg_path ),
74+ change_to_path_dir (cfg_path ),
75+ parser_context (load_value_mode = parser .parser_mode ),
76+ ):
77+ cfg_str = cfg_path .read_text ()
7078 try :
7179 config = load_value (cfg_str , path = str (config ))
7280 except get_loader_exceptions () as ex :
@@ -86,7 +94,8 @@ def _parse_class_kwargs_from_config(cls: type[T], config: str | PathLike | dict,
8694 parser .add_class_arguments (cls )
8795 for required in iter_required_keys (parser ):
8896 clear_required (parser , required )
89- cfg = parser .parse_object (config , defaults = False )
97+ with load_config_path_context (cfg_path ), change_to_path_dir (cfg_path ):
98+ cfg = parser .parse_object (config , defaults = False )
9099 return parser .instantiate (cfg ).as_dict (), cls
91100
92101
0 commit comments