11import argparse
22import inspect
33import os
4+ import sys
45from dataclasses import dataclass
56from logging import getLogger
67from pathlib import Path
78from pprint import pprint
89from textwrap import dedent
910from typing import Any , Callable , Dict , List , Optional
1011
11- from hydra ._internal .defaults_list import DefaultsList
12+ try :
13+ from lerna ._internal .defaults_list import DefaultsList
14+ except ImportError :
15+ from hydra ._internal .defaults_list import DefaultsList
1216from omegaconf import DictConfig , ListConfig , OmegaConf
1317
1418try :
@@ -115,7 +119,10 @@ def _find_group_options(config_loader, path, config_name, overrides, results):
115119 Note that it will pick up config files that are not intended to be used as config group options,
116120 but that exist to provide common config options to other files in the group (i.e. to default)
117121 """
118- from hydra .core .object_type import ObjectType
122+ try :
123+ from lerna .core .object_type import ObjectType
124+ except ImportError :
125+ from hydra .core .object_type import ObjectType
119126
120127 groups = config_loader .get_group_options (path , ObjectType .GROUP , config_name , overrides )
121128 options = config_loader .get_group_options (path , ObjectType .CONFIG , config_name , overrides )
@@ -184,9 +191,10 @@ def load_config(
184191 debug: (Experimental) Whether to enable debug mode. This will return more information about the configs on ConfigLoadResult.
185192 """
186193 # Heavy import, only import if used
187- import os
188-
189- from hydra import compose , initialize_config_dir
194+ try :
195+ from lerna import compose , initialize_config_dir
196+ except ImportError :
197+ from hydra import compose , initialize_config_dir
190198
191199 if return_hydra_config and debug :
192200 raise ValueError ("Cannot return hydra config and debug=True at the same time. Please set return_hydra_config=False." )
@@ -209,22 +217,47 @@ def load_config(
209217 result = ConfigLoadResult (root_config_dir = root_config_dir , root_config_name = root_config_name , cfg = cfg )
210218 if debug :
211219 import yaml
212- from hydra .core .global_hydra import GlobalHydra
213- from hydra .types import RunMode
220+
221+ try :
222+ from lerna .core .global_hydra import GlobalHydra
223+ from lerna .types import RunMode
224+ except ImportError :
225+ from hydra .core .global_hydra import GlobalHydra
226+ from hydra .types import RunMode
214227
215228 # To track the source file for each config value, we need to monkey patch the yaml loader
216229 original_yaml_load = yaml .load
230+ # Lerna may use a Rust YAML parser that bypasses yaml.load entirely.
231+ # Temporarily disable it so our monkey patch can intercept all YAML loading.
232+ _rust_patches = {}
233+ try :
234+ for _mod_name in (
235+ "lerna._internal.core_plugins.file_config_source" ,
236+ "lerna._internal.core_plugins.importlib_resources_config_source" ,
237+ ):
238+ _mod = sys .modules .get (_mod_name )
239+ if _mod and getattr (_mod , "_RUST_AVAILABLE" , False ):
240+ _rust_patches [_mod ] = True
241+ _mod ._RUST_AVAILABLE = False
242+ except Exception :
243+ pass
244+
217245 try :
218246
219247 def yaml_load (* args , ** kwargs ):
220248 res = original_yaml_load (* args , ** kwargs )
221- return _dict_add_source (res , args [0 ].name )
249+ # hydra passes file objects (with .name) to yaml.load;
250+ # lerna passes strings. Use "unknown" as fallback source.
251+ source = getattr (args [0 ], "name" , "unknown" ) if args else "unknown"
252+ return _dict_add_source (res , source )
222253
223254 yaml .load = yaml_load
224255 # We can't load the hydra config after monkey patching yaml loading, so skip that step
225256 result .cfg_sources = compose (config_name = root_config_name , overrides = overrides , return_hydra_config = False )
226257 finally :
227258 yaml .load = original_yaml_load
259+ for _mod , _val in _rust_patches .items ():
260+ _mod ._RUST_AVAILABLE = _val
228261
229262 config_loader = GlobalHydra .instance ().config_loader ()
230263 # Load defaults list using the standard hydra function
0 commit comments