adriank / ObjectPath

The agile query language for semi-structured data
http://objectpath.org
MIT License
380 stars 93 forks source link

TypeError when calling tree.execute() #86

Closed xuhcc closed 2 years ago

xuhcc commented 5 years ago

Case 1:

>>> tree = Tree({})
>>> tree.execute('1 in $.somekey')

  File "objectpath/core/interpreter.py", line 734, in execute
    ret = exe(tree)
  File "objectpath/core/interpreter.py", line 224, in exe
    return exe(node[1]) in exe(node[2])

TypeError: argument of type 'NoneType' is not iterable

Case 2:

>>> tree = Tree({'somekey': 1})
>>> tree.execute('1 in $.somekey')
TypeError: argument of type 'int' is not iterable

Case 3:

>>> tree = Tree({})
>>> tree.execute('count($.somekey) is 0')

  File "objectpath/core/interpreter.py", line 734, in execute
    ret = exe(tree)
  File "objectpath/core/interpreter.py", line 111, in exe
    return exe(node[1]) and exe(node[2])
  File "objectpath/core/interpreter.py", line 265, in exe
    ret = int(fst) == int(snd)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

It would be helpful to handle such cases gracefully (or throw a more specific exception), because quite often the exact structure of input data is not known.

ObjectPath version 0.6.1

adriank commented 5 years ago

PRs always welcomed :)

codejunction commented 5 years ago

for case 3: use {"somekey": 1} instead of {'somekey': 1} as it takes in json object and python dict doesn't work