Skip to content
Prev Previous commit
Next Next commit
Add tests for list(list) and list(tuple)
  • Loading branch information
mrahtz committed May 7, 2022
commit b82c9e0e9dbd3f6a372bdac31dddaf73b9014f01
21 changes: 21 additions & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for C-implemented GenericAlias."""

import collections
Comment thread
mrahtz marked this conversation as resolved.
Outdated
import unittest
import pickle
import copy
Expand Down Expand Up @@ -487,5 +488,25 @@ def test_del_iter(self):
del iter_x


class TypeIterationTests(unittest.TestCase):
_UNITERABLE_TYPES = (list, tuple)

def test_cannot_iterate(self):
for test_type in self._UNITERABLE_TYPES:
with self.subTest(type=test_type):
expected_error_regex = "object is not iterable"
with self.assertRaisesRegex(TypeError, expected_error_regex):
iter(test_type)
with self.assertRaisesRegex(TypeError, expected_error_regex):
list(test_type)
with self.assertRaisesRegex(TypeError, expected_error_regex):
for _ in test_type:
pass

def test_is_not_instance_of_iterable(self):
for type_to_test in self._UNITERABLE_TYPES:
self.assertNotIsInstance(type_to_test, collections.abc.Iterable)


if __name__ == "__main__":
unittest.main()