Skip to content

Commit 7b96134

Browse files
committed
Reformat with code formatter
`ruff format` with ruff 0.5.7
1 parent 6fd4dfd commit 7b96134

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

nest_asyncio.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,20 @@ def _get_event_loop(stacklevel=3):
4141
return loop
4242

4343
# Use module level _current_tasks, all_tasks and patch run method.
44-
if hasattr(asyncio, '_nest_patched'):
44+
if hasattr(asyncio, "_nest_patched"):
4545
return
4646
if sys.version_info >= (3, 6, 0):
47-
asyncio.Task = asyncio.tasks._CTask = asyncio.tasks.Task = \
48-
asyncio.tasks._PyTask
49-
asyncio.Future = asyncio.futures._CFuture = asyncio.futures.Future = \
47+
asyncio.Task = asyncio.tasks._CTask = asyncio.tasks.Task = asyncio.tasks._PyTask
48+
asyncio.Future = asyncio.futures._CFuture = asyncio.futures.Future = (
5049
asyncio.futures._PyFuture
50+
)
5151
if sys.version_info < (3, 7, 0):
5252
asyncio.tasks._current_tasks = asyncio.tasks.Task._current_tasks
5353
asyncio.all_tasks = asyncio.tasks.Task.all_tasks
5454
if sys.version_info >= (3, 9, 0):
55-
events._get_event_loop = events.get_event_loop = \
56-
asyncio.get_event_loop = _get_event_loop
55+
events._get_event_loop = events.get_event_loop = asyncio.get_event_loop = (
56+
_get_event_loop
57+
)
5758
asyncio.run = run
5859
asyncio._nest_patched = True
5960

@@ -93,8 +94,7 @@ def run_until_complete(self, future):
9394
if self._stopping:
9495
break
9596
if not f.done():
96-
raise RuntimeError(
97-
'Event loop stopped before Future completed.')
97+
raise RuntimeError("Event loop stopped before Future completed.")
9898
return f.result()
9999

100100
def _run_once(self):
@@ -108,10 +108,12 @@ def _run_once(self):
108108
heappop(scheduled)
109109

110110
timeout = (
111-
0 if ready or self._stopping
112-
else min(max(
113-
scheduled[0]._when - self.time(), 0), 86400) if scheduled
114-
else None)
111+
0
112+
if ready or self._stopping
113+
else min(max(scheduled[0]._when - self.time(), 0), 86400)
114+
if scheduled
115+
else None
116+
)
115117
event_list = self._selector.select(timeout)
116118
self._process_events(event_list)
117119

@@ -157,8 +159,10 @@ def manage_run(self):
157159
events._set_running_loop(old_running_loop)
158160
self._num_runs_pending -= 1
159161
if self._is_proactorloop:
160-
if (self._num_runs_pending == 0
161-
and self._self_reading_future is not None):
162+
if (
163+
self._num_runs_pending == 0
164+
and self._self_reading_future is not None
165+
):
162166
ov = self._self_reading_future._ov
163167
self._self_reading_future.cancel()
164168
if ov is not None:
@@ -167,7 +171,7 @@ def manage_run(self):
167171

168172
@contextmanager
169173
def manage_asyncgens(self):
170-
if not hasattr(sys, 'get_asyncgen_hooks'):
174+
if not hasattr(sys, "get_asyncgen_hooks"):
171175
# Python version is too old.
172176
return
173177
old_agen_hooks = sys.get_asyncgen_hooks()
@@ -176,7 +180,8 @@ def manage_asyncgens(self):
176180
if self._asyncgens is not None:
177181
sys.set_asyncgen_hooks(
178182
firstiter=self._asyncgen_firstiter_hook,
179-
finalizer=self._asyncgen_finalizer_hook)
183+
finalizer=self._asyncgen_finalizer_hook,
184+
)
180185
yield
181186
finally:
182187
self._set_coroutine_origin_tracking(False)
@@ -187,23 +192,27 @@ def _check_running(self):
187192
"""Do not throw exception if loop is already running."""
188193
pass
189194

