Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: amend IsFalseLike docs, logic, test
Browse files Browse the repository at this point in the history
osintalex committed Mar 28, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 0a1334b commit f287e1c
Showing 2 changed files with 14 additions and 13 deletions.
20 changes: 10 additions & 10 deletions dirty_equals/_boolean.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,8 @@

class IsFalseLike(DirtyEquals[B]):
"""
Check if the value is False like, and matches the given conditions.
Check if the value is False like. `IsFalseLike` allows comparison to anything and effectively uses
`return not bool(other)` (with string checks if `allow_strings=True` is set).
"""

def __init__(
@@ -16,34 +17,33 @@ def __init__(
allow_strings: Optional[bool] = None,
):
"""
Args:
allow_strings: if `True`, allow comparisons to False like strings
Example of basic usage:
```py title="IsFalseLike"
from dirty_equals import IsFalseLike
assert 'false' == IsFalseLike(allow_strings=True)
assert 'foobar' != IsFalseLike(allow_strings=True)
assert False == IsFalseLike
assert 0 == IsFalseLike
assert '0' == IsFalseLike(allow_strings=True)
assert 'True' != IsFalseLike(allow_strings=True)
assert [1] != IsFalseLike
assert {} == IsFalseLike
assert None == IsFalseLike
assert 'false' == IsFalseLike(allow_strings=True)
```
"""
self.allow_strings: Optional[bool] = allow_strings
super().__init__(
allow_strings=allow_strings,
)
super().__init__(allow_strings=allow_strings)

def equals(self, other: Any) -> bool:
if self.allow_strings:
if isinstance(other, str) and self.allow_strings:
return self.make_string_check(other)
return not bool(other)

@staticmethod
def make_string_check(other: str) -> bool:
if other.lower() in ('0', '0.0', 'false'):
return True
else:
return False
return other.lower() in {'0', '0.0', 'false'}
7 changes: 4 additions & 3 deletions tests/test_boolean.py
Original file line number Diff line number Diff line change
@@ -34,9 +34,10 @@ class TestIsFalseLike:
def test_dirty_equals(self, other, expected):
assert other == expected

def test_dirty_not_equals(self, other, expected):
with pytest.raises(AssertionError):
assert other != expected

def test_dirty_not_equals():
with pytest.raises(AssertionError):
assert 0 != IsFalseLike


def test_invalid_initialization():

0 comments on commit f287e1c

Please sign in to comment.