Bug in Python API "!=" doesn't behave as expected for base.Resource objects

Bug #1648207 reported by Chris Geroux
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-novaclient
Fix Released
Low
Akshil Verma

Bug Description

The result of some OpenStack objects, for example a server or a volume, do not compare with None as expected. For example if a server has been found and is clearly not None (e.g. I can print out its name) the test "server!=None" will be False. This really isn't the expected behavior. I can worked around this by using the "server is not None" but this would make bugs appear very easily if I slip back into "!=" which is my usual habit, and could be other's habit as well. I have attached a simple script which should reproduce the bug.

Revision history for this message
Chris Geroux (cgeroux) wrote :
Revision history for this message
Matt Riedemann (mriedem) wrote :

When you say 'objects' do you mean nova.objects.* objects? The attachment doesn't load for me.

summary: - Bug in Python API "!=" doesn't behave as expected
+ Bug in Python API "!=" doesn't behave as expected for versioned objects
Revision history for this message
Matt Riedemann (mriedem) wrote : Re: Bug in Python API "!=" doesn't behave as expected for versioned objects

I'm not really clear on this one, especially with the attachment not loading for me. But:

>>> from nova.objects import instance as instance_obj
>>> import uuid
>>> instance = instance_obj.Instance(uuid=str(uuid.uuid4()))
>>> instance.uuid
'2d066a8c-303a-48eb-8d65-f9233c77c08b'
>>> instance is None
False
>>> instance == None
False
>>> instance != None
True
>>>

Revision history for this message
Matt Riedemann (mriedem) wrote :

The script loads now, and I see you're talking about novaclient, not nova.

no longer affects: nova
Revision history for this message
Matt Riedemann (mriedem) wrote :

What version of novaclient?

This is the base class for novaclient resource objects:

https://github.com/openstack/python-novaclient/blob/master/novaclient/base.py#L118

And it implements __eq__:

https://github.com/openstack/python-novaclient/blob/master/novaclient/base.py#L202

Revision history for this message
Matt Riedemann (mriedem) wrote :

OK I guess I see the point here:

>>> from novaclient.v2 import servers
>>> server = servers.Server(manager=None, info={'id': 1, 'name': 'test'})
>>> server
<Server: test>
>>> server is None
False
>>> server == None
False
>>> server != None
False
>>> server is not None
True
>>>

no longer affects: oslo.versionedobjects
summary: - Bug in Python API "!=" doesn't behave as expected for versioned objects
+ Bug in Python API "!=" doesn't behave as expected for base.Resource
+ objects
Changed in python-novaclient:
status: New → Confirmed
importance: Undecided → Low
Revision history for this message
Chris Geroux (cgeroux) wrote :

I know there are a few ways to implement things, v2/v3 etc. and what I attached was only run in a very specific situation so I could easily see how it might not work with a different openstack setup. What I do in principle is a little more complex to handle v2/v3 authentication but I wanted to keep the number of code lines short.

Even so it looks like you identified the issue with the line "server!=None" producing the "False" result even when the "server is not None". I suspected it had to do with an implementation of the "__eq__" for base.py. I am assuming that other projects such as "cinder" etc. also use the same or very similar "base.py" definition for "__eq__".

Akshil Verma (akshilv)
Changed in python-novaclient:
assignee: nobody → Akshil (akshilv)
Akshil Verma (akshilv)
Changed in python-novaclient:
status: Confirmed → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to python-novaclient (master)

Fix proposed to branch: master
Review: https://review.openstack.org/409549

Akshil Verma (akshilv)
Changed in python-novaclient:
status: In Progress → Fix Committed
status: Fix Committed → In Progress
Revision history for this message
Akshil Verma (akshilv) wrote :

Hi Guys,
I am new to openstack and took this bug as my first contribution to openstack. I have one doubt.
I have made the necessary changes and the code review has been accepted. Do I change the status to Fix committed or will that happen automatically?

Changed in python-novaclient:
status: In Progress → Fix Committed
status: Fix Committed → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to python-novaclient (master)

Reviewed: https://review.openstack.org/409549
Committed: https://git.openstack.org/cgit/openstack/python-novaclient/commit/?id=78e621faf757e3bf4bd8475c29090bb1c83bc0cd
Submitter: Jenkins
Branch: master

commit 78e621faf757e3bf4bd8475c29090bb1c83bc0cd
Author: Akshil Verma <email address hidden>
Date: Sun Dec 11 13:37:25 2016 -0600

    Fixed the __ne__ implementation in base.Resource

    Any object of Resource class or its child class do not compare with
    None as expected. For example if a server has been found and is
    clearly not None, the test "server!=None" will be False.

    This was occuring because the __eq__ implementation was returning the
    'NotImplemented' keyword and the __ne__ implementation was returning
    the not of __eq__, which in this case will return False as the
    expected python behavior for a the not of NotImplemented is False.

    Changed the __ne__ implementation to return the correct boolean value
    and added the test case that fails with the older implementation and
    passes in the current fix.

    Change-Id: I6bf5a6e9c9eed4bbcf6678467df19dfea560b4de
    Closes-Bug: #1648207

Changed in python-novaclient:
status: In Progress → Fix Released
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/python-novaclient 7.1.0

This issue was fixed in the openstack/python-novaclient 7.1.0 release.

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.