mbj4668 / pyang

An extensible YANG validator and converter in python
ISC License
530 stars 343 forks source link

Incorrect "module not used" warning when the only use of a module is in a must() #777

Closed fperrin closed 2 years ago

fperrin commented 2 years ago

Hi,

pyang gives a warning imported module "b" not used when the only use of the b module is in a somewhat complex must expression such as: must "not(deref(.)/a:gizmo-attrs/b:b-attributes)". If I add to the same module an expression such as must "not(/a:gizmos/a:gizmo-attrs/b:b-attribute)", then I no longer get that warning.

I added a testcase to github.com/fperrin/pyang branch fred-pyang-unused-module-warning, that is: https://github.com/fperrin/pyang/tree/fred-pyang-unused-module-warning/test/test_issues/test_unused_prefix

Or if that's easier:

a.yang

module a {
  namespace "urn:a";
  prefix a;

  list gizmos {
    key "gizmo-name";

    leaf gizmo-name {
      type string;
    }

    container gizmo-attrs {
      // to be augmented by other modules
    }
  }
}

b.yang

module b {
  namespace "urn:b";
  prefix b;

  import a {
    prefix a;
  }

  augment /a:gizmos/a:gizmo-attrs {
    leaf b-attribute {
      type boolean;
    }
  }
}

c.yang

module c {
  namespace "urn:c";
  prefix c;

  import a {
    prefix a;
  }

  import b {
    prefix b;
  }

  list c-widget {
    key "widget-name";

    leaf widget-name {
      type string;
    }

    // to define a C widget, we need an A gizmo, but those with b-attributes are not usable
    leaf a-gizmo {
      type leafref {
        path "/a:gizmos/a:gizmo-name";
      }

      must "not(deref(.)/a:gizmo-attrs/b:b-attributes)" {
        error-message "Cannot use a gizmo that has b-attributes";
      }
    }
  }
}
fperrin commented 2 years ago

Thank you @mbj4668 , it works now!