Skip to content

URL.build: reject boolean port values#1772

Open
HrachShah wants to merge 3 commits into
aio-libs:masterfrom
HrachShah:fix/url-build-reject-bool-port
Open

URL.build: reject boolean port values#1772
HrachShah wants to merge 3 commits into
aio-libs:masterfrom
HrachShah:fix/url-build-reject-bool-port

Conversation

@HrachShah

@HrachShah HrachShah commented Jun 26, 2026

Copy link
Copy Markdown

What do these changes do?

Reject boolean port values in URL.build(port=...) with a clear TypeError at the call site, matching the behaviour of URL.with_port. The previous isinstance(port, int) guard accepted True and False because bool is a subclass of int; the bool then flowed into make_netloc and surfaced as ValueError: Invalid URL: port can't be converted to integer from inside the netloc assembly one frame deeper than the call site, with the wrong exception type.

Are there changes in behavior for the user?

Yes. URL.build(port=True) and URL.build(port=False) now raise TypeError at the call site instead of leaking a confusing ValueError from deeper in the netloc assembly. Real int ports and port=None are unaffected; only True and False change behaviour, and they were never producing a meaningful URL before.

Is it a substantial burden for the maintainers to support this?

No. The fix is two lines in one function. A regression test (test_build_rejects_non_int_port) is included that fails on the original guard and passes with the fix.

Related issue number

None.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder

Drafted with MiniMax M3; reviewed by keenrose.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jun 26, 2026
@codspeed-hq

codspeed-hq Bot commented Jun 26, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 9.84%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 96 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
test_url_build_encoded_with_host_and_port 254.4 µs 282.1 µs -9.84%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing HrachShah:fix/url-build-reject-bool-port (6e0388a) with master (95ffe17)

Open in CodSpeed

The bool-rejecting fix in URL.build rewrote the port guard to raise
'port should be int or None, got <type>' instead of 'The port is
required to be int, got <type>'. The pre-existing port-str parameter
case in test_build_with_port still matched the old text, so a
URL.build(port='') invocation raised the new message and the test
failed at the regex check. Update the test to match the new message
shape. The new test_build_rejects_non_int_port already covers the
bool, str, and float cases introduced by the same source change.
@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.86%. Comparing base (b0d27e4) to head (6e0388a).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1772   +/-   ##
=======================================
  Coverage   99.86%   99.86%           
=======================================
  Files          21       21           
  Lines        4310     4319    +9     
  Branches      252      252           
=======================================
+ Hits         4304     4313    +9     
  Misses          4        4           
  Partials        2        2           
Flag Coverage Δ
CI-GHA 99.79% <100.00%> (+<0.01%) ⬆️
OS-Linux 99.79% <100.00%> (+<0.01%) ⬆️
OS-Windows 98.54% <100.00%> (+<0.01%) ⬆️
OS-macOS 98.72% <100.00%> (+<0.01%) ⬆️
Py-3.10 99.69% <100.00%> (+<0.01%) ⬆️
Py-3.11 99.69% <100.00%> (+<0.01%) ⬆️
Py-3.12 99.69% <100.00%> (+<0.01%) ⬆️
Py-3.13 99.74% <100.00%> (+<0.01%) ⬆️
Py-3.14 99.74% <100.00%> (+<0.01%) ⬆️
Py-3.14t 99.74% <100.00%> (+<0.01%) ⬆️
Py-pypy-3.11 99.37% <100.00%> (+<0.01%) ⬆️
VM-macos-latest 98.72% <100.00%> (+<0.01%) ⬆️
VM-ubuntu-latest 99.79% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 98.54% <100.00%> (+<0.01%) ⬆️
pytest 99.79% <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 Harness.
📢 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.

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.

1 participant