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):