Skip to content

Plain equal sign (=) as node content results in an error #89

@rdn32

Description

@rdn32

Running this code:

import yaml
print(yaml.load("="))

Gives this error:
yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:value' in "<string>", line 1, column 1: = ^

There was, in the past, a ticket against PyYAML; but I suspect it got lost in a migration. See https://web.archive.org/web/20100707124750/http://pyyaml.org:80/ticket/140

The SnakeYAML people had a discussion of correct behaviour in this sort of situation at https://code.google.com/archive/p/snakeyaml/issues/192, the conclusion of which was (loosely paraphrased) "an equals sign is unambiguously a string value, and so doesn't need quoting, provided that you ignore the Value Key Language-Independent Type facility which never really worked anyway."

The equivalent fix to the one in SnakeYAML would be to remove the following lines from resolver.py:

Resolver.add_implicit_resolver(
        u'tag:yaml.org,2002:value',
        re.compile(ur'^(?:=)$'),
        [u'='])

I've also found it is possible to work around the issue with the following:

def construct_value(load, node):
    if not isinstance(node, yaml.ScalarNode):
        raise yaml.constructor.ConstructorError(
            "while constructing a value",
            node.start_mark,
            "expected a scalar, but found %s" % node.id, node.start_mark
        )
    yield str(node.value)

yaml.Loader.add_constructor(u'tag:yaml.org,2002:value', construct_value)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions