Skip to content
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

Fix K.match() on regex filter #41

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

XavierBrassoud
Copy link

@XavierBrassoud XavierBrassoud commented Sep 24, 2021

Issue

Prevent the regex filter from returning an object instead of a boolean
re.match return an object if match, None else

Demonstration

In this case, K.match() return a re.match() object instead of a boolean:

assert K(['=~', 'f', '^hi$']).match({'f': 'hi'}) == True # Fail.

Tests also broken. False positive here, object is not None so you got True:

def test_basic_regex_true(self):
    self.assertTrue(K(['=~', 'f', '^hi$']).match({'f': 'hi'})) # Success => False positive!
    self.assertIs(K(['=~', 'f', '^hi$']).match({'f': 'hi'}), True) # Fail as expected before the fix

This fix include a little class helper that shortcuts self.assertIs(exp, true) to self.assertIsTrue(exp) following your conventions. Tests are also updated with assertions helpers that follows Python AssertIs recommendations for booleans strict checking.

Fix

Changed this line:

'=~': lambda match_str, regex: regex.match(match_str) if match_str is not None else False,

By this line:

'=~': lambda match_str, regex: regex.match(match_str) is not None if match_str is not None else False,

is not None explicit convert re.match() to boolean as K.match() should work.

Prevent the regex filter from returning an object instead of a boolean
[re.match][1] return an object if match, None else

[1]: https://docs.python.org/3/library/re.html#re.match
Units tests previously return a false positive results for regex filters
In the case of the regex filter, the tests checked for the existence or not of the values, K.match() returned an object instead of a boolean
Prevent the regex filter from returning an object instead of a boolean
[re.match][1] return an object if match, None else

[1]: https://docs.python.org/3/library/re.html#re.match
@somewes
Copy link
Contributor

somewes commented Apr 7, 2022

thanks for this pr @XavierBrassoud sorry i didn't see it sooner. i can circle back around to this and get it merged in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants