Skip to content

dpath search not converting unknown datatypes to strings #147

@jgerry2002

Description

@jgerry2002

Given: dpath 2.0.1 on python 3.6.8

Example dictionary:
testdict = {'from': datetime.date(2018, 4, 15)}
dpath.util.search(testdict,"**/from*") returns:

Traceback (most recent call last):
  File "/home/test/lib64/python3.6/site-packages/dpath/segments.py", line 14, in kvs
    return iter(node.items())
AttributeError: 'datetime.date' object has no attribute 'items'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/test/lib64/python3.6/site-packages/dpath/util.py", line 232, in search
    return dpath.segments.fold(obj, f, {})
  File "/home/test/lib64/python3.6/site-packages/dpath/segments.py", line 349, in fold
    for pair in walk(obj):
  File "/home/test/lib64/python3.6/site-packages/dpath/segments.py", line 62, in walk
    for found in walk(v, location + (k,)):
  File "/home/test/lib64/python3.6/site-packages/dpath/segments.py", line 48, in walk
    for k, v in kvs(obj):
  File "/home/test/lib64/python3.6/site-packages/dpath/segments.py", line 16, in kvs
    return zip(range(len(node)), node)
TypeError: object of type 'datetime.date' has no len()

I'd suggest adding an option to be sure every value is parsed as a certain type. For example, json.dumps has the same issue. If I was using it I'd add a default option to interpret unknown things as strings as in:
Fails:
json.dumps(testdict,indent=1, sort_keys=True) gives me a similar object error

Works:
json.dumps(testdict,indent=1, sort_keys=True, default=str) then works since it forces unknown types to be altered to a string

I'd suggest a fix that would include an option like json.dumps:

Example:
dpath.util.search(testdict,"**/from*",default=str)
Would return:
{'from': 'datetime.date(2018, 4, 15)'}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions