Skip to content

Commit

Permalink
Fix: Use issubclass instead of isinstance
Browse files Browse the repository at this point in the history
Bug described at #2831
  • Loading branch information
jace authored and pgjones committed Mar 3, 2024
1 parent 4e5bdca commit 7ab3823
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/werkzeug/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
{},
)

Expand Down
22 changes: 22 additions & 0 deletions tests/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 7ab3823

Please sign in to comment.