On 07/08/14 12:15, Matthew Booth wrote: > I'm sure this is well known, but I recently encountered this problem for > the second time. > > --- > foo: > import oslo.config as cfg > > import bar > > CONF = cfg.CONF > CONF.register_opts('foo_opt') > > --- > bar: > import oslo.config as cfg > > CONF = cfg.CONF > > def bar_func(arg=CONF.foo_opt): > pass > --- > > importing foo results in an error in bar because CONF.foo_opt doesn't > exist. This is because bar is imported before CONF.register_opts. > CONF.import_opt() fails in the same way because it just imports foo and > hits the exact same problem when foo imports bar. > > A (the?) solution is to register_opts() in foo before importing any > modules which might also use oslo.config. This also allows import_opt() > to work in bar, which you should do to remove any dependency on import > order: > > --- > foo: > import oslo.config as cfg > > CONF = cfg.CONF > CONF.register_opts('foo_opt') > > import bar > > --- > bar: > import oslo.config as cfg > > CONF = cfg.CONF > CONF.import_opt('foo_opt', 'foo') > > def bar_func(arg=CONF.foo_opt): > pass > --- > > Even if it's old news it's worth a refresher because it was a bit of a > headscratcher.
This became pertinent, because it's now blocking: https://review.openstack.org/#/c/104145/. It has a -1 for not following the style guidelines in the ordering of imports. I think we should update our style guidelines to recommend that any module which calls register_opts() or import_opt() do so *before* importing another module which might also call one of those functions. If this is done consistently, it means that any module can import_opt() from another module and be certain that it won't be re-imported itself before register_opts() has been called. Matt -- Matthew Booth Red Hat Engineering, Virtualisation Team Phone: +442070094448 (UK) GPG ID: D33C3490 GPG FPR: 3733 612D 2D05 5458 8A8A 1600 3441 EA19 D33C 3490 _______________________________________________ OpenStack-dev mailing list OpenStackfirstname.lastname@example.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev