Skip to content

Commit

Permalink
tweak IsFalseLike repr, fix empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Mar 29, 2022
1 parent 634765a commit 2616625
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
45 changes: 22 additions & 23 deletions dirty_equals/_boolean.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
from typing import Any, Optional
from typing import Any

from ._base import DirtyEquals
from ._utils import Omit


class IsTrueLike(DirtyEquals[bool]):
"""
Check if the value is True like. `IsTrueLike` allows comparison to anything and effectively uses just
`return bool(other)`.
"""
def __init__(self) -> None:
"""
Example of basic usage:
Example of basic usage:
```py title="IsTrueLike"
from dirty_equals import IsTrueLike
assert True == IsTrueLike
assert 1 == IsTrueLike
assert 'true' == IsTrueLike
assert 'foobar' == IsTrueLike # any non-empty string is "True"
assert '' != IsTrueLike
assert [1] == IsTrueLike
assert {} != IsTrueLike
assert None != IsTrueLike
```
"""
super().__init__()
```py title="IsTrueLike"
from dirty_equals import IsTrueLike
assert True == IsTrueLike
assert 1 == IsTrueLike
assert 'true' == IsTrueLike
assert 'foobar' == IsTrueLike # any non-empty string is "True"
assert '' != IsTrueLike
assert [1] == IsTrueLike
assert {} != IsTrueLike
assert None != IsTrueLike
```
"""

def equals(self, other: Any) -> bool:
return bool(other)
Expand All @@ -42,7 +39,7 @@ def __init__(self, *, allow_strings: bool = False):
"""
Args:
allow_strings: if `True`, allow comparisons to `False` like strings, case-insensitive, allows
`'false'` and any string where `float(other) == 0` (e.g. `'0'`).
`''`, `'false'` and any string where `float(other) == 0` (e.g. `'0'`).
Example of basic usage:
Expand All @@ -59,10 +56,12 @@ def __init__(self, *, allow_strings: bool = False):
assert [1] != IsFalseLike
assert {} == IsFalseLike
assert None == IsFalseLike
assert '' == IsFalseLike(allow_strings=True)
assert '' == IsFalseLike
```
"""
self.allow_strings: Optional[bool] = allow_strings
super().__init__(allow_strings=allow_strings)
self.allow_strings = allow_strings
super().__init__(allow_strings=allow_strings or Omit)

def equals(self, other: Any) -> bool:
if isinstance(other, str) and self.allow_strings:
Expand All @@ -71,7 +70,7 @@ def equals(self, other: Any) -> bool:

@staticmethod
def make_string_check(other: str) -> bool:
if other.lower() == 'false':
if other.lower() in {'false', ''}:
return True

try:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
([], IsFalseLike),
([1], ~IsFalseLike),
((), IsFalseLike),
('', IsFalseLike),
('', IsFalseLike(allow_strings=True)),
((1, 2), ~IsFalseLike),
({}, IsFalseLike),
({1: 'a'}, ~IsFalseLike),
Expand All @@ -35,6 +37,12 @@ def test_is_false_like(other, expected):
assert other == expected


def test_is_false_like_repr():
assert repr(IsFalseLike) == 'IsFalseLike'
assert repr(IsFalseLike()) == 'IsFalseLike()'
assert repr(IsFalseLike(allow_strings=True)) == 'IsFalseLike(allow_strings=True)'


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

0 comments on commit 2616625

Please sign in to comment.