[New-bugs-announce] [issue15824] mutable urlparse return type

mastahyeti report at bugs.python.org
Thu Aug 30 20:17:45 CEST 2012


New submission from mastahyeti:

This patch removes the inheritance from namedtuple and attempts to add the necessary methods to make it backwards compatible.

When parsing a url with urlparse.urlparse, the return type is non-mutable (named tuple). This is really inconvenient, because one of the most common (imop) use cases for urlparse is to parse a url, make an adjustment or change and then unparse it. Currently, something like this is required:

    import urlparse

    url = list(urlparse.urlparse('http://www.example.com/foo/bar?hehe=haha'))
    url[1] = 'python.com'
    new_url = urllib.urlunparse(url)

I think this is really clunky. Moving to a mutable return type is challenging because (to my knowledge) there are no types that are mutable and compatible with tuple. This patch removes the inheritance from namedtuple and attempts to add the necessary methods to make it backwards compatible. Does any one know of a better way to do this? It would be nice if there were a namedlist type that acted like namedtuple but was mutable. 

With these updates, urlparse can be used as follows:

    import urlparse

    url = list(urlparse.urlparse('http://www.example.com/foo/bar?hehe=haha'))
    url.netloc = 'www.python.com'
    urlparse.urlunparse(url)

I think this is much better. Let me know if you disagree...

Also, I ran the script through autopep8 because it was messy.

Also, I'm not sure if I'm supposed to duplicate this patch over to Python3. I can do that if necessary

----------
components: Library (Lib)
files: urlparse_patch.patch
keywords: patch
messages: 169474
nosy: mastahyeti
priority: normal
severity: normal
status: open
title: mutable urlparse return type
type: enhancement
versions: Python 2.7
Added file: http://bugs.python.org/file27063/urlparse_patch.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15824>
_______________________________________


More information about the New-bugs-announce mailing list