190-
if hasattr(loop, '_nest_patched'):
195+
if hasattr(loop, "_nest_patched"):
191196
return
192197
if not isinstance(loop, asyncio.BaseEventLoop):
193-
raise ValueError('Can\'t patch loop of type %s' % type(loop))
198+
raise ValueError("Can't patch loop of type %s" % type(loop))
194199
cls = loop.__class__
195200
cls.run_forever = run_forever
196201
cls.run_until_complete = run_until_complete
197202
cls._run_once = _run_once
198203
cls._check_running = _check_running
199204
cls._check_runnung = _check_running # typo in Python 3.7 source
200205
cls._num_runs_pending = 1 if loop.is_running() else 0
201-
cls._is_proactorloop = (
202-
os.name == 'nt' and issubclass(cls, asyncio.ProactorEventLoop))
206+
cls._is_proactorloop = os.name == "nt" and issubclass(
207+
cls, asyncio.ProactorEventLoop
208+
)
203209
if sys.version_info < (3, 7, 0):
204210
cls._set_coroutine_origin_tracking = cls._set_coroutine_wrapper
205-
curr_tasks = asyncio.tasks._current_tasks \
206-
if sys.version_info >= (3, 7, 0) else asyncio.Task._current_tasks
211+
curr_tasks = (
212+
asyncio.tasks._current_tasks
213+
if sys.version_info >= (3, 7, 0)
214+
else asyncio.Task._current_tasks
215+
)
207216
cls._nest_patched = True
208217

209218

@@ -212,8 +221,9 @@ def _patch_tornado():
212221
If tornado is imported before nest_asyncio, make tornado aware of
213222
the pure-Python asyncio Future.
214223
"""
215-
if 'tornado' in sys.modules:
224+
if "tornado" in sys.modules:
216225
import tornado.concurrent as tc # type: ignore
226+
217227
tc.Future = asyncio.Future
218228
if asyncio.Future not in tc.FUTURES:
219229
tc.FUTURES += (asyncio.Future,)

tests/nest_test.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def exception_handler(loop, context):
9-
print('Exception:', context)
9+
print("Exception:", context)
1010

1111

1212
class NestTest(unittest.TestCase):
@@ -27,7 +27,6 @@ async def coro(self):
2727
return 42
2828

2929
def test_nesting(self):
30-
3130
async def f1():
3231
result = self.loop.run_until_complete(self.coro())
3332
self.assertEqual(result, await self.coro())
@@ -42,7 +41,6 @@ async def f2():
4241
self.assertEqual(result, 42)
4342

4443
def test_ensure_future_with_run_until_complete(self):
45-
4644
async def f():
4745
task = asyncio.ensure_future(self.coro())
4846
return self.loop.run_until_complete(task)
@@ -51,19 +49,18 @@ async def f():
5149
self.assertEqual(result, 42)
5250

5351
def test_ensure_future_with_run_until_complete_with_wait(self):
54-
5552
async def f():
5653
task = asyncio.ensure_future(self.coro())
5754
done, pending = self.loop.run_until_complete(
58-
asyncio.wait([task], return_when=asyncio.ALL_COMPLETED))
55+
asyncio.wait([task], return_when=asyncio.ALL_COMPLETED)
56+
)
5957
task = done.pop()
6058
return task.result()
6159

6260
result = self.loop.run_until_complete(f())
6361
self.assertEqual(result, 42)
6462

6563
def test_timeout(self):
66-
6764
async def f1():
6865
await asyncio.sleep(0.1)
6966

@@ -74,7 +71,6 @@ async def f2():
7471
self.loop.run_until_complete(f2())
7572

7673
def test_two_run_until_completes_in_one_outer_loop(self):
77-
7874
async def f1():
7975
self.loop.run_until_complete(asyncio.sleep(0.02))
8076
return 4
@@ -83,14 +79,14 @@ async def f2():
8379
self.loop.run_until_complete(asyncio.sleep(0.01))
8480
return 2
8581

86-
result = self.loop.run_until_complete(
87-
asyncio.gather(f1(), f2()))
82+
result = self.loop.run_until_complete(asyncio.gather(f1(), f2()))
8883
self.assertEqual(result, [4, 2])
8984

90-
@unittest.skipIf(sys.version_info < (3, 7, 0), 'No contextvars module')
85+
@unittest.skipIf(sys.version_info < (3, 7, 0), "No contextvars module")
9186
def test_contextvars(self):
9287
from contextvars import ContextVar
93-
var = ContextVar('var')
88+
89+
var = ContextVar("var")
9490
var.set(0)
9591

9692
async def set_val():
@@ -105,5 +101,5 @@ async def coro():
105101
self.assertEqual(result, 42)
106102

107103

108-
if __name__ == '__main__':
104+
if __name__ == "__main__":
109105
unittest.main()

0 commit comments

Comments
 (0)