gh-92261: Disallow iteration of Union (and other special forms) (GH-92262) (GH-92582)

(cherry picked from commit 4739997e14)

Co-authored-by: Matthew Rahtz <matthew.rahtz@gmail.com>
This commit is contained in:
Miss Islington (bot) 2022-05-09 22:39:31 -07:00 committed by GitHub
parent 74c094d804
commit 7540a432ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 5 deletions

View file

@ -487,5 +487,25 @@ class BaseTest(unittest.TestCase):
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, Iterable)
if __name__ == "__main__":
unittest.main()