diff --git a/box/box.py b/box/box.py index 8252643..235eeb8 100644 --- a/box/box.py +++ b/box/box.py @@ -1021,7 +1021,7 @@ def to_yaml( default_flow_style: bool = False, encoding: str = "utf-8", errors: str = "strict", - width: int = 120, + width: int = 0, **yaml_kwargs, ): """ @@ -1031,7 +1031,7 @@ def to_yaml( :param default_flow_style: False will recursively dump dicts :param encoding: File encoding :param errors: How to handle encoding errors - :param width: Line width for YAML output + :param width: Line width for YAML output (0 for no limit) :param yaml_kwargs: additional arguments to pass to yaml.dump :return: string of YAML (if no filename provided) """ @@ -1084,7 +1084,7 @@ def to_yaml( default_flow_style: bool = False, encoding: str = "utf-8", errors: str = "strict", - width: int = 120, + width: int = 0, **yaml_kwargs, ): raise BoxError('yaml is unavailable on this system, please install the "ruamel.yaml" or "PyYAML" package') diff --git a/box/converters.py b/box/converters.py index bb91ba6..db7adda 100644 --- a/box/converters.py +++ b/box/converters.py @@ -197,23 +197,24 @@ def _to_yaml( errors: str = "strict", ruamel_typ: str = "rt", ruamel_attrs: dict | None = None, - width: int = 120, + width: int = 0, **yaml_kwargs, ): if not ruamel_attrs: ruamel_attrs = {} + effective_width = width if width else None if filename: _exists(filename, create=True) with open(filename, "w", encoding=encoding, errors=errors) as f: if ruamel_available: yaml_dumper = YAML(typ=ruamel_typ) yaml_dumper.default_flow_style = default_flow_style - yaml_dumper.width = width + yaml_dumper.width = effective_width for attr, value in ruamel_attrs.items(): setattr(yaml_dumper, attr, value) return yaml_dumper.dump(obj, stream=f, **yaml_kwargs) elif pyyaml_available: - return yaml.dump(obj, stream=f, default_flow_style=default_flow_style, width=width, **yaml_kwargs) + return yaml.dump(obj, stream=f, default_flow_style=default_flow_style, width=effective_width, **yaml_kwargs) else: raise BoxError(MISSING_PARSER_ERROR) @@ -221,14 +222,14 @@ def _to_yaml( if ruamel_available: yaml_dumper = YAML(typ=ruamel_typ) yaml_dumper.default_flow_style = default_flow_style - yaml_dumper.width = width + yaml_dumper.width = effective_width for attr, value in ruamel_attrs.items(): setattr(yaml_dumper, attr, value) with StringIO() as string_stream: yaml_dumper.dump(obj, stream=string_stream, **yaml_kwargs) return string_stream.getvalue() elif pyyaml_available: - return yaml.dump(obj, default_flow_style=default_flow_style, width=width, **yaml_kwargs) + return yaml.dump(obj, default_flow_style=default_flow_style, width=effective_width, **yaml_kwargs) else: raise BoxError(MISSING_PARSER_ERROR)