adrienverge / yamllint

A linter for YAML files.
GNU General Public License v3.0
2.86k stars 273 forks source link

`syntax`: valid yaml `[\?]` raises syntax error #594

Closed silverwind closed 1 year ago

silverwind commented 1 year ago

[\?] is valid according to various other parsers, but this one rejects it:

1:3       error    syntax error: expected ',' or ']', but got '?' (syntax)

It should decode to JSON ["\\?"].

andrewimeson commented 1 year ago

PyYAML doesn't view it as valid

$ cat << 'EOF' | python3 -c 'import sys, yaml, json; y=yaml.safe_load(sys.stdin.read()); print(json.dumps(y))'
[\?]
EOF
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/__init__.py", line 125, in safe_load
    return load(stream, SafeLoader)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/__init__.py", line 81, in load
    return loader.get_single_data()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/constructor.py", line 49, in get_single_data
    node = self.get_single_node()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/composer.py", line 36, in get_single_node
    document = self.compose_document()
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/composer.py", line 55, in compose_document
    node = self.compose_node(None, None)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/composer.py", line 82, in compose_node
    node = self.compose_sequence_node(anchor)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/composer.py", line 110, in compose_sequence_node
    while not self.check_event(SequenceEndEvent):
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/parser.py", line 98, in check_event
    self.current_event = self.state()
                         ^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/parser.py", line 483, in parse_flow_sequence_entry
    raise ParserError("while parsing a flow sequence", self.marks[-1],
yaml.parser.ParserError: while parsing a flow sequence
  in "<unicode string>", line 1, column 1:
    [\?]
    ^
expected ',' or ']', but got '?'
  in "<unicode string>", line 1, column 3:
    [\?]
      ^

Nor does yq (Golang).

YAML is a superset of JSON, so the output you produced is valid JSON and YAML

This is equivalent to your JSON as well, and yamllint and other tools can parse it without issue.

- \?
silverwind commented 1 year ago

Hmm, it does work for me in pyyaml:

$ python3
Python 3.11.5 (main, Aug 24 2023, 15:18:16) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from yaml import load, CLoader
>>> load("[\?]", CLoader)
['\\?']

Edit: It works with CLoader but not with Loader. Looks like a bug in Loader.

silverwind commented 1 year ago

Opened https://github.com/yaml/pyyaml/issues/753

silverwind commented 1 year ago

Bug is not in this repo, so let's close it.