Skip to content

Commit 2872181

Browse files
author
Babata
committed
docs: note UUID-to-int coercion in with_query docs (#1458)
Types implementing __int__ (e.g. uuid.UUID) are converted to integers by the query parameter API because int is preferred over str for numeric coercion. Add a tip with a doctest showing how to cast to str to get the human-readable representation.
1 parent 148fc70 commit 2872181

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

CHANGES/1645.doc.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Added a note in the :meth:`~yarl.URL.with_query` documentation explaining
2+
that types implementing ``__int__`` (e.g. :class:`~uuid.UUID`) are
3+
converted to integers, and advising users to cast to :class:`str` when the
4+
human-readable representation is needed.

docs/api.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,20 @@ section generates a new :class:`URL` instance.
696696
Please see :ref:`yarl-bools-support` for the reason why :class:`bool` is not
697697
supported out-of-the-box.
698698

699+
.. tip::
700+
701+
Types that implement :meth:`~object.__int__` (e.g. :class:`~uuid.UUID`)
702+
are converted to integers because :class:`int` is preferred over
703+
:class:`str` for numeric coercion. If you need the human-readable
704+
representation, cast the value to :class:`str` before passing it:
705+
706+
.. doctest::
707+
708+
>>> import uuid
709+
>>> uid = uuid.UUID('3199712f-1b78-4420-852b-a73ee09e6a8f')
710+
>>> URL('http://example.com/').with_query({'id': str(uid)})
711+
URL('http://example.com/?id=3199712f-1b78-4420-852b-a73ee09e6a8f')
712+
699713
.. doctest::
700714

701715
>>> URL('http://example.com/path?a=b').with_query('c=d')

0 commit comments

Comments
 (0)