Skip to content

Commit 1671b0e

Browse files
bpo-41720: Add "return NotImplemented" in turtle.Vec2D.__rmul__(). (GH-22092)
(cherry picked from commit fd4cafd) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent c73ee5a commit 1671b0e

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Lib/test/test_turtle.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ def assertVectorsAlmostEqual(self, vec1, vec2):
127127
self.assertAlmostEqual(
128128
i, j, msg='values at index {} do not match'.format(idx))
129129

130+
class Multiplier:
131+
132+
def __mul__(self, other):
133+
return f'M*{other}'
134+
135+
def __rmul__(self, other):
136+
return f'{other}*M'
137+
130138

131139
class TestVec2D(VectorComparisonMixin, unittest.TestCase):
132140

@@ -208,9 +216,15 @@ def test_vector_multiply(self):
208216
self.assertAlmostEqual(answer, expected)
209217

210218
vec = Vec2D(0.5, 3)
211-
answer = vec * 10
212219
expected = Vec2D(5, 30)
213-
self.assertVectorsAlmostEqual(answer, expected)
220+
self.assertVectorsAlmostEqual(vec * 10, expected)
221+
self.assertVectorsAlmostEqual(10 * vec, expected)
222+
self.assertVectorsAlmostEqual(vec * 10.0, expected)
223+
self.assertVectorsAlmostEqual(10.0 * vec, expected)
224+
225+
M = Multiplier()
226+
self.assertEqual(vec * M, Vec2D(f"{vec[0]}*M", f"{vec[1]}*M"))
227+
self.assertEqual(M * vec, f'M*{vec}')
214228

215229
def test_vector_negative(self):
216230
vec = Vec2D(10, -10)

Lib/turtle.py

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ def __mul__(self, other):
258258
def __rmul__(self, other):
259259
if isinstance(other, int) or isinstance(other, float):
260260
return Vec2D(self[0]*other, self[1]*other)
261+
return NotImplemented
261262
def __sub__(self, other):
262263
return Vec2D(self[0]-other[0], self[1]-other[1])
263264
def __neg__(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed :meth:`turtle.Vec2D.__rmul__` for arguments which are not int or
2+
float.

0 commit comments

Comments
 (0)