Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set a comment for a boolean item #390

Closed
abulgher opened this issue Dec 27, 2024 · 3 comments · Fixed by #397
Closed

How to set a comment for a boolean item #390

abulgher opened this issue Dec 27, 2024 · 3 comments · Fixed by #397

Comments

@abulgher
Copy link

I am facing an issue with boolean items and their comments.

Here is a minimalist piece of code to reproduce the issue:

from tomlkit import document, table, comment

doc = document()

p_table = table()
p_table.comment('a table level comment')
p_table['variable1'] = 5
p_table['variable1'].comment('variable1 is integer')
p_table['bool_variable'] = True
p_table['bool_variable'].comment('bool_variable is bool ')  # it fails here!!!
doc.add(p_table, 'proc')

when I try to add a comment to the boolean variable, an exception is raised complaining that bool type has no comment attribute.

Reading this other issue, it looks like this might be the expected behavior and not a bug as I was suspecting.

Is this correct? Is this happening only for boolean fields, or other types might be affected? I could not find anything in the documentation.
Is there a workaround to assign a comment to a boolean field?

Thanks

@frostming
Copy link
Contributor

Boolean values are special because it is highly common to do this:

if p_table['bool_value'] is True:
    ...

if we return a wrapper object, this check will fail.

@h1kken
Copy link

h1kken commented Jan 24, 2025

p_table['bool_variable'] = 'True'
p_table['bool_variable'].comment('bool_variable is bool')
p_table['bool_variable'] = True

@frostming
Copy link
Contributor

p_table['bool_variable'] = 'True'
p_table['bool_variable'].comment('bool_variable is bool')
p_table['bool_variable'] = True

This is weird, it can be p_table.value.item("bool_variable"].comment("bool_variable is bool")

@frostming frostming linked a pull request Jan 26, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants