Skip to content

Commit

Permalink
Fix stacklevel in _CoroGuard's warning (#2125)
Browse files Browse the repository at this point in the history
Fixes #2106
  • Loading branch information
hynek authored and asvetlov committed Jul 25, 2017
1 parent 0de2c16 commit b54f2e9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Gregory Haynes
Günther Jena
Hu Bo
Hugo Herter
Hynek Schlawack
Igor Davydenko
Igor Pavlov
Ingmar Steen
Expand Down
7 changes: 6 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ def __await__(self):


class _CoroGuard(_BaseCoroMixin):
"""Only to be used with func:`deprecated_noop`.
Otherwise the stack information in the raised warning doesn't line up with
the user's code anymore.
"""
__slots__ = ('_msg', '_awaited')

def __init__(self, coro, msg):
Expand All @@ -126,7 +131,7 @@ def __await__(self):
def __del__(self):
self._coro = None
if not self._awaited:
warnings.warn(self._msg, DeprecationWarning)
warnings.warn(self._msg, DeprecationWarning, stacklevel=2)


coroutines = asyncio.coroutines
Expand Down
1 change: 1 addition & 0 deletions changes/2106.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warnings about unawaited coroutines now correctly point to the user's code.
5 changes: 4 additions & 1 deletion tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ def test_close_coro(create_session, loop):
def test_close_deprecated(create_session):
session = create_session()

with pytest.warns(DeprecationWarning):
with pytest.warns(DeprecationWarning) as ctx:
session.close()

# Assert the warning points at us and not at _CoroGuard.
assert ctx.list[0].filename == __file__


def test_init_headers_simple_dict(create_session):
session = create_session(headers={"h1": "header1",
Expand Down
7 changes: 6 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
def test_warn():
with pytest.warns(DeprecationWarning) as ctx:
helpers.deprecated_noop('Text')
assert str(ctx.list[0].message) == 'Text'

w = ctx.list[0]

assert str(w.message) == 'Text'
# Assert the warning points at us and not at _CoroGuard.
assert w.filename == __file__


@asyncio.coroutine
Expand Down

0 comments on commit b54f2e9

Please sign in to comment.