Skip to content

Commit 4636463

Browse files
committed
Fix boolean comparison
1 parent 4a980e2 commit 4636463

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

tests/test_items.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from tomlkit import parse
1515
from tomlkit._compat import PY2
1616
from tomlkit.exceptions import NonExistentKey
17+
from tomlkit.items import Bool
1718
from tomlkit.items import InlineTable
1819
from tomlkit.items import Integer
1920
from tomlkit.items import Key
@@ -450,3 +451,22 @@ def test_deleting_inline_table_elemeent_does_not_leave_trailing_separator():
450451
table["baz"] = "boom"
451452

452453
assert '{baz = "boom"}' == table.as_string()
454+
455+
456+
def test_booleans_comparison():
457+
boolean = Bool(True, Trivia())
458+
459+
assert boolean
460+
461+
boolean = Bool(False, Trivia())
462+
463+
assert not boolean
464+
465+
s = """[foo]
466+
value = false
467+
"""
468+
469+
content = parse(s)
470+
471+
assert {"foo": {"value": False}} == content
472+
assert {"value": False} == content["foo"]

tomlkit/items.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ class Bool(Item):
496496
A boolean literal.
497497
"""
498498

499-
def __init__(self, t, trivia): # type: (float, Trivia) -> None
499+
def __init__(self, t, trivia): # type: (int, Trivia) -> None
500500
super(Bool, self).__init__(trivia)
501501

502502
self._value = bool(t)
@@ -515,6 +515,17 @@ def as_string(self): # type: () -> str
515515
def _getstate(self, protocol=3):
516516
return self._value, self._trivia
517517

518+
def __bool__(self):
519+
return self._value
520+
521+
__nonzero__ = __bool__
522+
523+
def __eq__(self, other):
524+
if not isinstance(other, bool):
525+
return NotImplemented
526+
527+
return other == self._value
528+
518529

519530
class DateTime(Item, datetime):
520531
"""

0 commit comments

Comments
 (0)