Skip to content

Commit d6f7feb

Browse files
serhiy-storchakaSeth Sims
authored and
Seth Sims
committed
bpo-41720: Add "return NotImplemented" in turtle.Vec2D.__rmul__(). (pythonGH-22092)
1 parent e8f8016 commit d6f7feb

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
@@ -130,6 +130,14 @@ def assertVectorsAlmostEqual(self, vec1, vec2):
130130
self.assertAlmostEqual(
131131
i, j, msg='values at index {} do not match'.format(idx))
132132

133+
class Multiplier:
134+
135+
def __mul__(self, other):
136+
return f'M*{other}'
137+
138+
def __rmul__(self, other):
139+
return f'{other}*M'
140+
133141

134142
class TestVec2D(VectorComparisonMixin, unittest.TestCase):
135143

@@ -211,9 +219,15 @@ def test_vector_multiply(self):
211219
self.assertAlmostEqual(answer, expected)
212220

213221
vec = Vec2D(0.5, 3)
214-
answer = vec * 10
215222
expected = Vec2D(5, 30)
216-
self.assertVectorsAlmostEqual(answer, expected)
223+
self.assertVectorsAlmostEqual(vec * 10, expected)
224+
self.assertVectorsAlmostEqual(10 * vec, expected)
225+
self.assertVectorsAlmostEqual(vec * 10.0, expected)
226+
self.assertVectorsAlmostEqual(10.0 * vec, expected)
227+
228+
M = Multiplier()
229+
self.assertEqual(vec * M, Vec2D(f"{vec[0]}*M", f"{vec[1]}*M"))
230+
self.assertEqual(M * vec, f'M*{vec}')
217231

218232
def test_vector_negative(self):
219233
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)