-
Notifications
You must be signed in to change notification settings - Fork 66
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
👌 Fail and emit warning on filters that do not return a boolean result #964
Conversation
5d90818
to
1335b7e
Compare
Thanks for the Bugfix 👍 The code itself looks good, but I'm not sure if the check gets performed on the right location. result = bool(eval(filter_string, filter_context))`
I think the intention of this fix is to check if the provided filter statement is returning a boolean value or something else. Feel also free to add yourself to the |
3aaa39d
to
4fe9a87
Compare
Hello @danwos |
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.
Thanks for the update, but we should change the logic for the check to be more bulletproof.
sphinx_needs/filter_common.py
Outdated
else: | ||
result = bool(eval(filter_string, filter_context)) | ||
result = eval(filter_string, filter_context) | ||
if isinstance(result, str): |
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.
It makes more sense to check here, if the result is not a boolean value, as this is the only allowed return type for a filter.
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 see that when we use the string filter like "search("AAA", title)" -> the result
type is re.Match type, this result is not a boolean but it also valid, so that If we only catch the result as a boolean, the filter with search()
will not work
I have convert result
to boolean when it is returned. We will cover all data type except a string
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.
Mhh then this is a bug for search()
. And luckily search()
returns None
, which gets transformed to False
, or it returns a re.Match object, which is always True
.
So we should fix our search()
implementation as well and I would stay with my opinion, that we should only allow Boolean returns values for eval()
.
Checking for string
only solves some specific problems, but is not a generic solution, which detects all faulty filter 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.
@danwos from your idea, I have implemented to catch search()
filter that transformed to True
if the result return a re.Match object otherwise we will check the data type as boolean
or not
57a0aa5
to
867dca7
Compare
sphinx_needs/filter_common.py
Outdated
else: | ||
result = bool(eval(filter_string, filter_context)) | ||
result = eval(filter_string, filter_context) | ||
if isinstance(result, re.Match): |
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 do this not here, but in the search function:
https://github.com/useblocks/sphinx-needs/blob/master/sphinx_needs/filter_common.py#L303
I would add something like this to filter_common.py
:
def need_search(*args, **kwargs)
return bool(re.search(*args, **kwargs))
... some other code
filter_context["search"] = need_search
Then the result of search()
gets transformed when used.
And we can save the check for re.Match here, as we should always get boolean-values back.
Sorry for making your PR a little more complex, but you found a new bug with it and for a generic solution for your problem we need to fix this as well.
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 have added the function as your mention to transform the result of search()
I also want a best solution, no problem or inconvenience for me
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.
Looks good, jsut add changelog line to the not released version.
Co-authored-by: Chris Sewell <chrisj_sewell@hotmail.com>
Co-authored-by: Chris Sewell <chrisj_sewell@hotmail.com>
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.
Thanks!
I added my change to version 1.4.0 |
@danwos Can this PR be merged? |
@danwos the pull request is up-to-date now |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #964 +/- ##
=======================================
Coverage 85.90% 85.90%
=======================================
Files 56 56
Lines 6511 6515 +4
=======================================
+ Hits 5593 5597 +4
Misses 918 918
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Co-authored-by: Chris Sewell <chrisj_sewell@hotmail.com>
for more information, see https://pre-commit.ci
Will fix the failing doc build, and I want to add a test, after #1128 😄 |
Ok this is ready to merge, but ... I have changed the title from a bug fix, to simply a change, since I don't think this fixes a bug, it simply changes sphinx-needs behaviour: @danwos can you just confirm you are happy with this breaking change, then happy to merge (but if you merge it, please do with a commit message, that mentions the breaking nature of the change 😄) |
No description provided.