diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst index fb5353e1895bf9..31fbac9c0a40ad 100644 --- a/Doc/library/urllib.parse.rst +++ b/Doc/library/urllib.parse.rst @@ -193,6 +193,11 @@ or on combining URL components into a URL string. Characters that affect netloc parsing under NFKC normalization will now raise :exc:`ValueError`. + .. versionchanged:: 3.14 + Values for ``url`` and ``scheme`` other than strings or bytes raise + :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be + changed to :exc:`TypeError` in future versions of Python). + .. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None, separator='&') @@ -282,6 +287,11 @@ or on combining URL components into a URL string. query parameter separator. This has been changed to allow only a single separator key, with ``&`` as the default separator. + .. versionchanged:: 3.14 + Values for ``qs`` and ``separator`` other than strings or bytes raise + :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be + changed to :exc:`TypeError` in future versions of Python). + .. function:: urlunparse(parts) @@ -291,6 +301,11 @@ or on combining URL components into a URL string. unnecessary delimiters (for example, a ``?`` with an empty query; the RFC states that these are equivalent). + .. versionchanged:: 3.14 + Items in ``parts`` other than strings or bytes raise + :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be + changed to :exc:`TypeError` in future versions of Python). + .. function:: urlsplit(urlstring, scheme='', allow_fragments=True) @@ -364,6 +379,11 @@ or on combining URL components into a URL string. .. versionchanged:: 3.12 Leading WHATWG C0 control and space characters are stripped from the URL. + .. versionchanged:: 3.14 + Values for the url argument and ``scheme`` other than strings or bytes raise + :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be + changed to :exc:`TypeError` in future versions of Python). + .. _WHATWG spec: https://url.spec.whatwg.org/#concept-basic-url-parser .. function:: urlunsplit(parts) @@ -374,6 +394,11 @@ or on combining URL components into a URL string. URL that was parsed originally had unnecessary delimiters (for example, a ? with an empty query; the RFC states that these are equivalent). + .. versionchanged:: 3.14 + Items in ``parts`` other than strings or bytes raise + :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be + changed to :exc:`TypeError` in future versions of Python). + .. function:: urljoin(base, url, allow_fragments=True) @@ -408,6 +433,11 @@ or on combining URL components into a URL string. Behavior updated to match the semantics defined in :rfc:`3986`. + .. versionchanged:: 3.14 + Values for ``base`` and ``url`` other than strings or bytes raise + :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be + changed to :exc:`TypeError` in future versions of Python). + .. function:: urldefrag(url) @@ -433,6 +463,11 @@ or on combining URL components into a URL string. .. versionchanged:: 3.2 Result is a structured object rather than a simple 2-tuple. + .. versionchanged:: 3.14 + Values other than strings or bytes raise + :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be + changed to :exc:`TypeError` in future versions of Python). + .. function:: unwrap(url) Extract the url from a wrapped URL (that is, a string formatted as diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index e1bd52370d776c..6f2eaf66c3d0ec 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -552,6 +552,11 @@ Deprecated use :func:`inspect.iscoroutinefunction` instead. (Contributed by Jiahao Li and Kumar Aditya in :gh:`122875`.) +* Providing anything but a string or bytes object to :mod:`urllib.parse` + functions expecting strings or bytes now raises :exc:`DeprecationWarning` + if the value tests false, or :exc:`TypeError` if it tests true. + (Contributed by Jacob Walls in :issue:`19094`.) + .. Add deprecations above alphabetically, not here at the end. .. include:: ../deprecations/c-api-pending-removal-in-3.15.rst diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index d49e4388696ab4..ee56b43ff49882 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -1128,6 +1128,12 @@ def test_mixed_types_rejected(self): with self.assertRaisesRegex(TypeError, "Cannot mix str"): urllib.parse.urljoin(b"http://python.org", "http://python.org") + def test_non_string_true_values_rejected(self): + # True values raise informative TypeErrors + msg = "Expected a string or bytes object: got