Skip to content

Commit bffbd32

Browse files
Allow NoneType
1 parent 2bf57f3 commit bffbd32

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

Doc/library/urllib.parse.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ or on combining URL components into a URL string.
221221
Added the *missing_as_none* parameter.
222222

223223
.. versionchanged:: 3.15
224-
Values for ``url`` and ``scheme`` other than strings or bytes raise
225-
:exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be
226-
changed to :exc:`TypeError` in future versions of Python).
224+
Values for ``url`` and ``scheme`` other than strings, bytes, or ``None``
225+
raise :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to
226+
be changed to :exc:`TypeError` in future versions of Python).
227227

228228
.. _WHATWG spec: https://url.spec.whatwg.org/#concept-basic-url-parser
229229

@@ -321,9 +321,9 @@ or on combining URL components into a URL string.
321321
separator key, with ``&`` as the default separator.
322322

323323
.. versionchanged:: 3.15
324-
Values for ``qs`` and ``separator`` other than strings or bytes raise
325-
:exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be
326-
changed to :exc:`TypeError` in future versions of Python).
324+
Values for ``qs`` and ``separator`` other than strings, bytes, or
325+
``None`` raise :exc:`TypeError` if true or :exc:`DeprecationWarning` if
326+
false (to be changed to :exc:`TypeError` in future versions of Python).
327327

328328

329329
.. function:: urlunsplit(parts)
@@ -385,7 +385,7 @@ or on combining URL components into a URL string.
385385
Added the *keep_empty* parameter.
386386

387387
.. versionchanged:: 3.15
388-
Items in ``parts`` other than strings or bytes raise
388+
Items in ``parts`` other than strings, bytes, or ``None`` raise
389389
:exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be
390390
changed to :exc:`TypeError` in future versions of Python).
391391

@@ -433,9 +433,9 @@ or on combining URL components into a URL string.
433433
Behavior updated to match the semantics defined in :rfc:`3986`.
434434

435435
.. versionchanged:: 3.15
436-
Values for ``base`` and ``url`` other than strings or bytes raise
437-
:exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be
438-
changed to :exc:`TypeError` in future versions of Python).
436+
Values for ``base`` and ``url`` other than strings, bytes, or ``None``
437+
raise :exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to
438+
be changed to :exc:`TypeError` in future versions of Python).
439439

440440

441441
.. function:: urldefrag(url, *, missing_as_none=False)
@@ -468,7 +468,7 @@ or on combining URL components into a URL string.
468468
Added the *missing_as_none* parameter.
469469

470470
.. versionchanged:: 3.15
471-
Values other than strings or bytes raise
471+
Values other than other than strings, bytes, or ``None`` raise
472472
:exc:`TypeError` if true or :exc:`DeprecationWarning` if false (to be
473473
changed to :exc:`TypeError` in future versions of Python).
474474

Doc/whatsnew/3.15.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,9 +1763,10 @@ New deprecations
17631763

17641764
* :mod:`urllib`:
17651765

1766-
* Providing anything but a string or bytes object to :mod:`urllib.parse`
1767-
functions expecting strings or bytes now raises :exc:`DeprecationWarning`
1768-
if the value tests false, or :exc:`TypeError` if it tests true.
1766+
* Providing anything but a string, bytes object, or ``None`` to
1767+
:mod:`urllib.parse` functions expecting strings or bytes now raises
1768+
:exc:`DeprecationWarning` if the value tests false, or :exc:`TypeError` if
1769+
it tests true.
17691770
(Contributed by Jacob Walls in :issue:`19094`.)
17701771

17711772
* ``__version__``

Lib/urllib/parse.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,21 @@ def _coerce_args(*args):
124124
# - noop for str inputs
125125
# - encoding function otherwise
126126
str_input = None
127+
empty_values = {"", b"", None}
127128
for arg in args:
128129
if arg:
129130
if str_input is None:
130131
str_input = isinstance(arg, str)
131132
else:
132133
if isinstance(arg, str) != str_input:
133134
raise TypeError("Cannot mix str and non-str arguments")
134-
elif str_input is False and not hasattr(arg, 'decode'):
135-
if arg:
136-
raise TypeError(f"Expected a string or bytes object: got {type(arg)}")
137-
else:
138-
warnings.warn(
139-
f"Providing false values other than strings or bytes "
140-
f"to urllib.parse is deprecated: got {type(arg)}",
141-
DeprecationWarning, stacklevel=3)
135+
if str_input is False and arg is not None and not hasattr(arg, "decode"):
136+
raise TypeError(f"Expected a string, bytes, or None: got {type(arg)}")
137+
elif arg not in empty_values:
138+
warnings.warn(
139+
f"Providing false values other than empty strings, bytes, or"
140+
f"None to urllib.parse is deprecated: got {type(arg)}",
141+
DeprecationWarning, stacklevel=3)
142142
if str_input is None:
143143
for arg in args:
144144
if arg is not None:

0 commit comments

Comments
 (0)