33import pytest
44
55from jsonargparse import FromConfigMixin
6- from jsonargparse_tests .conftest import json_or_yaml_dump , skip_if_omegaconf_unavailable
6+ from jsonargparse ._paths import PathError
7+ from jsonargparse_tests .conftest import json_or_yaml_dump , skip_if_no_pyyaml , skip_if_omegaconf_unavailable
78
89# __init__ defaults override tests
910
@@ -178,16 +179,47 @@ class DefaultsOverrideInvalid(DefaultsOverrideParent):
178179# from_config method tests
179180
180181
182+ class FromConfigMethodParent (FromConfigMixin ):
183+ def __init__ (self , parent_param : str = "parent_default" ):
184+ self .parent_param = parent_param
185+
186+
187+ class FromConfigMethodChild (FromConfigMethodParent ):
188+ def __init__ (self , child_param : str = "child_default" , ** kwargs ):
189+ super ().__init__ (** kwargs )
190+ self .child_param = child_param
191+
192+
181193def test_from_config_method_path (tmp_cwd ):
182194 config_path = tmp_cwd / "config.yaml"
183- config_path .write_text (json_or_yaml_dump ({"param" : "value_from_file" }))
195+ config_path .write_text (json_or_yaml_dump ({"parent_param" : "value_from_file" }))
196+
197+ instance = FromConfigMethodParent .from_config (config_path )
198+ assert instance .parent_param == "value_from_file"
199+
200+
201+ def test_from_config_method_path_does_not_exist (tmp_cwd ):
202+ config_path = tmp_cwd / "config.yaml"
203+
204+ with pytest .raises (PathError , match = "File does not exist:" ):
205+ FromConfigMethodParent .from_config (config_path )
206+
207+
208+ @skip_if_no_pyyaml
209+ def test_from_config_method_path_invalid_yaml (tmp_cwd ):
210+ config_path = tmp_cwd / "config.yaml"
211+ config_path .write_text ("::: invalid content :::" )
212+
213+ with pytest .raises (TypeError , match = "Problems parsing config" ):
214+ FromConfigMethodParent .from_config (config_path )
184215
185- class FromConfigMethodPath (FromConfigMixin ):
186- def __init__ (self , param : str = "default_value" ):
187- self .param = param
188216
189- instance = FromConfigMethodPath .from_config (config_path )
190- assert instance .param == "value_from_file"
217+ def test_from_config_method_path_not_dict (tmp_cwd ):
218+ config_path = tmp_cwd / "config.yaml"
219+ config_path .write_text ("[1, 2]" )
220+
221+ with pytest .raises (TypeError , match = "Expected config to be a dict or parse into a dict" ):
222+ FromConfigMethodParent .from_config (config_path )
191223
192224
193225def test_from_config_method_dict ():
@@ -224,15 +256,6 @@ def __init__(self, param1: str = "default_value", param2: int = 1):
224256
225257
226258def test_from_config_method_subclass ():
227- class FromConfigMethodParent (FromConfigMixin ):
228- def __init__ (self , parent_param : str = "parent_default" ):
229- self .parent_param = parent_param
230-
231- class FromConfigMethodChild (FromConfigMethodParent ):
232- def __init__ (self , child_param : str = "child_default" , ** kwargs ):
233- super ().__init__ (** kwargs )
234- self .child_param = child_param
235-
236259 instance = FromConfigMethodChild .from_config (
237260 {"parent_param" : "overridden_parent" , "child_param" : "overridden_child" }
238261 )
@@ -241,6 +264,27 @@ def __init__(self, child_param: str = "child_default", **kwargs):
241264 assert instance .child_param == "overridden_child"
242265
243266
267+ def test_from_config_method_class_path_subclass ():
268+ instance = FromConfigMethodParent .from_config (
269+ {
270+ "class_path" : f"{ __name__ } .FromConfigMethodChild" ,
271+ "init_args" : {"parent_param" : "overridden_parent" , "child_param" : "overridden_child" },
272+ }
273+ )
274+ assert isinstance (instance , FromConfigMethodChild )
275+ assert instance .parent_param == "overridden_parent"
276+ assert instance .child_param == "overridden_child"
277+
278+
279+ class SomeOtherClass :
280+ pass
281+
282+
283+ def test_from_config_method_class_path_not_subclass ():
284+ with pytest .raises (TypeError , match = "SomeOtherClass' is not a subclass of 'FromConfigMethodParent'" ):
285+ FromConfigMethodParent .from_config ({"class_path" : f"{ __name__ } .SomeOtherClass" })
286+
287+
244288@skip_if_omegaconf_unavailable
245289def test_from_config_method_parser_kwargs ():
246290 class FromConfigMethodParserKwargs (FromConfigMixin ):
0 commit comments