Skip to content

Different behavior for SingleKey and DottedKey with single value #430

@deltamarnix

Description

@deltamarnix

I am building a tomlkit document and want to generate keys dynamically. Sometimes the lengths of my key specifications are only 1 long, but tomlkit still generates a DottedKey out of it. The documentation of tomlkit.key is very clear about it: specifying a list will return a DottedKey.

But I think it would be a nice addition if the function checks the length of the list before making that decision.

A minimal example of what goes wrong now:

import tomlkit

doc = tomlkit.document()
doc.append(tomlkit.key("my_key"), "value")
key_in_doc = "my_key" in doc # returns True

doc2 = tomlkit.document()
doc2.append(tomlkit.key(["my_key"]), "value")
key_in_doc2 = "my_key" in doc2 # returns False

assert key_in_doc == key_in_doc2 # unfortunately not

The in functionality doesn't seem to recognize the DottedKey as equal to a single str.

I was able to do a workaround in the code now by:

doc = tomlkit.document()
if len(key_spec) == 1:
    doc.append(tomlkit.key(key_spec[0]), "value")
else:
    doc.append(tomlkit.key(key_spec, "value")

Or in whatever way you want to implement it. At least, I really needed to unwrap the key_spec in order to make the in function work correctly.

Tested with tomlkit 0.13.3

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