Skip to content

Commit 265918b

Browse files
bpo-23819: asyncio: Replace AssertionError with TypeError where it makes sense (GH-29894)
1 parent 8518ee3 commit 265918b

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

Lib/asyncio/base_events.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,8 @@ def call_later(self, delay, callback, *args, context=None):
706706
Any positional arguments after the callback will be passed to
707707
the callback when it is called.
708708
"""
709+
if delay is None:
710+
raise TypeError('delay must not be None')
709711
timer = self.call_at(self.time() + delay, callback, *args,
710712
context=context)
711713
if timer._source_traceback:
@@ -717,6 +719,8 @@ def call_at(self, when, callback, *args, context=None):
717719
718720
Absolute time corresponds to the event loop's time() method.
719721
"""
722+
if when is None:
723+
raise TypeError("when cannot be None")
720724
self._check_closed()
721725
if self._debug:
722726
self._check_thread()

Lib/asyncio/events.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ class TimerHandle(Handle):
101101
__slots__ = ['_scheduled', '_when']
102102

103103
def __init__(self, when, callback, args, loop, context=None):
104-
assert when is not None
105104
super().__init__(callback, args, loop, context)
106105
if self._source_traceback:
107106
del self._source_traceback[-1]
@@ -661,7 +660,8 @@ def get_event_loop(self):
661660
def set_event_loop(self, loop):
662661
"""Set the event loop."""
663662
self._local._set_called = True
664-
assert loop is None or isinstance(loop, AbstractEventLoop)
663+
if loop is not None and not isinstance(loop, AbstractEventLoop):
664+
raise TypeError(f"loop must be an instance of AbstractEventLoop or None, not '{type(loop).__name__}'")
665665
self._local._loop = loop
666666

667667
def new_event_loop(self):
@@ -745,7 +745,8 @@ def set_event_loop_policy(policy):
745745
746746
If policy is None, the default policy is restored."""
747747
global _event_loop_policy
748-
assert policy is None or isinstance(policy, AbstractEventLoopPolicy)
748+
if policy is not None and not isinstance(policy, AbstractEventLoopPolicy):
749+
raise TypeError(f"policy must be an instance of AbstractEventLoopPolicy or None, not '{type(policy).__name__}'")
749750
_event_loop_policy = policy
750751

751752

Lib/test/test_asyncio/test_base_events.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ def cb():
255255
self.assertIsInstance(h, asyncio.TimerHandle)
256256
self.assertIn(h, self.loop._scheduled)
257257
self.assertNotIn(h, self.loop._ready)
258+
with self.assertRaises(TypeError, msg="delay must not be None"):
259+
self.loop.call_later(None, cb)
258260

259261
def test_call_later_negative_delays(self):
260262
calls = []
@@ -286,6 +288,8 @@ def cb():
286288
# tolerate a difference of +800 ms because some Python buildbots
287289
# are really slow
288290
self.assertLessEqual(dt, 0.9, dt)
291+
with self.assertRaises(TypeError, msg="when cannot be None"):
292+
self.loop.call_at(None, cb)
289293

290294
def check_thread(self, loop, debug):
291295
def cb():

Lib/test/test_asyncio/test_events.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,10 +2322,6 @@ def callback(*args):
23222322
self.assertIsNone(h._callback)
23232323
self.assertIsNone(h._args)
23242324

2325-
# when cannot be None
2326-
self.assertRaises(AssertionError,
2327-
asyncio.TimerHandle, None, callback, args,
2328-
self.loop)
23292325

23302326
def test_timer_repr(self):
23312327
self.loop.get_debug.return_value = False
@@ -2592,7 +2588,7 @@ def test_set_event_loop(self):
25922588
policy = asyncio.DefaultEventLoopPolicy()
25932589
old_loop = policy.get_event_loop()
25942590

2595-
self.assertRaises(AssertionError, policy.set_event_loop, object())
2591+
self.assertRaises(TypeError, policy.set_event_loop, object())
25962592

25972593
loop = policy.new_event_loop()
25982594
policy.set_event_loop(loop)
@@ -2608,7 +2604,7 @@ def test_get_event_loop_policy(self):
26082604

26092605
def test_set_event_loop_policy(self):
26102606
self.assertRaises(
2611-
AssertionError, asyncio.set_event_loop_policy, object())
2607+
TypeError, asyncio.set_event_loop_policy, object())
26122608

26132609
old_policy = asyncio.get_event_loop_policy()
26142610

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replaced asserts with exceptions in asyncio, patch by Kumar Aditya.

0 commit comments

Comments
 (0)