diff --git a/.gitignore b/.gitignore index 64dcbce9b7f..c2f46f18bfe 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,4 @@ virtualenv.py .python-version .pytest_cache .vscode +.mypy_cache \ No newline at end of file diff --git a/CHANGES/2966.feature b/CHANGES/2966.feature new file mode 100644 index 00000000000..2eed6c5c68c --- /dev/null +++ b/CHANGES/2966.feature @@ -0,0 +1 @@ +Apply assertions in debug mode only \ No newline at end of file diff --git a/aiohttp/web_app.py b/aiohttp/web_app.py index bebad310bdc..a808b06f917 100644 --- a/aiohttp/web_app.py +++ b/aiohttp/web_app.py @@ -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 @@ -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): diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index b0c327b1dc0..16cf008986d 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -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) @@ -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: diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index 4999c043912..9354a06fb75 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -14,6 +14,7 @@ import aiohttp from aiohttp import (FormData, HttpVersion10, HttpVersion11, TraceConfig, multipart, web) +from aiohttp.helpers import DEBUG try: @@ -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()