-
Notifications
You must be signed in to change notification settings - Fork 291
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
Wrong conversion of division to old_div #434
Comments
While mathematically equivalent, this is not equal on Python 2. If Though of course, that doesn't change the bug. |
I also thought so, which is why I used a Python 2.7.15 as well as Python 3.7.1 to run the example and validate that they show the same behavior. It is good to hear that my first intention was not completely wrong. Maybe it's also connected to the OS? I am running the example on Windows 7 x64. Can you give an example in which cases Python 2 would evaluate the expression in another way? |
>>> y = range(10)
>>> [x / 2 * 3.0 for x in y]
[0.0, 0.0, 3.0, 3.0, 6.0, 6.0, 9.0, 9.0, 12.0, 12.0]
>>> [x * (3.0 / 2) for x in y]
[0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5] (Your Expected conversion is of course the correct result.) |
Totally what I would have expected. However I explicitly tested using 1 for x and got 1.5 as a result. I also forgot to mention, that there is a different output of the code if you don't use the parentheses. In this case futurize will yield the correct conversion.
|
I encountered the same bug and am working on the fix on my branch.
Original output
New output
I'm going to create a pull request when I see time.. |
Here are more examples.
Contrary to what you said, I see
Original output
New output
|
PR created (#441) |
I have also observed that the 1.0/1024.0 becomes old_div(1.0,1024.0) I think this conversion is unnecessary since the division where both operands are floats produces the same result, both in Python 2 and Python 3. |
@mattgathu Which version of futurize and Python do you use? With Python 3.7.2 and futurize 0.17.1 (the latest in PyPI),
|
Here are my versions: $ python -V
Python 2.7.16
$ futurize --version
0.16.0 I have upgraded to 0.17.0 and it seems not to have the issue. Thanks |
Indeed it seems v0.17.0 includes an enhancement to skip unnecessary conversions. https://github.com/PythonCharmers/python-future/releases/tag/v0.17.0
|
I also have this problem, running futurize 0.17.1 on Python 2.7.16. This example illustrates the problem:
Note that |
Example code
The following code contains an old style division.
The expected result
The result running the above code with arbitrary values assigned to x should be the following, where the parentheses explicitly show the order of the operations
(x / 2) * 3.0
. Or written in another order the result should be evaluated asx * (3.0 / 2)
.The issue
Once we run
futurize
on the code above, anold_div
gets inserted by thefix_division_safe
fixture. However as one can see in the diff below, the function gets called with a wrong order of arguments.Expected conversion
As already stated in the section 'expected result' the correct conversion should have been:
(old_div(x, 2) * 3.0)
The text was updated successfully, but these errors were encountered: