-
Notifications
You must be signed in to change notification settings - Fork 233
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
Index Error Fix #811
Index Error Fix #811
Conversation
…tched from mongo database due to pymongo check of indexes being 1,-1 or 2d
Hi @gaurav25122000 |
beanie/odm/fields.py
Outdated
fields | ||
and isinstance(fields[0][1], float) | ||
and fields[0][1] == 1.0 | ||
or fields[0][1] == -1.0 |
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 expression is equivalent (see operator precedence) with
(fields and isinstance(fields[0][1], float) and fields[0][1] == 1.0) or fields[0][1] == -1.0
So, when fields
is evaluated as False
, then fields[0]
will raise an IndexError
(or other error, if it is not a list) and that may not be what we want.
Note: Even if
and
andor
would have the same precedence, the same situation could be possible. That is why it is more readable to use parenthesis when different logical operators are present in an expression.
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 called short circuiting and when fields is evaluated as False, it won't go ahead to check the further 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.
The and
sequence is not evaluated completely because of short circuiting, but the left side of or
yes.
Try this out:
0 and 1 and 2 or 3
The expression can be parenthesis-ed as (0 and 1 and 2) or 3
This conditional expression introduce a new bug.
If you would write test for the case when bool(fields)
is Ḟalse˙, it would crash probably with IndexError
.
To help other potential contributors to understand the code and the change, it would be nice to give a context in the pull request description or a reference to the issue where the context is given. About the commit message:
According to git conventions, the subject should be in
This is already in These could cost some effort for the contributor, but would save time for others and speed up the development of the project. |
Can't find the test cases for this particular function |
This is true. Could you please add one? @gaurav25122000 |
This PR is stale because it has been open 45 days with no activity. |
This PR was closed because it has been stalled for 14 days with no activity. |
I am facing this same issue. I moved a lot of code from my custom wrapper to beanie only to find out I am not able to use it because of this issue! |
I can add a test quick if @gaurav25122000 is not working on this one? cc: @roman-right |
Added conversion for float to int for existing indexes while being fetched from mongo database due to pymongo check of indexes being 1,-1 or 2d