-
-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add IsFalseLike #23
Conversation
Codecov Report
@@ Coverage Diff @@
## main #23 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 9 10 +1
Lines 584 599 +15
Branches 153 155 +2
=========================================
+ Hits 584 599 +15
Continue to review full report at Codecov.
|
dirty_equals/_boolean.py
Outdated
Check if the value is False like, and matches the given conditions. | ||
""" | ||
|
||
allowed_types: Union[Type[B], Tuple[Union[_SpecialForm, type], ...]] = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There might be a much simpler way to do this, I got quite confused working with _SpecialForm
and mypy here. This also requires a type ignore here.
Hey, just wondering if there's any interest on this? Happy to try and help with something else if that would be a better fit, I think it's a really cool project! |
Sorry, somehow I missed this. I'll look soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great, just a few things to fix.
We'll need to add IsTrueLike
in the same release, but I can do that.
dirty_equals/_boolean.py
Outdated
Check if the value is False like, and matches the given conditions. | ||
""" | ||
|
||
allowed_types: Union[Type[B], Tuple[Union[_SpecialForm, type], ...]] = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should allow all types to IsFalseLike
, with that:
- the logic will be simpler - effectively
return not bool(thing)
(except for the string logic) - it will be more pythonic
- it will be easier for users to understand - they don't have to remember (or look up) what types allowed - they can just use their intuition about python
dirty_equals/_boolean.py
Outdated
|
||
def __init__( | ||
self, | ||
numeric: Optional[bool] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not required:
assert bool(1) is True
assert bool(0) is False
Is already the case.
dirty_equals/_boolean.py
Outdated
def __init__( | ||
self, | ||
numeric: Optional[bool] = None, | ||
string: Optional[bool] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is good, but we should ignore case and give it a more descritive name.
assert 'false' == IsFalseLike(allow_strings=True)
also, I think you should make it a keyword only argument.
dirty_equals/_boolean.py
Outdated
if self.numeric and not isinstance(self.numeric, bool): | ||
raise ValueError('"numeric" requires a boolean argument') | ||
if self.string and not isinstance(self.string, bool): | ||
raise ValueError('"string" requires a boolean argument') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for explicit type checks like this, we don't do it elsewhere.
@@ -0,0 +1,3 @@ | |||
# Boolean Types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good.
|
||
|
||
@pytest.mark.parametrize( | ||
'other, expected', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good.
Thanks for the helpful feedback! All makes sense, about to push another commit now. |
I think those check fails are all about an error in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise looking good, main should now be fixed, so you can rebase to fix build
dirty_equals/_boolean.py
Outdated
super().__init__( | ||
allow_strings=allow_strings, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super().__init__( | |
allow_strings=allow_strings, | |
) | |
super().__init__(allow_strings=allow_strings) |
|
||
assert False == IsFalseLike | ||
assert 0 == IsFalseLike | ||
assert '0' == IsFalseLike(allow_strings=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put the assert 'false' == IsFalseLike(allow_strings=True)
case before these.
You should also add assert 'foobar' != IsFalseLike(allow_strings=True)
to show that other values don't raise an error.
allow_strings: Optional[bool] = None, | ||
): | ||
""" | ||
Example of basic usage: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to add Args
and document what values are allowed when allow_strings=True
.
dirty_equals/_boolean.py
Outdated
|
||
class IsFalseLike(DirtyEquals[B]): | ||
""" | ||
Check if the value is False like, and matches the given conditions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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). | |
Or similar |
dirty_equals/_boolean.py
Outdated
if other.lower() in ('0', '0.0', 'false'): | ||
return True | ||
else: | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use just:
if other.lower() in ('0', '0.0', 'false'): | |
return True | |
else: | |
return False | |
return other.lower() in {'0', '0.0', 'false'} |
in
checks are almost always faster with sets than tuples or lists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! Did not know that re sets.
dirty_equals/_boolean.py
Outdated
) | ||
|
||
def equals(self, other: Any) -> bool: | ||
if self.allow_strings: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self.allow_strings: | |
if isinstance(other, str) and self.allow_strings: |
Otherwise you'd get an error here!
tests/test_boolean.py
Outdated
def test_dirty_not_equals(self, other, expected): | ||
with pytest.raises(AssertionError): | ||
assert other != expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't achieving anything, you can remove it and make this a simple function test.
thanks so much. For future reference, you've done something weird here when updating from main, which means my changes are showing in your PR, but shouldn't be a problem on this occation. |
Pleasure - thanks for the feedback. My bad on that, think I should have forced push but instead hit |
I thought this library looked pretty cool so wanted to give this a go.