robshakir / pyangbind

A plugin for pyang that creates Python bindings for a YANG model.
Other
204 stars 121 forks source link

Boolean comparison on undefined leafrefs #361

Open ianbarrere opened 1 month ago

ianbarrere commented 1 month ago

Hello,

It seems that some values, like leafrefs which aren't defined, end up as a False sort of thing, but represented as a YANGBaseClass object. Trying to determine in python whether this value is referenced to something real or not is difficult, since python thinks something is there, but the thing is a False YANGBaseClass. The workaround I'm using for this is to string cast the object, which results in "False", and then compare that with "False". Seems like a silly a way to do it, so I'm wondering if there is a better way, like an actual boolean attribute on the YANGBaseClass, perhaps. Or maybe this is unintended behavior of undefined leafrefs, I'm not sure. Here is a basic reproduction:

module leafref {
  namespace "http://example.com/leafref";
  prefix "leafref";

  leaf-list test {
    type string;
  }
  container leafref-test {
    leaf test {
      type leafref {
        path "/test";
      }
    }
  }
}

And some code:

>>> import leafref
>>> binding = leafref.leafref()
>>> binding.get()
{'test': [], 'leafref-test': {'test': False}}
>>> binding.leafref_test.test
False
>>> type(binding.leafref_test.test)
<class 'pyangbind.lib.yangtypes.YANGDynClass.<locals>.YANGBaseClass'>
>>> binding.leafref_test.test is False
False
>>> str(binding.leafref_test.test) == "False"
True
>>>