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: Remove float() coercion in Python loops #6259

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions generators/python/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,11 @@ def ${Python.FUNCTION_NAME_PLACEHOLDER_}(start, stop, step):
if (stringUtils.isNumber(arg)) {
// Simple number.
arg = Number(arg);
} else if (arg.match(/^\w+$/)) {
// Variable.
arg = 'float(' + arg + ')';
} else {
// It's complicated.
} else if (!arg.match(/^\w+$/)) {
// Not a variable, it's complicated.
const varName = Python.nameDB_.getDistinctName(
variable0 + suffix, NameType.VARIABLE);
code += varName + ' = float(' + arg + ')\n';
code += varName + ' = ' + arg + '\n';
arg = varName;
}
return arg;
Expand Down
24 changes: 12 additions & 12 deletions tests/generators/golden/generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,22 @@ def test_count_by():
loglist.append(x)
assertEquals(loglist, [1, 2.5, 4, 5.5, 7], 'count with floats')
loglist = []
x_start = float(1 + 0)
x_end = float(8 + 0)
x_inc = float(1 - 2)
x_start = 1 + 0
x_end = 8 + 0
x_inc = 1 - 2
for x in (x_start <= x_end) and upRange(x_start, x_end, x_inc) or downRange(x_start, x_end, x_inc):
loglist.append(x)
assertEquals(loglist, [1, 2, 3, 4, 5, 6, 7, 8], 'count up non-trivial ints')
loglist = []
x_start2 = float(8 + 0)
x_end2 = float(1 + 0)
x_start2 = 8 + 0
x_end2 = 1 + 0
for x in (x_start2 <= x_end2) and upRange(x_start2, x_end2, 2) or downRange(x_start2, x_end2, 2):
loglist.append(x)
assertEquals(loglist, [8, 6, 4, 2], 'count down non-trivial ints')
loglist = []
x_start3 = float(5 + 0.5)
x_end3 = float(1 + 0)
x_inc2 = float(1 + 0)
x_start3 = 5 + 0.5
x_end3 = 1 + 0
x_inc2 = 1 + 0
for x in (x_start3 <= x_end3) and upRange(x_start3, x_end3, x_inc2) or downRange(x_start3, x_end3, x_inc2):
loglist.append(x)
assertEquals(loglist, [5.5, 4.5, 3.5, 2.5, 1.5], 'count with floats')
Expand All @@ -255,14 +255,14 @@ def test_count_loops():
log = str(log) + str(x)
assertEquals(log, '87654321', 'count down')
loglist = []
x_start4 = float(1 + 0)
x_end4 = float(4 + 0)
x_start4 = 1 + 0
x_end4 = 4 + 0
for x in (x_start4 <= x_end4) and upRange(x_start4, x_end4, 1) or downRange(x_start4, x_end4, 1):
loglist.append(x)
assertEquals(loglist, [1, 2, 3, 4], 'count up non-trivial')
loglist = []
x_start5 = float(3 + 1)
x_end5 = float(1 + 0)
x_start5 = 3 + 1
x_end5 = 1 + 0
for x in (x_start5 <= x_end5) and upRange(x_start5, x_end5, 1) or downRange(x_start5, x_end5, 1):
loglist.append(x)
assertEquals(loglist, [4, 3, 2, 1], 'count down non-trivial')
Expand Down