"wrappers/python/vscode:/vscode.git/clone" did not exist on "68eb50bd3cbb669fef8ce8e65d976209297f5fa4"
Commit 6220775c authored by Andrea Rizzi's avatar Andrea Rizzi
Browse files

Fix #1940: Bug in Quantity.__setitem__ when assigning slices

parent 07c1b86c
...@@ -737,8 +737,8 @@ class Quantity(object): ...@@ -737,8 +737,8 @@ class Quantity(object):
# Delegate slices to one-at-a time ___setitem___ # Delegate slices to one-at-a time ___setitem___
if isinstance(key, slice): # slice if isinstance(key, slice): # slice
indices = key.indices(len(self)) indices = key.indices(len(self))
for i in range(*indices): for value_idx, self_idx in enumerate(range(*indices)):
self[i] = value[i] self[self_idx] = value[value_idx]
else: # single index else: # single index
# Check unit compatibility # Check unit compatibility
if self.unit.is_dimensionless() and is_dimensionless(value): if self.unit.is_dimensionless() and is_dimensionless(value):
......
...@@ -288,6 +288,10 @@ class TestUnits(QuantityTestCase): ...@@ -288,6 +288,10 @@ class TestUnits(QuantityTestCase):
# Tests that __setitem__ converts to the unit of the container # Tests that __setitem__ converts to the unit of the container
s[0] = 1 * u.nanometers s[0] = 1 * u.nanometers
self.assertEqual(s[0]._value, 10) self.assertEqual(s[0]._value, 10)
# Tests that __setitem__ handles slice assignment correctly
x = [0, 1, 2, 3, 4] * u.kelvin
x[2:4] = [-2, -3] * u.kelvin
self.assertEqual(x._value, [0, 1, -2, -3, 4])
# Tests standard unit conversions # Tests standard unit conversions
x = [1, 2, 3] * u.centimeters x = [1, 2, 3] * u.centimeters
self.assertEqual(x / u.millimeters, [10, 20, 30]) self.assertEqual(x / u.millimeters, [10, 20, 30])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment