@@ -436,19 +436,22 @@ def _bind_to_class(self, toml_dict: Mapping[str, Any], instance: T | None, conte
436436 else :
437437 return replace (instance , ** parsed ) # type: ignore[type-var]
438438
439- def format_toml (self ) -> Iterator [str ]:
439+ def format_toml (self , context : str = "" ) -> Iterator [str ]:
440440 """
441441 Yield lines of TOML text for populating the dataclass or object that we are binding to.
442442
443443 If we are binding to an object, non-default values from that object will be output.
444444
445445 If we are binding to a class, example values for mandatory fields will be derived from the field types;
446446 these example values can be syntactically incorrect placeholders.
447+
448+ The `context` parameter contains a dot-separated key path for the bound object/class:
449+ this will be prefixed to all yielded TOML table names.
447450 """
448451
449- return self ._format_toml_root (template = False )
452+ return self ._format_toml_root (context = context , template = False )
450453
451- def format_toml_template (self ) -> Iterator [str ]:
454+ def format_toml_template (self , context : str = "" ) -> Iterator [str ]:
452455 """
453456 Yield lines of TOML text as a template for populating the dataclass or object that we are binding to.
454457
@@ -457,12 +460,15 @@ def format_toml_template(self) -> Iterator[str]:
457460
458461 If we are binding to an object, values from that object will be used to populate the template.
459462 If we are binding to a class, example values will be derived from the field types.
463+
464+ The `context` parameter contains a dot-separated key path for the bound object/class:
465+ this will be prefixed to all yielded TOML table names.
460466 """
461467
462- return self ._format_toml_root (template = True )
468+ return self ._format_toml_root (context = context , template = True )
463469
464- def _format_toml_root (self , * , template : bool ) -> Iterator [str ]:
465- table = Table (self , "" , self ._instance , None )
470+ def _format_toml_root (self , * , context : str , template : bool ) -> Iterator [str ]:
471+ table = Table (self , context , self ._instance , None )
466472 lines = table .format_table (set (), template = template )
467473 for line in lines :
468474 if line :
0 commit comments