From a7bf556abb901096f6344b7aa536b701edc43f83 Mon Sep 17 00:00:00 2001 From: "E. Paine" <63801254+E-Paine@users.noreply.github.com> Date: Thu, 24 Sep 2020 11:52:53 +0100 Subject: [PATCH 1/7] Add neg and equal methods (+ tests) --- Lib/tkinter/font.py | 10 ++++++++++ Lib/tkinter/test/test_tkinter/test_font.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Lib/tkinter/font.py b/Lib/tkinter/font.py index 15ad7ab4b63a815..aad60075ad4595f 100644 --- a/Lib/tkinter/font.py +++ b/Lib/tkinter/font.py @@ -118,6 +118,11 @@ def __del__(self): except Exception: pass + def __neg__(self): + cp = self.copy() + cp['size'] = -(cp['size']) + return cp + def copy(self): "Return a distinct copy of the current font" return Font(self._tk, **self.actual()) @@ -149,6 +154,11 @@ def config(self, **options): configure = config + def equal(self, other): + if not isinstance(other, Font): + return NotImplemented + return self.actual() == other.actual() + def measure(self, text, displayof=None): "Return text width" args = (text,) diff --git a/Lib/tkinter/test/test_tkinter/test_font.py b/Lib/tkinter/test/test_tkinter/test_font.py index a021ea336807bb8..4a750df8f0bec89 100644 --- a/Lib/tkinter/test/test_tkinter/test_font.py +++ b/Lib/tkinter/test/test_tkinter/test_font.py @@ -72,6 +72,24 @@ def test_eq(self): self.assertNotEqual(font1, 0) self.assertEqual(font1, ALWAYS_EQ) + def test_neg(self): + font1 = font.Font(root=self.root, size=32) + font2 = -font1 + self.assertEqual(font1['size'], -(font2['size'])) + self.assertEqual(font1['size'], -font2['size']) + + def test_equal(self): + font1 = font.Font(root=self.root, size=32) + font2 = font1.copy() + self.assertTrue(font1.equal(font2)) + self.assertTrue(font2.equal(font1)) + font2['size'] = 30 + self.assertFalse(font1.equal(font2)) + font3 = font.Font(root=self.root, size=30, underline=True) + self.assertFalse(font2.equal(font3)) + font2['underline'] = True + self.assertTrue(font2.equal(font3)) + def test_measure(self): self.assertIsInstance(self.font.measure('abc'), int) From 3ee84dd41fc08f184c8454329b908a165b49c20e Mon Sep 17 00:00:00 2001 From: "E. Paine" <63801254+E-Paine@users.noreply.github.com> Date: Thu, 24 Sep 2020 12:12:58 +0100 Subject: [PATCH 2/7] Add doc for new methods --- Doc/library/tkinter.font.rst | 12 +++++++++++- Lib/tkinter/font.py | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst index b0f4505e9e3c692..9791dd77503cea1 100644 --- a/Doc/library/tkinter.font.rst +++ b/Doc/library/tkinter.font.rst @@ -61,6 +61,11 @@ The different font weights and slants are: Return new instance of the current font. + .. method:: equal(other) + + Compare the font to another and check if they represent the same font (in + contrast to ``__eq__``, which checks if they are the same Tk object). + .. method:: measure(text, displayof=None) Return amount of space the text would occupy on the specified display @@ -83,6 +88,11 @@ The different font weights and slants are: *fixed* - 1 if font is fixed-width else 0 + .. method:: __neg__() + + Returns a new font object with the size negated. In practice, this means + converting the font size between points and pixels. + .. function:: families(root=None, displayof=None) Return the different font families. @@ -93,4 +103,4 @@ The different font weights and slants are: .. function:: nametofont(name) - Return a :class:`Font` representation of a tk named font. \ No newline at end of file + Return a :class:`Font` representation of a tk named font. diff --git a/Lib/tkinter/font.py b/Lib/tkinter/font.py index aad60075ad4595f..5735eaaab7df64b 100644 --- a/Lib/tkinter/font.py +++ b/Lib/tkinter/font.py @@ -156,7 +156,7 @@ def config(self, **options): def equal(self, other): if not isinstance(other, Font): - return NotImplemented + raise TypeError("Value for comparison must also be a Font") return self.actual() == other.actual() def measure(self, text, displayof=None): From f04f408829c6372960a46cc241d7706698fbc38e Mon Sep 17 00:00:00 2001 From: "E. Paine" <63801254+E-Paine@users.noreply.github.com> Date: Thu, 24 Sep 2020 12:18:48 +0100 Subject: [PATCH 3/7] Doc versionadded --- Doc/library/tkinter.font.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst index 9791dd77503cea1..c0f0971ddec1427 100644 --- a/Doc/library/tkinter.font.rst +++ b/Doc/library/tkinter.font.rst @@ -66,6 +66,8 @@ The different font weights and slants are: Compare the font to another and check if they represent the same font (in contrast to ``__eq__``, which checks if they are the same Tk object). + .. versionadded:: 3.10 + .. method:: measure(text, displayof=None) Return amount of space the text would occupy on the specified display @@ -93,6 +95,8 @@ The different font weights and slants are: Returns a new font object with the size negated. In practice, this means converting the font size between points and pixels. + .. versionadded:: 3.10 + .. function:: families(root=None, displayof=None) Return the different font families. From 3c07af6d48a7319861edc2406cb1cfe3e75e268b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 24 Sep 2020 11:29:22 +0000 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst diff --git a/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst b/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst new file mode 100644 index 000000000000000..d49efed65fe391b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst @@ -0,0 +1 @@ +Add neg and equal methods to tkinter font class \ No newline at end of file From 4e8bbdea6d35e94bf422be487bf88b4b73331a09 Mon Sep 17 00:00:00 2001 From: "E. Paine" <63801254+E-Paine@users.noreply.github.com> Date: Thu, 24 Sep 2020 12:37:47 +0100 Subject: [PATCH 5/7] Remove doc trailing whitespace --- Doc/library/tkinter.font.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst index c0f0971ddec1427..abaff1525f591f7 100644 --- a/Doc/library/tkinter.font.rst +++ b/Doc/library/tkinter.font.rst @@ -62,7 +62,7 @@ The different font weights and slants are: Return new instance of the current font. .. method:: equal(other) - + Compare the font to another and check if they represent the same font (in contrast to ``__eq__``, which checks if they are the same Tk object). @@ -91,7 +91,7 @@ The different font weights and slants are: *fixed* - 1 if font is fixed-width else 0 .. method:: __neg__() - + Returns a new font object with the size negated. In practice, this means converting the font size between points and pixels. From 0b43e8467ec8d633294fef292fe55284c495bcba Mon Sep 17 00:00:00 2001 From: "E. Paine" <63801254+E-Paine@users.noreply.github.com> Date: Thu, 24 Sep 2020 14:21:55 +0100 Subject: [PATCH 6/7] Resolve @gareth-rees' comments --- Doc/library/tkinter.font.rst | 4 ++-- Lib/tkinter/font.py | 2 +- Lib/tkinter/test/test_tkinter/test_font.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst index abaff1525f591f7..f657979dea1b66c 100644 --- a/Doc/library/tkinter.font.rst +++ b/Doc/library/tkinter.font.rst @@ -64,7 +64,7 @@ The different font weights and slants are: .. method:: equal(other) Compare the font to another and check if they represent the same font (in - contrast to ``__eq__``, which checks if they are the same Tk object). + contrast to :meth:`__eq__`, which checks if they are the same Tk object). .. versionadded:: 3.10 @@ -92,7 +92,7 @@ The different font weights and slants are: .. method:: __neg__() - Returns a new font object with the size negated. In practice, this means + Return a new font object with the size negated. In practice, this means converting the font size between points and pixels. .. versionadded:: 3.10 diff --git a/Lib/tkinter/font.py b/Lib/tkinter/font.py index 5735eaaab7df64b..31585fa12330ae1 100644 --- a/Lib/tkinter/font.py +++ b/Lib/tkinter/font.py @@ -120,7 +120,7 @@ def __del__(self): def __neg__(self): cp = self.copy() - cp['size'] = -(cp['size']) + cp['size'] = -self['size'] return cp def copy(self): diff --git a/Lib/tkinter/test/test_tkinter/test_font.py b/Lib/tkinter/test/test_tkinter/test_font.py index 4a750df8f0bec89..812584ddf294581 100644 --- a/Lib/tkinter/test/test_tkinter/test_font.py +++ b/Lib/tkinter/test/test_tkinter/test_font.py @@ -75,8 +75,8 @@ def test_eq(self): def test_neg(self): font1 = font.Font(root=self.root, size=32) font2 = -font1 - self.assertEqual(font1['size'], -(font2['size'])) self.assertEqual(font1['size'], -font2['size']) + self.assertEqual(font1['size'], (-font2)['size']) def test_equal(self): font1 = font.Font(root=self.root, size=32) From 52cb6266937d709fd4f38b6780c179652fe78bee Mon Sep 17 00:00:00 2001 From: "E. Paine" <63801254+E-Paine@users.noreply.github.com> Date: Sat, 26 Sep 2020 16:11:46 +0100 Subject: [PATCH 7/7] Remove neg method additions --- Doc/library/tkinter.font.rst | 7 ------- Lib/tkinter/font.py | 5 ----- Lib/tkinter/test/test_tkinter/test_font.py | 6 ------ .../next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst | 2 +- 4 files changed, 1 insertion(+), 19 deletions(-) diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst index f657979dea1b66c..0f828259c19d1a3 100644 --- a/Doc/library/tkinter.font.rst +++ b/Doc/library/tkinter.font.rst @@ -90,13 +90,6 @@ The different font weights and slants are: *fixed* - 1 if font is fixed-width else 0 - .. method:: __neg__() - - Return a new font object with the size negated. In practice, this means - converting the font size between points and pixels. - - .. versionadded:: 3.10 - .. function:: families(root=None, displayof=None) Return the different font families. diff --git a/Lib/tkinter/font.py b/Lib/tkinter/font.py index 31585fa12330ae1..874257e0246f114 100644 --- a/Lib/tkinter/font.py +++ b/Lib/tkinter/font.py @@ -118,11 +118,6 @@ def __del__(self): except Exception: pass - def __neg__(self): - cp = self.copy() - cp['size'] = -self['size'] - return cp - def copy(self): "Return a distinct copy of the current font" return Font(self._tk, **self.actual()) diff --git a/Lib/tkinter/test/test_tkinter/test_font.py b/Lib/tkinter/test/test_tkinter/test_font.py index 812584ddf294581..a2cb5e8773c5d2f 100644 --- a/Lib/tkinter/test/test_tkinter/test_font.py +++ b/Lib/tkinter/test/test_tkinter/test_font.py @@ -72,12 +72,6 @@ def test_eq(self): self.assertNotEqual(font1, 0) self.assertEqual(font1, ALWAYS_EQ) - def test_neg(self): - font1 = font.Font(root=self.root, size=32) - font2 = -font1 - self.assertEqual(font1['size'], -font2['size']) - self.assertEqual(font1['size'], (-font2)['size']) - def test_equal(self): font1 = font.Font(root=self.root, size=32) font2 = font1.copy() diff --git a/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst b/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst index d49efed65fe391b..d258381389f4784 100644 --- a/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst +++ b/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst @@ -1 +1 @@ -Add neg and equal methods to tkinter font class \ No newline at end of file +Add :meth:`equal` method to tkinter font class