Skip to content

Commit 5d714cc

Browse files
author
Bob Green
committed
Allow sync decorator call from async function
litl#83
1 parent 4c98ce6 commit 5d714cc

File tree

2 files changed

+0
-64
lines changed

2 files changed

+0
-64
lines changed

backoff/_decorator.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,6 @@ def decorate(target):
8585
import backoff._async
8686
retry = backoff._async.retry_predicate
8787

88-
elif _is_event_loop() and _is_current_task():
89-
# Verify that sync version is not being run from coroutine
90-
# (that would lead to event loop hiccups).
91-
raise TypeError(
92-
"backoff.on_predicate applied to a regular function "
93-
"inside coroutine, this will lead to event loop "
94-
"hiccups. Use backoff.on_predicate on coroutines in "
95-
"asynchronous code.")
96-
9788
if retry is None:
9889
retry = _sync.retry_predicate
9990

@@ -174,14 +165,6 @@ def decorate(target):
174165
if asyncio.iscoroutinefunction(target):
175166
import backoff._async
176167
retry = backoff._async.retry_exception
177-
elif _is_event_loop() and _is_current_task():
178-
# Verify that sync version is not being run from coroutine
179-
# (that would lead to event loop hiccups).
180-
raise TypeError(
181-
"backoff.on_exception applied to a regular function "
182-
"inside coroutine, this will lead to event loop "
183-
"hiccups. Use backoff.on_exception on coroutines in "
184-
"asynchronous code.")
185168

186169
if retry is None:
187170
retry = _sync.retry_exception
@@ -193,25 +176,3 @@ def decorate(target):
193176

194177
# Return a function which decorates a target with a retry loop.
195178
return decorate
196-
197-
198-
def _is_event_loop(): # pragma: no cover
199-
import asyncio
200-
201-
try:
202-
if sys.version_info >= (3, 7):
203-
asyncio.get_running_loop()
204-
205-
asyncio.get_event_loop()
206-
except RuntimeError:
207-
return False
208-
else:
209-
return True
210-
211-
212-
def _is_current_task(): # pragma: no cover
213-
import asyncio
214-
if sys.version_info >= (3, 7):
215-
return asyncio.current_task() is not None
216-
217-
return asyncio.Task.current_task() is not None

tests/python35/test_backoff_async.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -615,31 +615,6 @@ async def coro():
615615
assert (await task)
616616

617617

618-
@pytest.mark.asyncio
619-
async def test_on_exception_on_regular_function():
620-
# Force this function to be a running coroutine.
621-
await asyncio.sleep(0)
622-
623-
with pytest.raises(TypeError) as excinfo:
624-
@backoff.on_exception(backoff.expo, ValueError)
625-
def regular_func():
626-
pass
627-
assert "applied to a regular function" in str(excinfo.value)
628-
629-
630-
@pytest.mark.asyncio
631-
async def test_on_predicate_on_regular_function():
632-
# Force this function to be a running coroutine.
633-
await asyncio.sleep(0)
634-
635-
with pytest.raises(TypeError) as excinfo:
636-
@backoff.on_predicate(backoff.expo)
637-
def regular_func():
638-
pass
639-
640-
assert "applied to a regular function" in str(excinfo.value)
641-
642-
643618
def test_on_predicate_on_regular_function_without_event_loop(monkeypatch):
644619
monkeypatch.setattr('time.sleep', lambda x: None)
645620

0 commit comments

Comments
 (0)