Skip to content

fix: handle uuid.UUID conversion in query params#1643

Open
toroleapinc wants to merge 2 commits intoaio-libs:masterfrom
toroleapinc:fix/issue-1458-uuid-query-params
Open

fix: handle uuid.UUID conversion in query params#1643
toroleapinc wants to merge 2 commits intoaio-libs:masterfrom
toroleapinc:fix/issue-1458-uuid-query-params

Conversation

@toroleapinc
Copy link
Copy Markdown

This PR fixes the issue where uuid.UUID objects were being converted to integers instead of their string representation when used as query parameter values.

Problem

When passing a uuid.UUID object as a query parameter, yarl was converting it to an integer because UUID implements int(), making it match the SupportsInt protocol. This resulted in URLs like:

Expected: http://example.com?user_id=3199712f-1b78-4420-852b-a73ee09e6a8f
Actual: http://example.com?user_id=65928888857327045292976149998723820175

Solution

Added explicit handling for uuid.UUID objects in the query_var function to prioritize string conversion over integer conversion. The fix:

  1. Checks for isinstance(v, uuid.UUID) before isinstance(v, SupportsInt)
  2. Uses str(v) for UUID objects, which returns the canonical UUID string format
  3. Maintains backward compatibility for all other types

Changes

  • yarl/_query.py: Added uuid import and UUID handling in query_var()
  • tests/test_update_query.py: Added comprehensive UUID tests covering single values, multiple values, and mixed types
  • CHANGES/1458.bugfix.rst: Added changelog entry

Testing

Added test cases covering:

  • Single UUID in query parameters
  • Multiple UUID values for same parameter
  • UUID mixed with other data types
  • Both with_query() and update_query() methods

Fixes #1458

- Added explicit UUID handling to query_var function
- UUID objects now convert to string representation instead of integer
- Added comprehensive tests for UUID in query parameters
- Updated type hints to include uuid.UUID in SimpleQuery type
- Fixes issue where UUID('3199712f-1b78-4420-852b-a73ee09e6a8f')
  would become '65928888857327045292976149998723820175' instead
  of '3199712f-1b78-4420-852b-a73ee09e6a8f'

Closes aio-libs#1458
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Mar 14, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 14, 2026

Merging this PR will not alter performance

✅ 99 untouched benchmarks


Comparing toroleapinc:fix/issue-1458-uuid-query-params (a40a7ed) with master (148fc70)

Open in CodSpeed

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.48%. Comparing base (148fc70) to head (a40a7ed).

❌ Your project check has failed because the head coverage (97.64%) is below the target coverage (100.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1643   +/-   ##
=======================================
  Coverage   99.47%   99.48%           
=======================================
  Files          30       30           
  Lines        5942     5977   +35     
  Branches      283      284    +1     
=======================================
+ Hits         5911     5946   +35     
  Misses         22       22           
  Partials        9        9           
Flag Coverage Δ
CI-GHA 99.48% <100.00%> (+<0.01%) ⬆️
MyPy 97.64% <100.00%> (+0.01%) ⬆️
OS-Linux 99.71% <100.00%> (+<0.01%) ⬆️
OS-Windows 98.43% <100.00%> (+0.01%) ⬆️
OS-macOS 98.57% <100.00%> (+0.01%) ⬆️
Py-3.10.11 98.40% <100.00%> (+0.01%) ⬆️
Py-3.10.19 ?
Py-3.10.20 99.63% <100.00%> (?)
Py-3.11.14 99.27% <100.00%> (-0.36%) ⬇️
Py-3.11.15 99.39% <100.00%> (?)
Py-3.11.9 98.40% <100.00%> (+0.01%) ⬆️
Py-3.12.10 98.40% <100.00%> (+0.01%) ⬆️
Py-3.12.12 ?
Py-3.12.13 99.63% <100.00%> (+0.24%) ⬆️
Py-3.13.12 99.68% <100.00%> (+<0.01%) ⬆️
Py-3.13.12t 99.68% <100.00%> (+<0.01%) ⬆️
Py-3.14.3 99.68% <100.00%> (+<0.01%) ⬆️
Py-3.14.3t 99.68% <100.00%> (+<0.01%) ⬆️
Py-pypy3.10.16-7.3.19 99.30% <100.00%> (+<0.01%) ⬆️
VM-macos-latest 98.57% <100.00%> (+0.01%) ⬆️
VM-ubuntu-latest 99.71% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 98.43% <100.00%> (+0.01%) ⬆️
pytest 99.73% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

naarob pushed a commit to naarob/yarl that referenced this pull request Mar 26, 2026
…ry (aio-libs#1643), split_url delim perf

fix aio-libs#1630: URL.__repr__ exposed passwords in plaintext, risking credential
  leakage in logs and tracebacks. Added password masking with '********'.
  str(), == and all other operations remain unaffected.

fix aio-libs#1643: uuid.UUID was converted to int in query params because UUID
  implements __int__(), matching the SupportsInt protocol. Fixed by checking
  whether type(v).__str__ is not object.__str__ before using int() conversion.
  This is a general solution that works for any type with a meaningful __str__.

perf: split_url() delimiter search refactored from a for-loop over delim_chars
  to 3 conditional find() calls using the already-computed has_hash and
  has_question_mark flags. Reduces per-call overhead for URL-heavy workloads.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert an uuid.UUID variable into int when pass it in params

1 participant