mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
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:
parent
74c094d804
commit
7540a432ce
4 changed files with 72 additions and 5 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue