openshift/openshift-restclient-python

Comparing ResourceField results in "AttributeError: 'dict' object has no attribute '__dict__'"

mmirecki opened this issue · 8 comments

Comparing a ResourceField (openshift.dynamic.client.ResourceField) to a dict object results in:
"AttributeError: 'dict' object has no attribute 'dict'"

This is because ResourceField defines:

def __eq__(self, other):
    return self.__dict__ == other.__dict__

but a dict has no __dict__ attribute.
This makes comparison to a dict impossible (I assume this is not intentional?)

We're using the openshift-restclient-python for tests, in which verify data retrieved from openshift (using the restclient) to data retrieved from files (json/yaml), which we have in the form of a dict after reading it .

There could be a few ways to solve this.
The simplest, would be just to modify __eq__ to accomodate dict comparison:

def __eq__(self, other):
    if type(other) == dict
        return self.__dict__== other
    return self.__dict__ == other.__dict__

Another would be adding a ResourceField.to_dict function, taking care of nested ResourceFields too.

Please advise on the recommended approach (I'll be happy to provide a patch then).

Hmm, I don't know if we want to allow comparing ResourceField to dict directly, but we should at least be checking the type and failing with a TypeError. I believe the ResourceField object supports being converted to dict by just calling dict, ie

>>> rf = ResourceField(a='b')
>>> dict(rf)
{'a': 'b'}
>>> dict(rf) == {'a': 'b'}
True

Clearly the documentation around that is lacking though, sorry about that

ResourceField does not support being converted to dict by just calling dict.
It will only work as long as there are no nested ResourceField's in it.
Changing the conversion to handle nested ResourceField's was one of the proposed solutions in the first post.

Sorry for the delayed response, I was out at Ansiblefest.

It will only work as long as there are no nested ResourceField's in it.

Ah my mistake, in that case we should definitely either fix that behavior ie, make dict() recursively resolve, or by adding a to_dict method as the other resources have. I'd be ok with either approach. If you're still up for making the patch let me know, otherwise I can add it to my backlog

Hi, wondering if there's any update on this? I'd also be interested in the ResourceField.to_dict() or recursive dict() call functionality.

Issues go stale after 90d of inactivity.

Mark the issue as fresh by commenting /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.
Exclude this issue from closing by commenting /lifecycle frozen.

If this issue is safe to close now please do so with /close.

/lifecycle stale

Stale issues rot after 30d of inactivity.

Mark the issue as fresh by commenting /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.
Exclude this issue from closing by commenting /lifecycle frozen.

If this issue is safe to close now please do so with /close.

/lifecycle rotten
/remove-lifecycle stale

Rotten issues close after 30d of inactivity.

Reopen the issue by commenting /reopen.
Mark the issue as fresh by commenting /remove-lifecycle rotten.
Exclude this issue from closing again by commenting /lifecycle frozen.

/close

@openshift-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.

Reopen the issue by commenting /reopen.
Mark the issue as fresh by commenting /remove-lifecycle rotten.
Exclude this issue from closing again by commenting /lifecycle frozen.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.