@@ -221,7 +221,11 @@ def to_flat_dict(self, _prefix: str = "") -> dict[str, Any]:
221
221
222
222
223
223
def print_configuration (
224
- config : Configuration | BaseModel , section : str | None = None , indent : int = 4
224
+ config : Configuration | BaseModel ,
225
+ section : str | None = None ,
226
+ * ,
227
+ skip_none : bool = False ,
228
+ indent : int = 4 ,
225
229
) -> None :
226
230
"""Pretty print a configuration.
227
231
@@ -232,18 +236,22 @@ def print_configuration(
232
236
)
233
237
if section is None :
234
238
_print , _escape = _get_rich_print ()
235
- return _print_dict_as_tree (cfg , indent = indent , _print = _print , _escape = _escape )
239
+ return _print_dict_as_tree (
240
+ cfg , skip_none = skip_none , indent = indent , _print = _print , _escape = _escape
241
+ )
236
242
237
243
cfg_section = cfg .get (section )
238
244
if cfg_section is None :
239
245
message = f"'{ type (cfg ).__name__ } ' has no section '{ section } '"
240
246
raise KeyError (message ) from None
241
247
242
248
if isinstance (cfg_section , Configuration ):
243
- return print_configuration (cfg_section , indent = indent )
249
+ return print_configuration (cfg_section , skip_none = skip_none , indent = indent )
244
250
245
251
* _ , key = section .split ("." )
246
- return print_configuration (Configuration ({key : cfg_section }), indent = indent )
252
+ return print_configuration (
253
+ Configuration ({key : cfg_section }), skip_none = skip_none , indent = indent
254
+ )
247
255
248
256
249
257
def _get_rich_print () -> tuple [
@@ -264,13 +272,17 @@ def _get_rich_print() -> tuple[
264
272
265
273
def _print_dict_as_tree (
266
274
data : dict [str , Any ] | UserDict [str , Any ] | Configuration ,
275
+ * ,
276
+ skip_none : bool = False ,
267
277
indent : int = 4 ,
268
278
current_indent : int = 0 ,
269
279
_print : Callable [[str ], None ] = print ,
270
280
_escape : Callable [[str ], str ] = str ,
271
281
) -> None :
272
282
"""Print a nested dictionary as a tree."""
273
283
for key , value in data .items ():
284
+ if skip_none and value is None :
285
+ continue
274
286
if isinstance (value , dict | UserDict | Configuration ):
275
287
_print (" " * current_indent + f"- { key } " )
276
288
_print_dict_as_tree (
0 commit comments