Skip to content

[3.9] bpo-41720: Add "return NotImplemented" in turtle.Vec2D.__rmul__(). (GH-22092) #22137

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

Merged
merged 1 commit into from
Sep 7, 2020
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
18 changes: 16 additions & 2 deletions Lib/test/test_turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ def assertVectorsAlmostEqual(self, vec1, vec2):
self.assertAlmostEqual(
i, j, msg='values at index {} do not match'.format(idx))

class Multiplier:

def __mul__(self, other):
return f'M*{other}'

def __rmul__(self, other):
return f'{other}*M'


class TestVec2D(VectorComparisonMixin, unittest.TestCase):

Expand Down Expand Up @@ -208,9 +216,15 @@ def test_vector_multiply(self):
self.assertAlmostEqual(answer, expected)

vec = Vec2D(0.5, 3)
answer = vec * 10
expected = Vec2D(5, 30)
self.assertVectorsAlmostEqual(answer, expected)
self.assertVectorsAlmostEqual(vec * 10, expected)
self.assertVectorsAlmostEqual(10 * vec, expected)
self.assertVectorsAlmostEqual(vec * 10.0, expected)
self.assertVectorsAlmostEqual(10.0 * vec, expected)

M = Multiplier()
self.assertEqual(vec * M, Vec2D(f"{vec[0]}*M", f"{vec[1]}*M"))
self.assertEqual(M * vec, f'M*{vec}')

def test_vector_negative(self):
vec = Vec2D(10, -10)
Expand Down
1 change: 1 addition & 0 deletions Lib/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def __mul__(self, other):
def __rmul__(self, other):
if isinstance(other, int) or isinstance(other, float):
return Vec2D(self[0]*other, self[1]*other)
return NotImplemented
def __sub__(self, other):
return Vec2D(self[0]-other[0], self[1]-other[1])
def __neg__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed :meth:`turtle.Vec2D.__rmul__` for arguments which are not int or
float.