[Python] Ruamel.yaml adds id001 aliases?

Discussão em 'Python' iniciado por Stack, Agosto 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using ruamel.yaml to add an entry to a dictionary:

    import ruamel.yaml
    yaml = ruamel.yaml.YAML()
    yaml.preserve_quotes = True
    yaml.indent(mapping=2, sequence=4, offset=2)

    raw = """---
    # Some config file..
    # This is the main clients config
    clients:
    foobar1:
    name: foo Bar 1
    auth: false
    count: 1290
    barboz:
    name: BarraBoz
    auth: true
    count: 19
    uids_default: &uids_default
    - attribute_email: mail
    datafile: vars1
    uids:
    - <<: *uids_default
    skip: true
    - <<: *uids_default
    issuer: http://foo.bar
    """

    vanhelst = {"adfs": 124423423, "123": "adsfadsfasdf", "name": "Dirk Van helst"}

    data = yaml.load(raw)
    data["clients"].insert(len(data["clients"]), "vanhelst", vanhelst)

    # Write to file
    with open("/tmp/out.yml", "w") as f:
    yaml.dump(data, f)


    That works, the entry is added correctly, the comment is preserved. But it also changes some anchors that are in the file, to add aliases (?) like id001:

    # Some config file..
    # This is the main clients config
    clients:
    foobar1:
    name: foo Bar 1
    auth: false
    count: 1290
    barboz:
    name: BarraBoz
    auth: true
    count: 19
    vanhelst:
    adfs: 124423423
    '123': adsfadsfasdf
    name: Dirk Van helst
    uids_default:
    - &id001
    attribute_email: mail
    datafile: vars1
    uids:
    - <<: *id001
    skip: true
    - <<: *id001
    issuer: http://foo.bar


    Is there a way to not add those, and not touch the anchors at all?

    Continue reading...

Compartilhe esta Página