Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated content_transfer_encoding #8303

Merged
merged 5 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES/8303.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Removed ``content_transfer_encoding`` parameter in :py:meth:`FormData.add_field()
<aiohttp.FormData.add_field>` and passing bytes no longer creates a file
field unless the ``filename`` parameter is used -- by :user:`Dreamsorcerer`.
24 changes: 1 addition & 23 deletions aiohttp/formdata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import io
import warnings
from typing import Any, Iterable, List, Optional
from urllib.parse import urlencode

Expand Down Expand Up @@ -50,18 +49,9 @@ def add_field(
*,
content_type: Optional[str] = None,
filename: Optional[str] = None,
content_transfer_encoding: Optional[str] = None,
) -> None:
if isinstance(value, io.IOBase):
if isinstance(value, (io.IOBase, bytes, bytearray, memoryview)):
self._is_multipart = True
elif isinstance(value, (bytes, bytearray, memoryview)):
msg = (
"In v4, passing bytes will no longer create a file field. "
"Please explicitly use the filename parameter or pass a BytesIO object."
)
if filename is None and content_transfer_encoding is None:
warnings.warn(msg, DeprecationWarning)
filename = name

type_options: MultiDict[str] = MultiDict({"name": name})
if filename is not None and not isinstance(filename, str):
Expand All @@ -82,18 +72,6 @@ def add_field(
)
headers[hdrs.CONTENT_TYPE] = content_type
self._is_multipart = True
if content_transfer_encoding is not None:
if not isinstance(content_transfer_encoding, str):
raise TypeError(
"content_transfer_encoding must be an instance"
" of str. Got: %s" % content_transfer_encoding
)
msg = (
"content_transfer_encoding is deprecated. "
"To maintain compatibility with v4 please pass a BytesPayload."
)
warnings.warn(msg, DeprecationWarning)
self._is_multipart = True

self._fields.append((type_options, headers, value))

Expand Down
6 changes: 2 additions & 4 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def fname(here: Any):

def new_dummy_form():
form = FormData()
with pytest.warns(DeprecationWarning, match="BytesPayload"):
form.add_field("name", b"123", content_transfer_encoding="base64")
form.add_field("name", b"123")
return form


Expand Down Expand Up @@ -505,8 +504,7 @@ async def handler(request):
return web.Response()

form = FormData()
with pytest.warns(DeprecationWarning, match="BytesPayload"):
form.add_field("name", b"123", content_transfer_encoding="base64")
form.add_field("name", b"123")

app = web.Application()
app.router.add_post("/", handler)
Expand Down
Loading