summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHynek Schlawack <hs@ox.cx>2020-12-21 06:39:58 +0100
committerGitHub <noreply@github.com>2020-12-21 06:39:58 +0100
commitf132c07e556dde35d5f7ef30b736800eb08e9f3b (patch)
tree336d4f286ecef7e05b45fddc17f09aaf4b2ab400
parent3d274d0bfab142bc45504533f2c2b5f5ce519fd7 (diff)
downloadattrs-f132c07e556dde35d5f7ef30b736800eb08e9f3b.tar.gz
Make NOTHING falsey (#732)
* Make NOTHING falsey Fixes #720 * Add newsfragment * Python 2
-rw-r--r--changelog.d/732.change.rst1
-rw-r--r--src/attr/_make.py6
-rw-r--r--tests/test_dunders.py7
3 files changed, 14 insertions, 0 deletions
diff --git a/changelog.d/732.change.rst b/changelog.d/732.change.rst
new file mode 100644
index 0000000..b1acc4b
--- /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 1ed202f..7992e1d 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 2f1ebab..52b72f7 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):