diff --git a/dirty_equals/_boolean.py b/dirty_equals/_boolean.py index f3010b8..21f90be 100644 --- a/dirty_equals/_boolean.py +++ b/dirty_equals/_boolean.py @@ -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,11 +17,16 @@ 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) @@ -28,22 +34,16 @@ def __init__( 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'} diff --git a/tests/test_boolean.py b/tests/test_boolean.py index 65f863b..97f0bb1 100644 --- a/tests/test_boolean.py +++ b/tests/test_boolean.py @@ -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():