Python 3.10 - parenthesized context managers
Did you know?
Python 3.10 comes with support for continuation across multiple lines when using context managers.
An example👇
import unittest from unittest import mock from my_module import my_function # Python < 3.10 class Test(unittest.TestCase): def test(self): with mock.patch("my_module.secrets.token_urlsafe") as a, mock.patch("my_module.string.capwords") as b, mock.patch("my_module.collections.defaultdict") as c: my_function() a.assert_called() b.assert_called() c.assert_called() def test_same(self): with mock.patch("my_module.secrets.token_urlsafe") as a: with mock.patch("my_module.string.capwords") as b: with mock.patch("my_module.collections.defaultdict") as c: my_function() a.assert_called() b.assert_called() c.assert_called() # Python >= 3.10 class Test(unittest.TestCase): def test(self): with ( mock.patch("my_module.secrets.token_urlsafe") as a, mock.patch("my_module.string.capwords") as b, mock.patch("my_module.collections.defaultdictl") as c, ): my_function() a.assert_called() b.assert_called() c.assert_called()