CZ-NIC / yangson

GNU Lesser General Public License v3.0
53 stars 20 forks source link

Type error during Semantic Error resporting #95

Closed christian-herber closed 3 years ago

christian-herber commented 3 years ago

When validating data, I am sometimes faced with the situation that the expression passed to the SematicError actually raises another error:

  File "...\site-packages\yangson\schemanode.py", line 965, in _validate
    raise SemanticError(inst.json_pointer(expand_keys=True), "instance-required")
  File "...\site-packages\yangson\instance.py", line 199, in json_pointer
    res.insert(0, inst.schema_node.name + '=' + inst.value)
TypeError: can only concatenate str (not "int") to str

This could be an issue in other functionalities also, but here it is a bit annoying, because the Semantic Error cannot be reported. In this specific case, I have a LeafList with LeafRefs to an Integer node.

My proposal would be to change

res.insert(0, inst.schema_node.name + '=' + inst.value)

to

res.insert(0, '{}={}'.format(inst.schema_node.name, inst.value))
llhotka commented 3 years ago

This is related to #91 and has to do with the new expand_keys argument of the json_pointer method.

My proposal is to remove this argument, but also modify all instance-related exceptions (such as SematicError) to have the offending instance as its member, instead of path (JSON pointer). An exception handler could then inspect the instance directly.

@kwatsen @HRogge any thoughts on this?

llhotka commented 3 years ago

Fixed by PR #97 (merged in 1.4.0)