summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-09-07 16:30:21 (GMT)
committerGitHub <noreply@github.com>2020-09-07 16:30:21 (GMT)
commit1671b0e4e00061f23b6677e37e27a4ed44998329 (patch)
treede1e4b54fc1ebff84bef1b935e399b996ebeed14
parentc73ee5acc96b4bbd6885156883b224b8cc3e470c (diff)
downloadcpython-1671b0e4e00061f23b6677e37e27a4ed44998329.zip
cpython-1671b0e4e00061f23b6677e37e27a4ed44998329.tar.gz
cpython-1671b0e4e00061f23b6677e37e27a4ed44998329.tar.bz2
bpo-41720: Add "return NotImplemented" in turtle.Vec2D.__rmul__(). (GH-22092)
(cherry picked from commit fd4cafd4700dc03cb05fc2e5263c2666d785d6e3) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r--Lib/test/test_turtle.py18
-rw-r--r--Lib/turtle.py1
-rw-r--r--Misc/NEWS.d/next/Library/2020-09-04-20-45-38.bpo-41720.PW9MzZ.rst2
3 files changed, 19 insertions, 2 deletions
diff --git a/Lib/test/test_turtle.py b/Lib/test/test_turtle.py
index 38448c7..5d5f057 100644
--- a/Lib/test/test_turtle.py
+++ b/Lib/test/test_turtle.py
@@ -127,6 +127,14 @@ class VectorComparisonMixin:
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):
@@ -208,9 +216,15 @@ class TestVec2D(VectorComparisonMixin, unittest.TestCase):
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)
diff --git a/Lib/turtle.py b/Lib/turtle.py
index ee67a35..ba8288d 100644
--- a/Lib/turtle.py
+++ b/Lib/turtle.py
@@ -258,6 +258,7 @@ class Vec2D(tuple):
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):
diff --git a/Misc/NEWS.d/next/Library/2020-09-04-20-45-38.bpo-41720.PW9MzZ.rst b/Misc/NEWS.d/next/Library/2020-09-04-20-45-38.bpo-41720.PW9MzZ.rst
new file mode 100644
index 0000000..5d2a509
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-09-04-20-45-38.bpo-41720.PW9MzZ.rst
@@ -0,0 +1,2 @@
+Fixed :meth:`turtle.Vec2D.__rmul__` for arguments which are not int or
+float.