MAASClient.get() is reported to take a string, but raises an AssertionError

Bug #1418158 reported by Christian Reis
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
MAAS
Invalid
Wishlist
Unassigned

Bug Description

When writing up a response to http://askubuntu.com/questions/581444/how-to-query-maas-api-with-curl I noticed that you can't actually do a

    print client.get("nodes/", "list").read()

because it raises an AssertionError:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/apiclient/maas_client.py", line 235, in get
    url, headers = self._formulate_get(path, kwargs)
  File "/usr/lib/python2.7/dist-packages/apiclient/maas_client.py", line 194, in _formulate_get
    url = self._make_url(path)
  File "/usr/lib/python2.7/dist-packages/apiclient/maas_client.py", line 160, in _make_url
    assert not isinstance(path, bytes)
AssertionError

Forcing the caller to wrap the string in a unicode() seems silly. Note that the docstring for make_url says that strings are allowed:

    def _make_url(self, path):
        """Compose an absolute URL to `path`.

        :param path: Either a string giving a path to the desired resource,
            or a sequence of items that make up the path.
        :return: An absolute URL leading to `path`.
        """

Revision history for this message
Gavin Panella (allenap) wrote :

In MAAS we've been fairly strict with what we consider to be a string, getting ready for Python 3. Implicit encoding and decoding can also introduce failures and/or corruption. Try instead:

    client.get(u"nodes/", "list").read()

or put:

    from __future__ import unicode_literals

at the top of the module.

Changed in maas:
status: New → Invalid
Revision history for this message
Christian Reis (kiko) wrote : Re: [Bug 1418158] Re: MAASClient.get() is reported to take a string, but raises an AssertionError

On Wed, Feb 04, 2015 at 07:21:22PM -0000, Gavin Panella wrote:
> In MAAS we've been fairly strict with what we consider to be a string,
> getting ready for Python 3. Implicit encoding and decoding can also
> introduce failures and/or corruption.

While I agree with the general rule, this is a place where it really
doesn't add any value -- these are our URL paths we are talking about.
--
Christian Robottom Reis | [+1] 612 888 4935 | http://launchpad.net/~kiko
Canonical VP Hyperscale | [+55 16] 9 9112 6430

Revision history for this message
Christian Reis (kiko) wrote :

And besides, I find it nonsensical that in:

    client.get(u"nodes/", "list").read()

you think requiring the path be unicode is sensible, but the operation can be a string.

The get() docstring doesn't make you any wiser, either. So I think you're wrong wrong wrong :)

Changed in maas:
status: Invalid → New
Revision history for this message
Gavin Panella (allenap) wrote :

> And besides, I find it nonsensical that in:
>
> client.get(u"nodes/", "list").read()
>
> you think requiring the path be unicode is sensible, but the operation
> can be a string.

I missed adding the prefix to the operation string in my example, that's
all. In MAAS, every single module has (amongst other things):

    from __future__ import unicode_literals
    str = None

at the top. Byte strings **in MAAS** do not qualify as text strings. We
try to be careful about encoding and decoding explicitly. I'm sure we've
missed a few cases because Python 2 makes it trivially easy to do so.

>
> The get() docstring doesn't make you any wiser, either. So I think
> you're wrong wrong wrong :)

The assumption across the whole of MAAS is that strings are unicode, so
it's not specified everywhere. Users of MAAS's code need to treat it as
an all-unicode environment. If/when we migrate to Python 3, this will be
unavoidable.

Gavin Panella (allenap)
Changed in maas:
importance: Undecided → Wishlist
status: New → Triaged
Revision history for this message
Adam Collard (adam-collard) wrote :

This bug has not seen any activity in the last 6 months, so it is being automatically closed.

If you are still experiencing this issue, please feel free to re-open.

MAAS Team

Changed in maas:
status: Triaged → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.