Thinking that maybe this is a bit where duck-typing might be preferable...e.g.

```python
try:
    from urllib.parse import quote as urllib_quote
except ImportError:
    from urllib import quote as urllib_quote
else:
    # Fall back to str when the first import succeeds
    unicode = str
```

And then

```python
def _quote_query(query):
    """Turn a dictionary into a query string in a URL, with keys
    in alphabetical order."""
    return "&".join("%s=%s" % (
        k, urllib_quote(
            unicode(query[k]).encode('utf-8'), safe='~'))
            for k in sorted(query))
```