diff --git a/CHANGELOG.md b/CHANGELOG.md index f89fdc8..af1aae6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Unreleased - dropped support for Python 2.6, 3.3 - fixed support for Python 3.6 ([#57](https://github.com/stchris/untangle/pull/57)) - formatted code with black +- support Element truthiness on Python 3 ([#68](https://github.com/stchris/untangle/pull/68/)) - `main` is now the default branch - switch to Github Actions - switch to poetry and pytest diff --git a/tests/test_untangle.py b/tests/test_untangle.py index 69ef820..8d279d7 100755 --- a/tests/test_untangle.py +++ b/tests/test_untangle.py @@ -31,6 +31,13 @@ def test_basic_with_decl(self): self.assertTrue("c" in o.a) self.assertTrue("d" not in o.a) + def test_truthiness(self): + o = untangle.parse("") + self.assertTrue(o) + self.assertTrue(o.a) + self.assertTrue(o.a.b) + self.assertTrue(o.a.c) + def test_with_attributes(self): o = untangle.parse( """ diff --git a/untangle.py b/untangle.py index a4a85fe..c608b32 100755 --- a/untangle.py +++ b/untangle.py @@ -115,9 +115,11 @@ def __repr__(self): self.cdata, ) - def __nonzero__(self): + def __bool__(self): return self.is_root or self._name is not None + __nonzero__ = __bool__ + def __eq__(self, val): return self.cdata == val