Skip to content

Fixed get_component method in Field to get working with subclassess of collections.Mapping #2140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2014
Merged

Conversation

sicarrots
Copy link

Method get_components checks if object is instance of dictionary, but it fails when custom dict-alike implementations (especially subclasses of collections.Mapping) are used.
This commit adds checking for presence of dictionary interface methods on obj in method get_components.

hasattr(obj, '__getitem__') and
hasattr(obj, '__len__') and
hasattr(obj, '__iter__') and
hasattr(obj, 'get')):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super-keen on this. Can we use isinstance(obj, (dict, Mapping)) instead? Any other cleaner options?

@tomchristie
Copy link
Member

Would be happy to support Mapping, and in favor of this PR in general, tho implementation needs minor tweaking. Not sure we should consider it in scope to accept any arbitrary object that implements some of the dict like interface, as that'd get super-unspecified.

@sicarrots
Copy link
Author

I've just updated pr with this check: isinstance(obj, (dict, Mapping)).
But generally i'm not sure if it covers all similar cases.

def __iter__(self):
return iter(self._data)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is just a test case, can we stub this out to just __getitem__?
And to also just do self._data = data, ignoring the None case?
Those codepaths won't ever be run, so be better to drop them.
Let's also include a docstring explaining the motivation of the class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abstract base class Mapping forces to define all these methods - __getitem__, __len__, __iter__.
Yes, we can ignore None case, i'll update pr shortly.

@tomchristie
Copy link
Member

You know what? Let's just drop the test case. Contrary to popular opinion code that makes sense is actually sufficient, we don't need to test every little edge case and the 'isinstance' check is obvious and correct. Let's just have the code change for this.

@@ -52,7 +53,7 @@ def get_component(obj, attr_name):
Given an object, and an attribute name,
return that attribute on the object.
"""
if isinstance(obj, dict):
if isinstance(obj, (dict, collections.Mapping)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the documentation, dict is a subclass of Mapping, so isinstance(obj, collections.Mapping) should be sufficient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, and confirmed this is true all the way down to 2.6

@tomchristie
Copy link
Member

Just noticed that this one is for 2.x.
Milestoning as 2.4.5 now. There's no promise that we will roll a new 2.x release, but it is possible.
Once the test has been dropped and the isinstance has been changes to just collections.Mapping I think this'd be reasonable.

@tomchristie tomchristie added this to the 2.4.5 Release milestone Nov 27, 2014
@sicarrots
Copy link
Author

Ok, so i've updated pull request according to proposed changes.
But personally i'm against dropping test for this case. Even if such test don't increase coverage, it prevents from accidental change.

tomchristie added a commit that referenced this pull request Nov 27, 2014
Fixed get_component method in Field to get working with subclassess of collections.Mapping
@tomchristie tomchristie merged commit 67ae6b2 into encode:version-2.4.x Nov 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants