pyyaml icon indicating copy to clipboard operation
pyyaml copied to clipboard

yaml.dump has unintuitive defaults

Open jonchu opened this issue 14 years ago • 7 comments

yaml.dump(dictionary) outputs a string for a dictionary.

yaml.dump(dictionary, default_flow_style=False) outputs a yaml string.

Seeing as this is a yaml library and all assumptions would point to outputting a yaml string as the default, these defaults seem kind of confusing and unintuitive.

jonchu avatar Sep 14 '12 03:09 jonchu

I agree with you. I'll probably start working on this over on sigmavirus24/pyyaml

sigmavirus24 avatar Dec 02 '13 02:12 sigmavirus24

Is this still an issue? @jonchu I'm missing an example. I can't see how pyyaml was behaving in 2012, but it could be that you were just seeing flow style YAML instead of block style.

perlpunk avatar Apr 01 '18 18:04 perlpunk

PyYAML has always dumped non-nested dicts in the flow style (looks like JSON or Python) by default:

>>> yaml.dump({'a': 1, 'b': 2})
'{a: 1, b: 2}\n'
>>> yaml.dump({'a': 1, 'b': 2}, default_flow_style=False)
'a: 1\nb: 2\n’

Never liked that default, but that’s the way it is.

I suspect you thought the default output was a python string, like this:

>>> {'a': 1, 'b': 2}
{'a': 1, 'b': 2}
>>> repr({'a': 1, 'b': 2})
"{'a': 1, 'b': 2}"

But notice how the a and b keys are quoted...

Closing this issue.

ingydotnet avatar Apr 01 '18 21:04 ingydotnet

Saying "I never liked the default, but that's the way it is" seems...not good. I'd expect a YAML library to output YAML that's easy for humans to read and avoids "flow style" and tags wherever possible. It's really quite annoying to continually have to fight the defaults for this library to get sane and normal YAML output.

robbieh avatar Apr 05 '18 00:04 robbieh

@robbieh I can't imagine that changing the default would not break a lot of stuff. I'm sure YAML dumps must be used in a lot of testing, and this has always been the default.

Can you think of a way that we can change this default responsibly?

ingydotnet avatar Apr 11 '18 16:04 ingydotnet

See #199

ingydotnet avatar Jul 03 '18 17:07 ingydotnet

It possible to reopen issues on github. Closing issue #1 just to create a new issue #199 with exactly the same problem, seems strange choice. Why do that?

Yes it's not possible to change it backwards compat, but it can (and should) be changed it in a major version release.

wimglenn avatar Sep 11 '18 03:09 wimglenn