Skip to content

Feature request: provide literal: bool or quote_char: str as an attribute of tomlkit.items.String #443

@sirosen

Description

@sirosen

Feature Request

Provide an attribute of tomlkit parsed strings which lets code inspect whether it is single quoted (literal) or double quoted.

Use Case

I'm using tomlkit to implement some round-trip modifications of strings, and want to preserve quotes (related: #325).

I found I was able to get my desired behavior by checking as_string(), like so:

if old_value.as_string().startswith("'"):
    new_value = tomlkit.string(raw, literal=True)
else:
    new_value = tomlkit.string(raw, literal=False)

In #325 one of the comments mentioned inspecting _t which also works, but uses a non-public interface.

What I have is sufficient but it would be more discoverable and clearer if the literal property was exposed in some way by the tomlkit.items.String type.


Would a PR implementing this as a feature be worthwhile? I considered the possibility of trying to expose this via the trivia, but looking at the APIs around that, I don't see a clean way to define a specialized StringTrivia for this purpose. Simply adding a property or attribute which provides this information would be nice for consumers.

I'd also be happy to add a bit of doc (it would need a new page; "Examples"?) to demo how this can be used to preserve quote style when updating a value, in a separate PR.

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