Skip to content

Commit

Permalink
Merge pull request #68 from davidjb/truthiness
Browse files Browse the repository at this point in the history
Support Element truthiness on Python 3
  • Loading branch information
stchris committed Jul 1, 2022
2 parents bfa52f7 + b7e96f3 commit f0370dc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/test_untangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<a><b/><c/></a>")
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(
"""
Expand Down
4 changes: 3 additions & 1 deletion untangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f0370dc

Please sign in to comment.