[openstack-dev] [nova][oslo] oslo.config and import chains
Matthew Booth
mbooth at redhat.com
Thu Aug 7 16:41:06 UTC 2014
On 07/08/14 17:11, Kevin L. Mitchell wrote:
> On Thu, 2014-08-07 at 10:55 -0500, Matt Riedemann wrote:
>>
>> On 8/7/2014 10:27 AM, Kevin L. Mitchell wrote:
>>> On Thu, 2014-08-07 at 12:15 +0100, Matthew Booth wrote:
>>>> A (the?) solution is to register_opts() in foo before importing any
>>>> modules which might also use oslo.config.
>>>
>>> Actually, I disagree. The real problem here is the definition of
>>> bar_func(). The default value of the parameter "arg" will likely always
>>> be the default value of foo_opt, rather than the configured value,
>>> because "CONF.foo_opt" will be evaluated at module load time. The way
>>> bar_func() should be defined would be:
>>>
>>> def bar_func(arg=None):
>>> if not arg:
>>> arg = CONF.foo_opt
>>> …
>>>
>>> That ensures that arg will be the configured value, and should also
>>> solve the import conflict.
>>>
>>
>> Surely you mean:
>>
>> if arg is not None:
>>
>> right?! I'm pretty sure there is a hacking check for that now too...
>
> No, I meant "if not arg", which would be true if arg is None—or 0, or
> empty string, or False. If those alternate false values are potential
> values of arg, then clearly an "if arg is None" would be the correct
> incantation. However, an "if arg is not None" would never be
> appropriate for this logic :) (And the hacking check is against "if arg
> == None" or "if arg != None"…)
... or arg is an object which defines __nonzero__(), or defines
__getattr__() and then explodes because of the unexpected lookup of a
__nonzero__ attribute. Or it's False (no quotes when printed by the
debugger), but has a unicode type and therefore evaluates to True[1].
However, if you want to compare a value with None and write 'foo is
None' it will always do exactly what you expect, regardless what you
pass to it. I think it's also nicer to the reviewer and the maintainer,
who then don't need to go looking for context to check if anything
invalid might be passed in.
Matt
[1] I actually hit this. I still don't understand it.
--
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
More information about the OpenStack-dev
mailing list