This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Make urllib.parse.SplitResult etc arguments optional
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: martin.panter, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2015-02-09 04:03 by martin.panter, last changed 2022-04-11 14:58 by admin.

File name Uploaded Description Edit
split-result-default.patch martin.panter, 2015-02-10 05:05 review
Messages (3)
msg235584 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-02-09 04:03
This would be a simple API enhancement and would allow easier building of URLs, like

>>> SplitResult("rtp", address, query=urlencode(query)).geturl()

It seems the best way to do this at the moment is annoyingly verbose:

SplitResult("rtp", address, path="", query=urlencode(query), fragment="").geturl()

The way hinted by the documentation can leave an ugly empty query string:

>>> query = ()
>>> "rtp://%s?%s" % (address, urlencode(query))

This enhancement would also allow easy parsing of usernames, ports, etc:

>>> SplitResult(netloc="[::1]:0").hostname
>>> SplitResult(netloc="[::1]:0").port

Looking at the code, I think this could be implemented by adding an explicit constructor to each of the concrete classes, with arguments defaulting to "" or b"" as appropriate.
msg235664 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-02-10 05:05
Adding a patch implementing my suggested enhancement
msg256104 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-12-08 09:08
I agree that it would be nice to have. But this feature has a cost.

$ ./python -m timeit -s 'from urllib.parse import ParseResult' -- "ParseResult('http', '', '/doc/', '', 'query=yes', 'frag')"
Unpatched: 1.61 usec per loop
Patched:   2.1 usec per loop

$ ./python -m timeit -s 'from urllib.parse import urlparse' -- 'urlparse("")'

Unpatched: 8.87 usec per loop
Patched:   9.22 usec per loop

Is this cost significant?

The cost can be decreased by using global _tuple_new = tuple.__new__. But this couples the code too tight to implementation details of namedtuple.
Date User Action Args
2022-04-11 14:58:12adminsetgithub: 67604
2015-12-08 09:08:05serhiy.storchakasetnosy: + rhettinger, serhiy.storchaka
messages: + msg256104
2015-02-10 05:05:45martin.pantersetfiles: + split-result-default.patch
keywords: + patch
messages: + msg235664

versions: + Python 3.5
2015-02-09 04:03:57martin.pantercreate