Skip to content
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

Confusing behavior when using star globs and list indexing #166

Open
igordertigor opened this issue Nov 17, 2022 · 5 comments · Fixed by #172 · May be fixed by #173
Open

Confusing behavior when using star globs and list indexing #166

igordertigor opened this issue Nov 17, 2022 · 5 comments · Fixed by #172 · May be fixed by #173
Assignees

Comments

@igordertigor
Copy link

igordertigor commented Nov 17, 2022

When using dpath.util.search retrieves an empty list, it won't include it in the result:

>>> import dpath.util
>>> a = {'a': {'b': []}}
>>> b = {'a': {'b': [1, 2, 3]}}
>>> dpath.util.search(a, 'a/b/*')  # expecting {'a': {'b': []}} here
{}
>>> dpath.util.search(b, 'a/b/*', afilter=x > 3 if isinstance(x, int) else True)   # should again give {'a': {'b': []}}
{}
>>> dpath.util.search(a, 'a/b')
{'a': {'b': []}}

I find this behaviour counterintuitive: I tend to "pronounce" the star as "give me all items in this location". Hence if the item is an empty list, then I want that empty list. Am I misunderstanding something here? What would be the expected behaviour? Also, the final call illustrates something that I find particularly strange here: I would think of both globs to give the same results, but they don't seem to do so.

A second confusing thing seems to be the handling of numeric indexes for lists:

>>> dpath.util.search(b, 'a/b/0')
{'a': {'b': [1]}}
>>> dpath.util.search(b, 'a/b/-1')  # expecting {'a': {'b': [3]}} here.
{}

How would I pass negative indexes? Is that supported at all?

@moomoohk moomoohk self-assigned this Nov 30, 2022
@moomoohk moomoohk linked a pull request Dec 1, 2022 that will close this issue
@moomoohk
Copy link
Collaborator

moomoohk commented Dec 1, 2022

Hi @igordertigor,
Negative indexing is not supported at the moment. I am currently experimenting with this feature on #171.

I will also investigate the first issue you mentioned with the stars.

Regards

@moomoohk
Copy link
Collaborator

moomoohk commented Dec 2, 2022

@igordertigor
Please review the changes on #172 which add support for negative number indexing.

I've started investigating the other issue and it seems to be more problematic. I'm not sure I will be able to resolve it. Follow #173 for updates on that.

@moomoohk
Copy link
Collaborator

moomoohk commented Dec 4, 2022

Negative index suggestion also appears in #138.

@igordertigor
Copy link
Author

Thanks @moomoohk for looking into that. I didn't comment on #172 because it was already merged. Regarding #173, I'm not entirely sure if I understand what exactly the issue is. But that might be my limited insight into dpath's internals.

@moomoohk
Copy link
Collaborator

moomoohk commented Dec 8, 2022

Sorry the issue closed automatically. I'm reopening until the second issue is resolved.

@moomoohk moomoohk reopened this Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment