@@ -79,6 +79,9 @@ def _resolve_imports(
7979 snippet = load_config (config_path )
8080 if isinstance (snippet , dict ) and "imports" in snippet :
8181 snippet = _resolve_imports (snippet , _loading | {config_path })
82+ # Unwrap _list_content (multi-document YAML: imports + list content)
83+ if isinstance (snippet , dict ) and "_list_content" in snippet :
84+ snippet = snippet ["_list_content" ]
8285 import_map [name ] = snippet
8386
8487 def _lookup (ref_name : str , context : str ) -> Any :
@@ -89,58 +92,65 @@ def _lookup(ref_name: str, context: str) -> Any:
8992 )
9093 return import_map [ref_name ]
9194
95+ def _resolve_list (entries : list [Any ]) -> list [Any ]:
96+ """Resolve $import markers in a list of quant_cfg-style entries."""
97+ resolved : list [Any ] = []
98+ for entry in entries :
99+ if isinstance (entry , dict ) and _IMPORT_KEY in entry :
100+ # {$import: name} → splice imported list
101+ if len (entry ) > 1 :
102+ raise ValueError (
103+ f"$import must be the only key in the dict, got extra keys: "
104+ f"{ sorted (k for k in entry if k != _IMPORT_KEY )} "
105+ )
106+ imported = _lookup (entry [_IMPORT_KEY ], "list entry" )
107+ if not isinstance (imported , list ):
108+ raise ValueError (
109+ f"$import { entry [_IMPORT_KEY ]!r} in list must resolve to a "
110+ f"list, got { type (imported ).__name__ } ."
111+ )
112+ resolved .extend (imported )
113+ elif (
114+ isinstance (entry , dict )
115+ and isinstance (entry .get ("cfg" ), dict )
116+ and _IMPORT_KEY in entry ["cfg" ]
117+ ):
118+ # cfg: {$import: name_or_list, ...inline} → import then override
119+ #
120+ # Precedence (lowest → highest):
121+ # 1. Imports in list order (later imports override earlier)
122+ # 2. Inline keys (override all imports)
123+ ref = entry ["cfg" ].pop (_IMPORT_KEY )
124+ inline_keys = dict (entry ["cfg" ])
125+ ref_names = ref if isinstance (ref , list ) else [ref ]
126+
127+ merged : dict [str , Any ] = {}
128+ for name in ref_names :
129+ snippet = _lookup (name , f"cfg of { entry } " )
130+ if not isinstance (snippet , dict ):
131+ raise ValueError (
132+ f"$import { name !r} in cfg must resolve to a dict, "
133+ f"got { type (snippet ).__name__ } ."
134+ )
135+ merged .update (snippet )
136+
137+ merged .update (inline_keys )
138+ entry ["cfg" ] = merged
139+ resolved .append (entry )
140+ else :
141+ resolved .append (entry )
142+ return resolved
143+
92144 # Resolve $import references in quant_cfg entries
93145 quantize = data .get ("quantize" )
94146 if isinstance (quantize , dict ):
95147 quant_cfg = quantize .get ("quant_cfg" )
96148 if isinstance (quant_cfg , list ):
97- resolved_cfg : list [Any ] = []
98- for entry in quant_cfg :
99- if isinstance (entry , dict ) and _IMPORT_KEY in entry :
100- # {$import: name} → splice imported list into quant_cfg
101- if len (entry ) > 1 :
102- raise ValueError (
103- f"$import must be the only key in the dict, got extra keys: "
104- f"{ sorted (k for k in entry if k != _IMPORT_KEY )} "
105- )
106- imported = _lookup (entry [_IMPORT_KEY ], "quant_cfg entry" )
107- if not isinstance (imported , list ):
108- raise ValueError (
109- f"$import { entry [_IMPORT_KEY ]!r} in quant_cfg must resolve to a "
110- f"list, got { type (imported ).__name__ } . Config snippets used as "
111- f"quant_cfg entries must be YAML lists."
112- )
113- resolved_cfg .extend (imported )
114- elif (
115- isinstance (entry , dict )
116- and isinstance (entry .get ("cfg" ), dict )
117- and _IMPORT_KEY in entry ["cfg" ]
118- ):
119- # cfg: {$import: name_or_list, ...inline} → import then override
120- #
121- # Precedence (lowest → highest):
122- # 1. Imports in list order (later imports override earlier)
123- # 2. Inline keys (override all imports)
124- ref = entry ["cfg" ].pop (_IMPORT_KEY )
125- inline_keys = dict (entry ["cfg" ]) # remaining inline keys
126- ref_names = ref if isinstance (ref , list ) else [ref ]
127-
128- merged : dict [str , Any ] = {}
129- for name in ref_names :
130- snippet = _lookup (name , f"cfg of { entry } " )
131- if not isinstance (snippet , dict ):
132- raise ValueError (
133- f"$import { name !r} in cfg must resolve to a dict, "
134- f"got { type (snippet ).__name__ } ."
135- )
136- merged .update (snippet )
137-
138- merged .update (inline_keys )
139- entry ["cfg" ] = merged
140- resolved_cfg .append (entry )
141- else :
142- resolved_cfg .append (entry )
143- quantize ["quant_cfg" ] = resolved_cfg
149+ quantize ["quant_cfg" ] = _resolve_list (quant_cfg )
150+
151+ # Resolve $import references in _list_content (multi-document snippets)
152+ if "_list_content" in data :
153+ data ["_list_content" ] = _resolve_list (data ["_list_content" ])
144154
145155 return data
146156
0 commit comments