From 7ab3823fb620d29bcfeeff7499dd9a64ad90f360 Mon Sep 17 00:00:00 2001 From: Kiran Jonnalagadda Date: Mon, 8 Jan 2024 15:32:22 +0530 Subject: [PATCH] Fix: Use `issubclass` instead of `isinstance` Bug described at https://github.com/pallets/werkzeug/issues/2831 --- src/werkzeug/test.py | 6 ++++-- tests/test_test.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/werkzeug/test.py b/src/werkzeug/test.py index 7b5899a97..694e5d8e5 100644 --- a/src/werkzeug/test.py +++ b/src/werkzeug/test.py @@ -809,10 +809,12 @@ def __init__( if response_wrapper in {None, Response}: response_wrapper = TestResponse - elif not isinstance(response_wrapper, TestResponse): + elif response_wrapper is not None and not issubclass( + response_wrapper, TestResponse + ): response_wrapper = type( "WrapperTestResponse", - (TestResponse, response_wrapper), # type: ignore + (TestResponse, response_wrapper), {}, ) diff --git a/tests/test_test.py b/tests/test_test.py index c7f21fa11..d317d69c9 100644 --- a/tests/test_test.py +++ b/tests/test_test.py @@ -16,6 +16,7 @@ from werkzeug.test import EnvironBuilder from werkzeug.test import run_wsgi_app from werkzeug.test import stream_encode_multipart +from werkzeug.test import TestResponse from werkzeug.utils import redirect from werkzeug.wrappers import Request from werkzeug.wrappers import Response @@ -903,3 +904,24 @@ def test_no_content_type_header_addition(): c = Client(no_response_headers_app) response = c.open() assert response.headers == Headers([("Content-Length", "8")]) + + +def test_client_response_wrapper(): + class CustomResponse(Response): + pass + + class CustomTestResponse(TestResponse, Response): + pass + + c1 = Client(Response(), CustomResponse) + r1 = c1.open() + + assert isinstance(r1, CustomResponse) + assert type(r1) is not CustomResponse # Got subclassed + assert issubclass(type(r1), CustomResponse) + + c2 = Client(Response(), CustomTestResponse) + r2 = c2.open() + + assert isinstance(r2, CustomTestResponse) + assert type(r2) is CustomTestResponse # Did not get subclassed