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

crash when tring to replace None value in a list #92

Open
martinResearch opened this issue Aug 14, 2019 · 5 comments · May be fixed by #174
Open

crash when tring to replace None value in a list #92

martinResearch opened this issue Aug 14, 2019 · 5 comments · May be fixed by #174

Comments

@martinResearch
Copy link

a={}
dpath.util.new(a, ['b'], [])
dpath.util.new(a, ['b',3], 5)
dpath.util.new(a, ['b',1,"c"], 5)

creates error invalid literal for int() with base 10: 'c' lin 32 oin path.py ( result.append([path[-1], cur[int(path[-1])].class]))

martinResearch pushed a commit to martinResearch/dpath-python that referenced this issue Aug 14, 2019
adding possibility to use integer as list indices in newly created lists (dpath-maintainers#91) and fixing issue dpath-maintainers#92
@akesterson
Copy link
Collaborator

Can you check your code against te latest version and see if your issue is resolved?

@calebcase
Copy link
Collaborator

calebcase commented Dec 30, 2019

In 2.0 this happens because new will fill the first 3 array elements with None. None is a leaf so you can't add a new element under it. It is possible however to set the value to a dictionary and then proceed.

import dpath.util

a={}
print(a)

dpath.util.new(a, ['b'], [])
print(a)

dpath.util.new(a, ['b',3], 5)
print(a)

dpath.util.set(a, ['b',1], {})
print(a)

dpath.util.new(a, ['b',1,"c"], 5)
print(a)

Which outputs:

{}
{'b': []}
{'b': [None, None, None, 5]}
{'b': [None, {}, None, 5]}
{'b': [None, {'c': 5}, None, 5]}

You can avoid the situation entirely if you create lists in order.

@calebcase
Copy link
Collaborator

This potentially could be made more user friendly by instead filling with a sentinel value. new could then special case the sentinel and replace it as necessary with whatever is necessary (basically treating it as if it didn't exist).

@akesterson
Copy link
Collaborator

The 2.0 error message is significantly more comprehensible than the 1.x error message, which is a ValueError. According to the documentation, lists are created by new() up to the index given, and are filled with None, not more mappings. new() sees a string key and assumes you are trying to set a key in a mapping, and proceeds to give a list a character it can't index. ValueError results.

+1 for the sentinel value, I tried a few creative ways to do it with the functionality already in 1.0 and it was prohibitively difficult.

@akesterson akesterson reopened this Dec 30, 2019
@calebcase calebcase removed the 3.x label Jan 26, 2020
@akesterson
Copy link
Collaborator

Blocked by #136

@moomoohk moomoohk self-assigned this Dec 5, 2022
@moomoohk moomoohk linked a pull request Dec 5, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants