From b6453adcbdb3bda5f66c5a971ed624f519963c66 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 30 Apr 2025 19:22:38 +0300 Subject: [PATCH 1/2] gh-133213: Add tests for `string.templatelib.TemplateIter` --- Lib/test/test_string/test_templatelib.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Lib/test/test_string/test_templatelib.py b/Lib/test/test_string/test_templatelib.py index 5cf187828510c4..100ce11ea9d5d0 100644 --- a/Lib/test/test_string/test_templatelib.py +++ b/Lib/test/test_string/test_templatelib.py @@ -1,5 +1,6 @@ import pickle import unittest +from collections.abc import Iterator, Iterable from string.templatelib import Template, Interpolation from test.test_string._support import TStringBaseCase, fstring @@ -118,5 +119,27 @@ def test_pickle_interpolation(self): self.assertEqual(unpickled.format_spec, interpolation.format_spec) +class TemplateIterTests(unittest.TestCase): + def test_basic(self): + self.assertIsInstance(iter(t''), Iterable) + self.assertIsInstance(iter(t''), Iterator) + + def test_final(self): + with self.assertRaisesRegex(TypeError, 'is not an acceptable base type'): + class Sub(type(iter(t''))): ... + + def test_iter(self): + x = 1 + res = list(iter(t'abc {x} yz')) + + self.assertEqual(res[0], 'abc ') + self.assertIsInstance(res[1], Interpolation) + self.assertEqual(res[1].value, 1) + self.assertEqual(res[1].expression, 'x') + self.assertEqual(res[1].conversion, None) + self.assertEqual(res[1].format_spec, '') + self.assertEqual(res[2], ' yz') + + if __name__ == '__main__': unittest.main() From 65c230e472d1de67f71a692bee7d8e32231fdbd2 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 30 Apr 2025 20:18:21 +0300 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- Lib/test/test_string/test_templatelib.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_string/test_templatelib.py b/Lib/test/test_string/test_templatelib.py index 100ce11ea9d5d0..0e72c8105682cb 100644 --- a/Lib/test/test_string/test_templatelib.py +++ b/Lib/test/test_string/test_templatelib.py @@ -120,13 +120,14 @@ def test_pickle_interpolation(self): class TemplateIterTests(unittest.TestCase): - def test_basic(self): + def test_abc(self): self.assertIsInstance(iter(t''), Iterable) self.assertIsInstance(iter(t''), Iterator) def test_final(self): + TemplateIter = type(iter(t'')) with self.assertRaisesRegex(TypeError, 'is not an acceptable base type'): - class Sub(type(iter(t''))): ... + class Sub(TemplateIter): ... def test_iter(self): x = 1