Skip to content

Commit

Permalink
Apply assertions in debug mode only (#2966)
Browse files Browse the repository at this point in the history
* Apply assertions in debug mode only

* Disable the check in release mode

* Always freeze

* Fix flake8

* Suppress coverage warnings

* Add change log
  • Loading branch information
asvetlov authored May 1, 2018
1 parent c4cbc61 commit bcd6fa4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
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

0 comments on commit bcd6fa4

Please sign in to comment.