Skip to content

Handle top level yaml property on #696

Open
@wsams

Description

@wsams

A document containing a top-level key named on is rendered as a boolean value instead of the literal string on when loading and dumping a document.

If a top level yaml property matches the regex for the resolver,

Resolver.add_implicit_resolver(
        'tag:yaml.org,2002:bool',
        re.compile(r'''^(?:yes|Yes|YES|no|No|NO
                    |true|True|TRUE|false|False|FALSE
                    |on|On|ON|off|Off|OFF)$''', re.X),
        list('yYnNtTfFoO'))

the boolean value after compiling is rendered in yaml output literally. So not just on, but words like off and no as well.

For example,

import yaml

tmpl = """on:
  push:
    branches: [ main ]
"""

print(yaml.dump(yaml.load(tmpl, Loader=yaml.Loader)))

will output,

true:
  push:
    branches:
    - main

I would expect,

on:
  push:
    branches:
    - main

but the word on is replaced with a boolean value true.

I'm using pyyaml==6.0 with Python 3.10.9.

Update: It appears any key that is a boolean value will be parsed as a boolean. For example,

no:
  push:
    branches:
    - main

also renders as,

false:
  push:
    branches:
    - main

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