Pyyaml: Change dump(..., default_flow_style=...) default to False

Created on 3 Jul 2018  路  4Comments  路  Source: yaml/pyyaml

This is probably the most annoying default in PyYAML. It's the only PyYAML FAQ
entry: https://pyyaml.org/wiki/PyYAMLDocumentation#frequently-asked-questions

Changing the default to always prefer indented/block style would be good, but
is not back compatible. Since we will be breaking backwards compat big time
with the upcoming safe-load-by-default changes, this change should probably be
timed with that one.

Most helpful comment

Hmm. It looks like there are actually three flow styles: True, False, and None:

>>> a = dict(a=dict(b=dict(a = ['a','b']), foo=45), c=dict(z=[1,2,3],yy=[4,5,6,7],zozo='Hey!'))
>>> print yaml.safe_dump(a)
a:
  b:
    a: [a, b]
  foo: 45
c:
  yy: [4, 5, 6, 7]
  z: [1, 2, 3]
  zozo: Hey!

>>> print yaml.safe_dump(a, default_flow_style=False)
a:
  b:
    a:
    - a
    - b
  foo: 45
c:
  yy:
  - 4
  - 5
  - 6
  - 7
  z:
  - 1
  - 2
  - 3
  zozo: Hey!

>>> print yaml.safe_dump(a, default_flow_style=True)
{a: {b: {a: [a, b]}, foo: 45}, c: {yy: [4, 5, 6, 7], z: [1, 2, 3], zozo: Hey!}}

This isn't clear from the documentation: there is the FAQ which mentions use of default_flow_style=False, and later on it mentions:

The flow_style flag indicates if a collection is block or flow. The possible values are None, True, False.

But there's no mention of what None, True, and False do. (flow = True, block = False, None = library picks whichever is better, right? I'm not sure.)

All 4 comments

Hmm. It looks like there are actually three flow styles: True, False, and None:

>>> a = dict(a=dict(b=dict(a = ['a','b']), foo=45), c=dict(z=[1,2,3],yy=[4,5,6,7],zozo='Hey!'))
>>> print yaml.safe_dump(a)
a:
  b:
    a: [a, b]
  foo: 45
c:
  yy: [4, 5, 6, 7]
  z: [1, 2, 3]
  zozo: Hey!

>>> print yaml.safe_dump(a, default_flow_style=False)
a:
  b:
    a:
    - a
    - b
  foo: 45
c:
  yy:
  - 4
  - 5
  - 6
  - 7
  z:
  - 1
  - 2
  - 3
  zozo: Hey!

>>> print yaml.safe_dump(a, default_flow_style=True)
{a: {b: {a: [a, b]}, foo: 45}, c: {yy: [4, 5, 6, 7], z: [1, 2, 3], zozo: Hey!}}

This isn't clear from the documentation: there is the FAQ which mentions use of default_flow_style=False, and later on it mentions:

The flow_style flag indicates if a collection is block or flow. The possible values are None, True, False.

But there's no mention of what None, True, and False do. (flow = True, block = False, None = library picks whichever is better, right? I'm not sure.)

yep, fixed by #256

None != False
I like the default_flow_style=None formatting the best.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

perrygeo picture perrygeo  路  8Comments

brianbruggeman picture brianbruggeman  路  9Comments

graingert picture graingert  路  7Comments

lucj picture lucj  路  7Comments

jeoffridavis picture jeoffridavis  路  4Comments