Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply assertions in debug mode only #2966

Merged
merged 7 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ virtualenv.py
.python-version
.pytest_cache
.vscode
.mypy_cache
1 change: 1 addition & 0 deletions CHANGES/2966.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Apply assertions in debug mode only
24 changes: 14 additions & 10 deletions aiohttp/web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,13 @@ def _prepare_middleware(self):

async def _handle(self, request):
match_info = await self._router.resolve(request)
assert isinstance(match_info, AbstractMatchInfo), match_info
if DEBUG: # pragma: no cover
if not isinstance(match_info, AbstractMatchInfo):
raise TypeError("match_info should be AbstractMAtchInfo "
"instance, not {!r}".format(match_info))
match_info.add_app(self)

if __debug__:
match_info.freeze()
match_info.freeze()

resp = None
request._match_info = match_info
Expand All @@ -335,13 +337,15 @@ async def _handle(self, request):

resp = await handler(request)

assert isinstance(resp, StreamResponse), \
("Handler {!r} should return response instance, "
"got {!r} [middlewares {!r}]").format(
match_info.handler, type(resp),
[middleware
for app in match_info.apps
for middleware in app.middlewares])
if DEBUG:
if not isinstance(resp, StreamResponse):
msg = ("Handler {!r} should return response instance, "
"got {!r} [middlewares {!r}]").format(
match_info.handler, type(resp),
[middleware
for app in match_info.apps
for middleware in app.middlewares])
raise TypeError(msg)
return resp

def __call__(self):
Expand Down
9 changes: 6 additions & 3 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from . import hdrs
from .abc import AbstractMatchInfo, AbstractRouter, AbstractView
from .helpers import DEBUG
from .http import HttpVersion11
from .web_exceptions import (HTTPExpectationFailed, HTTPForbidden,
HTTPMethodNotAllowed, HTTPNotFound)
Expand Down Expand Up @@ -191,9 +192,11 @@ def current_app(self):

@contextmanager
def set_current_app(self, app):
assert app in self._apps, (
"Expected one of the following apps {!r}, got {!r}"
.format(self._apps, app))
if DEBUG: # pragma: no cover
if app not in self._apps:
raise RuntimeError(
"Expected one of the following apps {!r}, got {!r}"
.format(self._apps, app))
prev = self._current_app
self._current_app = app
try:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import aiohttp
from aiohttp import (FormData, HttpVersion10, HttpVersion11, TraceConfig,
multipart, web)
from aiohttp.helpers import DEBUG


try:
Expand Down Expand Up @@ -66,6 +67,8 @@ async def handler(request):
assert 'OK' == txt


@pytest.mark.skipif(not DEBUG,
reason="The check is enabled in debug mode only")
async def test_handler_returns_not_response(aiohttp_server, aiohttp_client):
logger = mock.Mock()

Expand Down