Skip to content

Commit

Permalink
remove deprecated test response tuple behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Nov 6, 2021
1 parent c5677d0 commit 7d2784f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 50 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Unreleased
The ``user_agent`` module provides an interface that can be
subclassed to add a parser, such as ua-parser. By default it
only stores the whole string.
- The test client returns ``TestResponse`` instances and can no
longer be treated as a tuple. All data is available as
properties on the response.
- The ``has_key`` method on some mapping datastructures; use
``key in data`` instead.
- ``Request.disable_data_descriptor`` is removed, pass
Expand Down
53 changes: 15 additions & 38 deletions src/werkzeug/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import mimetypes
import sys
import typing as t
import warnings
from collections import defaultdict
from datetime import datetime
from datetime import timedelta
Expand Down Expand Up @@ -839,6 +838,11 @@ class Client:
`allow_subdomain_redirects` to `True` as if not no external redirects
are allowed.
.. versionchanged:: 2.1
Removed deprecated behavior of treating the response as a
tuple. All data is available as properties on the returned
response object.
.. versionchanged:: 2.0
``response_wrapper`` is always a subclass of
:class:``TestResponse``.
Expand Down Expand Up @@ -1013,7 +1017,6 @@ def resolve_redirect(
def open(
self,
*args: t.Any,
as_tuple: bool = False,
buffered: bool = False,
follow_redirects: bool = False,
**kwargs: t.Any,
Expand All @@ -1032,6 +1035,9 @@ def open(
:attr:`TestResponse.history` lists the intermediate
responses.
.. versionchanged:: 2.1
Removed the ``as_tuple`` parameter.
.. versionchanged:: 2.0
``as_tuple`` is deprecated and will be removed in Werkzeug
2.1. Use :attr:`TestResponse.request` and
Expand Down Expand Up @@ -1111,16 +1117,6 @@ def open(
# the input is an open temporary file.
response.call_on_close(request.input_stream.close)

if as_tuple:
warnings.warn(
"'as_tuple' is deprecated and will be removed in"
" Werkzeug 2.1. Access 'response.request.environ'"
" instead.",
DeprecationWarning,
stacklevel=2,
)
return request.environ, response # type: ignore

return response

def get(self, *args: t.Any, **kw: t.Any) -> "TestResponse":
Expand Down Expand Up @@ -1273,6 +1269,13 @@ class TestResponse(Response):
If the test request included large files, or if the application is
serving a file, call :meth:`close` to close any open files and
prevent Python showing a ``ResourceWarning``.
.. versionchanged:: 2.1
Removed deprecated behavior for treating the response instance
as a tuple.
.. versionadded:: 2.0
Test client methods always return instances of this class.
"""

request: Request
Expand All @@ -1298,29 +1301,3 @@ def __init__(
self.request = request
self.history = history
self._compat_tuple = response, status, headers

def __iter__(self) -> t.Iterator:
warnings.warn(
(
"The test client no longer returns a tuple, it returns"
" a 'TestResponse'. Tuple unpacking is deprecated and"
" will be removed in Werkzeug 2.1. Access the"
" attributes 'data', 'status', and 'headers' instead."
),
DeprecationWarning,
stacklevel=2,
)
return iter(self._compat_tuple)

def __getitem__(self, item: int) -> t.Any:
warnings.warn(
(
"The test client no longer returns a tuple, it returns"
" a 'TestResponse'. Item indexing is deprecated and"
" will be removed in Werkzeug 2.1. Access the"
" attributes 'data', 'status', and 'headers' instead."
),
DeprecationWarning,
stacklevel=2,
)
return self._compat_tuple[item]
12 changes: 0 additions & 12 deletions tests/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,15 +870,3 @@ def app(request):

response = client.get("/%3f?") # escaped ? in path, and empty query string
assert response.get_data(as_text=True) == "/?\n/%3f?"


def test_deprecated_tuple():
app = Request.application(lambda r: Response())
client = Client(app)
response = client.get()

with pytest.deprecated_call():
assert len(list(response)) == 3

with pytest.deprecated_call():
assert response[1] == "200 OK"

0 comments on commit 7d2784f

Please sign in to comment.