Make NOTHING falsey (#732)

* Make NOTHING falsey

Fixes #720

* Add newsfragment

* Python 2
This commit is contained in:
Hynek Schlawack 2020-12-21 06:39:58 +01:00 committed by GitHub
parent 3d274d0bfa
commit f132c07e55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 0 deletions

View File

@ -0,0 +1 @@
``bool(attr.NOTHING)`` is now ``False``.

View File

@ -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()
"""

View File

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