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

Comment not parsed when value is boolean #119

Open
lmmx opened this issue Apr 25, 2021 · 4 comments
Open

Comment not parsed when value is boolean #119

lmmx opened this issue Apr 25, 2021 · 4 comments

Comments

@lmmx
Copy link

lmmx commented Apr 25, 2021

I've just tried to set up comment parsing in a TOML config file to see if I could use it for documentation, and it seems like it'll work except for when the configured value is a boolean.

When I drop into a debugger and look at the attributes of the parsed value:

(Pdb) p default_toml[section]
{'CONVERT_TO_PDF': False}
(Pdb) x = default_toml[section]
(Pdb) y = x["CONVERT_TO_PDF"]

and then tab complete to show the attributes, the dict x has a trivia attribute but the value y does not:

(Pdb) p y.
y.__abs__            y.__floor__          y.__invert__         y.__radd__           y.__rpow__           y.__trunc__
y.__add__            y.__floordiv__       y.__le__             y.__rand__           y.__rrshift__        y.__xor__
y.__and__            y.__format__         y.__lshift__         y.__rdivmod__        y.__rshift__         y.as_integer_ratio
y.__bool__           y.__ge__             y.__lt__             y.__reduce__         y.__rsub__           y.bit_length
y.__ceil__           y.__getattribute__   y.__mod__            y.__reduce_ex__      y.__rtruediv__       y.conjugate
y.__class__          y.__getnewargs__     y.__mul__            y.__repr__           y.__rxor__           y.denominator
y.__delattr__        y.__gt__             y.__ne__             y.__rfloordiv__      y.__setattr__        y.from_bytes
y.__dir__            y.__hash__           y.__neg__            y.__rlshift__        y.__sizeof__         y.imag
y.__divmod__         y.__index__          y.__new__            y.__rmod__           y.__str__            y.numerator
y.__doc__            y.__init__           y.__or__             y.__rmul__           y.__sub__            y.real
y.__eq__             y.__init_subclass__  y.__pos__            y.__ror__            y.__subclasshook__   y.to_bytes
y.__float__          y.__int__            y.__pow__            y.__round__          y.__truediv__
  • I checked in x.trivia.comment but there's nothing there
  • For other types of value (integer, float and string) comments are expected to be in the trivia.comment attribute

Is this a bug, or should I be looking for the comment in another place?

To minimally reproduce:

import tomlkit
d = tomlkit.loads("""
[foo]
bar = 1 # hello
baz = false # goodbye
""")
v1 = d["foo"]["bar"]
v2 = d["foo"]["baz"]
print(hasattr(v1, "trivia"))
print(getattr(v1.trivia, "comment"))
print(hasattr(v2, "trivia"))

True
# hello
False
@frostming
Copy link
Contributor

Is this a bug, or should I be looking for the comment in another place?

Yes, the boolean value is a real bool while others are tomlkit wrapped objects. This is intended as users may compare the value with boolean singletons with is

@zmievsa
Copy link

zmievsa commented Oct 31, 2021

@frostming so is there any other way of getting the comment for it?

@frostming
Copy link
Contributor

frostming commented Nov 1, 2021

@Ovsyanka83

v2 = d['foo'].value.item('baz')
print(v2.trivia.comment)

@zmievsa
Copy link

zmievsa commented Nov 1, 2021

@frostming thanks for the quick reply! Spent an hour trying to figure it out on my own but gave up. I think, the problem is the lack of documentation on the topic. I.e. ".value" was never mentioned in the README.md, and I haven't found any other documentation for tomlkit. I think, there should be a small section on manually extracting all information from tables that mentions how "value" actually has the "item" method.

Also, there's the issue of Bool being not json-serializable.

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

No branches or pull requests

3 participants