Skip to content

Fixing dpath to work for any type of leaf nodes #122

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

Closed
wants to merge 1 commit into from

Conversation

nayyarv
Copy link

@nayyarv nayyarv commented Mar 3, 2020

from decimal import Decimal
ts = {
    "foo" :{
        "dude": Decimal(1.5)
    }
}
>>> dpath.util.get(ts, "foo/dude")
TypeError: object of type 'decimal.Decimal' has no len()

Given that leaf checks for a finite number of types any kind of odd type such as a function or custom object will simply not work in this dictionary. I've suggested an approach based on duck-typing, in which I check if the object has a __getitem__ instead of checking particular type (though I do that too for the case of string)

I have a couple of places I wasn't sure about and would love some opinions.

  1. When you have an object with a __getitem__ but no items or len the kvs method breaks. I've kind of just returned an empty iterator here, but it should be accessible via keys and doesn't really need to have a walk run on it if there is no glob for that field. I currently just treat it as a leaf, but maybe this might be addressed with some kind of warning?
  2. I also was worried about catching a generic Exception in thing[0] since it could easily be either an indication the object is mappable, but just uses a custom Exception and might have a len/items implemented. I've let it stand right now, but am open to change.

@nayyarv
Copy link
Author

nayyarv commented Mar 5, 2020

Another point of concern I had was the fact that we could have an object that implemenets getitem but not items or len.

Arguably since obj["key"] works, doing dpath.utils.get(obj, "key") should work, but it won't since it has to do a walk despite having no globs in the path.

I've left this unfixed, but added an expected fail test in the meantime. I think it's fair to require some iteration protocol when there's a glob for the current part, but otherwise it should be able to work.

calebcase added a commit that referenced this pull request Mar 29, 2020
This relates to:

#119

And incorporates some of the changes from:

#120
#122
#123
calebcase added a commit that referenced this pull request Mar 29, 2020
This relates to:

#119

And incorporates some of the changes from:

#120
#122
#123
"el2" : MappableButNotIterable()
}
}
try:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Split this into a new issue: #127

@nayyarv
Copy link
Author

nayyarv commented Apr 4, 2020

Closing this since there is another PR fixing the base issue.

@nayyarv nayyarv closed this Apr 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants