Skip to content

Commit

Permalink
fix: Remove float() coercion in Python loops (#6259)
Browse files Browse the repository at this point in the history
Coercion to a float is a very JavaScript thing to do, Python is not JavaScript.

From this discussion:
https://groups.google.com/g/blockly/c/OF_j9rKQgpI
  • Loading branch information
NeilFraser committed Jul 6, 2022
1 parent aaafbc2 commit 5612e13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
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

0 comments on commit 5612e13

Please sign in to comment.