diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst index 26e159306c8107..91000abf76debd 100644 --- a/Doc/library/urllib.parse.rst +++ b/Doc/library/urllib.parse.rst @@ -225,6 +225,11 @@ or on combining URL components into a URL string. .. versionchanged:: 3.15 Added the *missing_as_none* parameter. + .. versionchanged:: 3.16 + Values for ``url`` and ``scheme`` other than strings, bytes, or ``None`` + 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 @@ -320,6 +325,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.16 + Values for ``qs`` and ``separator`` other than strings, bytes, or + ``None`` raise :exc:`TypeError` if true or :exc:`DeprecationWarning` if + false (to be changed to :exc:`TypeError` in future versions of Python). + .. function:: urlunsplit(parts) urlunsplit(parts, *, keep_empty) @@ -379,6 +389,11 @@ or on combining URL components into a URL string. .. versionchanged:: 3.15 Added the *keep_empty* parameter. + .. versionchanged:: 3.16 + Items in ``parts`` other than strings, bytes, or ``None`` 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) @@ -422,6 +437,11 @@ or on combining URL components into a URL string. Behavior updated to match the semantics defined in :rfc:`3986`. + .. versionchanged:: 3.16 + Values for ``base`` and ``url`` other than strings, bytes, or ``None`` + raise :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to + be changed to :exc:`TypeError` in future versions of Python). + .. function:: urldefrag(url, *, missing_as_none=False) @@ -452,6 +472,11 @@ or on combining URL components into a URL string. .. versionchanged:: 3.15 Added the *missing_as_none* parameter. + .. versionchanged:: 3.16 + Values other than other than strings, bytes, or ``None`` 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.16.rst b/Doc/whatsnew/3.16.rst index da98f1ad6b9bc3..686ec4c1e10d8f 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -680,6 +680,14 @@ New deprecations open them one by one instead. (Contributed by Serhiy Storchaka in :gh:`152638`.) +* :mod:`urllib`: + + * Providing anything but a string, bytes object, or ``None`` 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/pending-removal-in-3.17.rst diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index a5b7966c7780e9..fb20766c7a7935 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -1255,6 +1255,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, bytes, or None: got