Skip to content

Commit c0a1276

Browse files
committed
Rework as a docs-only change
1 parent b9f43a3 commit c0a1276

4 files changed

Lines changed: 8 additions & 22 deletions

File tree

‎Doc/library/urllib.request.rst‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,17 @@ The following classes are provided:
246246
server, or ``None`` if no such data is needed. Currently HTTP
247247
requests are the only ones that use *data*. The supported object
248248
types include bytes, file-like objects, and iterables of bytes-like objects.
249-
Mappings, such as :class:`dict`, are not accepted and raise
250-
:exc:`TypeError`.
251249
If no ``Content-Length`` nor ``Transfer-Encoding`` header field
252250
has been provided, :class:`HTTPHandler` will set these headers according
253251
to the type of *data*. ``Content-Length`` will be used to send
254252
bytes objects, while ``Transfer-Encoding: chunked`` as specified in
255253
:rfc:`7230`, Section 3.3.1 will be used to send files and other iterables.
256254

255+
*data* is not serialized before it is sent: the elements of an iterable
256+
are sent as is, so a :class:`dict` yields only its keys. Structured
257+
data such as a JSON object must be encoded to bytes first, for example
258+
with :func:`json.dumps`.
259+
257260
For an HTTP POST request method, *data* should be a buffer in the
258261
standard :mimetype:`application/x-www-form-urlencoded` format. The
259262
:func:`urllib.parse.urlencode` function takes a mapping or sequence
@@ -320,9 +323,6 @@ The following classes are provided:
320323
provided and *data* is neither ``None`` nor a bytes object.
321324
Fall back to use chunked transfer encoding instead.
322325

323-
.. versionchanged:: next
324-
Raise :exc:`TypeError` if *data* is a mapping.
325-
326326
.. class:: OpenerDirector()
327327

328328
The :class:`OpenerDirector` class opens URLs via :class:`BaseHandler`\ s chained

‎Lib/test/test_urllib2.py‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import ftplib
1212
import socket
1313
import array
14-
import collections
1514
import sys
1615
import tempfile
1716
import subprocess
@@ -997,12 +996,6 @@ def test_http(self):
997996
req = Request("http://example.com/","badpost")
998997
self.assertRaises(TypeError, h.do_request_, req)
999998

1000-
# Check for TypeError on request data which is a mapping.
1001-
for data in ({'a': 1}, collections.UserDict({'a': 1})):
1002-
req = Request("http://example.com/", data)
1003-
with self.assertRaisesRegex(TypeError, type(data).__name__):
1004-
h.do_request_(req)
1005-
1006999
# check adding of standard headers
10071000
o.addheaders = [("Spam", "eggs")]
10081001
for data in b"", None: # POST, GET

‎Lib/urllib/request.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
import sys
9696
import time
9797
import tempfile
98-
from collections.abc import Mapping
98+
9999

100100
from urllib.error import URLError, HTTPError, ContentTooShortError
101101
from urllib.parse import (
@@ -1261,10 +1261,9 @@ def do_request_(self, request):
12611261

12621262
if request.data is not None: # POST
12631263
data = request.data
1264-
if isinstance(data, (str, Mapping)):
1264+
if isinstance(data, str):
12651265
msg = "POST data should be bytes, an iterable of bytes, " \
1266-
"or a file object. It cannot be of type " \
1267-
f"{type(data).__name__}."
1266+
"or a file object. It cannot be of type str."
12681267
raise TypeError(msg)
12691268
if not request.has_header('Content-type'):
12701269
request.add_unredirected_header(

‎Misc/NEWS.d/next/Library/2026-09-24-11-36-57.gh-issue-110462.DKAIo2.rst‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)