strictyaml icon indicating copy to clipboard operation
strictyaml copied to clipboard

Incorrect indentation enforcement for dirty_load(flow_style=True)?

Open marhar opened this issue 4 years ago • 0 comments

By my understanding, these two specifications should be identical:

short: {x:1, y:2}
very_long_thing_that_cannot_be_short: {x:1, y:2}

and

short:                                 {x:1, y:2}
very_long_thing_that_cannot_be_short:  {x:1, y:2}

Example 1 produces the error:

     ... ong_thing_that_cannot_be_short: {x:1, y:2}
                                         ^ (line: 2) Found mapping with indentation inconsistent with previous mapping   in "<unicode string>", line 2, column 49:
     ... that_cannot_be_short: {x:1, y:2}
                                         ^ (line: 2)

Both produce identical output with my local build with this modification in parse.py:

           if node.start_mark.column != previous_indentation:
                # change raise to print, so as to make a warning.
                #raise exceptions.InconsistentIndentationDisallowed(
                print('Warning: '
                    "While parsing",
                    node.start_mark,
                    "Found mapping with indentation "
                    "inconsistent with previous mapping",
                    node.end_mark,
                )

So, two questions:

  1. Is my understanding correct? In this case, please consider this a bug report.
  2. If my understanding is incorrect, can this be a feature request?

In either case I'm happy to provide a PR with updated doc, story, etc.

Here is an example of my actual use case. It's useful for us to have a hierarchy which strictly follows indentation, with metadata as necessary mixed in as appropriate in flow style.

metadata: { x: 0, y: 1 }
a:
  metadata: { x: 1 }
  b:
    metadata: { x: 2 }
    c:
      - metadata: { x: 3 }
      - d

Here is my example test code:

import strictyaml

def test(text):
  try:
    tree = strictyaml.dirty_load(text, allow_flow_style=True)
  except Exception as e:
    print(e)
    return
  print(tree.data)

test( """\
short: {x:1, y:2}
very_long_thing_that_cannot_be_short: {x:1, y:2}
""")

test( """\
short:                                 {x:1, y:2}
very_long_thing_that_cannot_be_short:  {x:1, y:2}
""")

test( """\
metadata: { x: 0, y: 1 }
a:
  metadata: { x: 1 }
  b:
    metadata: { x: 2 }
    c:
      - metadata: { x: 3 }
      - d
""")

test( """\
metadata:
  x: 0
  y: 1
a:
  metadata:
    x: 1
  b:
    metadata:
     x: 2
    c:
      - metadata:
          x: 3
      - d
""")

output_with_patched_code_is_as_follows = """
warning:  While parsing   in "<unicode string>", line 2, column 39:
     ... ong_thing_that_cannot_be_short: {x:1, y:2}
                                         ^ (line: 2) Found mapping with indentation inconsistent with previous mapping   in "<unicode string>", line 2, column 49:
     ... that_cannot_be_short: {x:1, y:2}
                                         ^ (line: 2)
{'short': {'x:1': '', 'y:2': ''}, 'very_long_thing_that_cannot_be_short': {'x:1': '', 'y:2': ''}}
{'short': {'x:1': '', 'y:2': ''}, 'very_long_thing_that_cannot_be_short': {'x:1': '', 'y:2': ''}}

warning:  While parsing   in "<unicode string>", line 5, column 5:
        metadata: { x: 2 }
        ^ (line: 5) Found mapping with indentation inconsistent with previous mapping   in "<unicode string>", line 9, column 1:

    ^ (line: 9)
warning:  While parsing   in "<unicode string>", line 3, column 3:
      metadata: { x: 1 }
      ^ (line: 3) Found mapping with indentation inconsistent with previous mapping   in "<unicode string>", line 9, column 1:

    ^ (line: 9)
{'metadata': {'x': '0', 'y': '1'}, 'a': {'metadata': {'x': '1'}, 'b': {'metadata': {'x': '2'}, 'c': [{'metadata': {'x': '3'}}, 'd']}}}
{'metadata': {'x': '0', 'y': '1'}, 'a': {'metadata': {'x': '1'}, 'b': {'metadata': {'x': '2'}, 'c': [{'metadata': {'x': '3'}}, 'd']}}}
"""

marhar avatar Apr 02 '21 21:04 marhar