From f132c07e556dde35d5f7ef30b736800eb08e9f3b Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Mon, 21 Dec 2020 06:39:58 +0100 Subject: [PATCH] Make NOTHING falsey (#732) * Make NOTHING falsey Fixes #720 * Add newsfragment * Python 2 --- changelog.d/732.change.rst | 1 + src/attr/_make.py | 6 ++++++ tests/test_dunders.py | 7 +++++++ 3 files changed, 14 insertions(+) create mode 100644 changelog.d/732.change.rst diff --git a/changelog.d/732.change.rst b/changelog.d/732.change.rst new file mode 100644 index 00000000..b1acc4be --- /dev/null +++ b/changelog.d/732.change.rst @@ -0,0 +1 @@ +``bool(attr.NOTHING)`` is now ``False``. diff --git a/src/attr/_make.py b/src/attr/_make.py index 1ed202f7..7992e1dd 100644 --- a/src/attr/_make.py +++ b/src/attr/_make.py @@ -69,6 +69,12 @@ class _Nothing(object): def __repr__(self): return "NOTHING" + def __bool__(self): + return False + + def __len__(self): + return 0 # __bool__ for Python 2 + NOTHING = _Nothing() """ diff --git a/tests/test_dunders.py b/tests/test_dunders.py index 2f1ebabd..52b72f7d 100644 --- a/tests/test_dunders.py +++ b/tests/test_dunders.py @@ -808,6 +808,13 @@ class TestNothing(object): assert not (_Nothing() != _Nothing()) assert 1 != _Nothing() + def test_false(self): + """ + NOTHING evaluates as falsey. + """ + assert not NOTHING + assert False is bool(NOTHING) + @attr.s(hash=True, order=True) class C(object):