← Back to team overview

yahoo-eng-team team mailing list archive

[Bug 1251653] Re: DictModel generates invalid python attribute names

 

** Changed in: neutron
       Status: Fix Committed => Fix Released

** Changed in: neutron
    Milestone: None => juno-1

-- 
You received this bug notification because you are a member of Yahoo!
Engineering Team, which is subscribed to neutron.
https://bugs.launchpad.net/bugs/1251653

Title:
  DictModel generates invalid python attribute names

Status in OpenStack Neutron (virtual network service):
  Fix Released

Bug description:
  neutron.agent.linux.dhcp.DictModel class generates objects which may
  have attributes with invalid names.

  On an instance of neutron.agent.linux.dhcp.Dnsmasq you have a network
  attribute which is a DictModel instance, this 'network' DictModel
  instance has attributes which do not respect the variable naming rules
  in python.

  i.e.:
  ipdb> dir(self.network)  # 'self' is an instance of neutron.agent.linux.dhcp.Dnsmasq
  ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_ns_name', 'admin_state_up', 'id', 'name', 'namespace', 'ports', 'provider:network_type', 'provider:physical_network', 'provider:segmentation_id', 'router:external', 'shared', 'status', 'subnets', 'tenant_id']

  As you can see, 'network.provider:network_type' is not a valid python
  attribute name and can only be accessed with getattr, so attribute
  access (as mentioned in DictModel's doc string) is therefore not
  guaranteed (attribute access on 'network.provider:network_type' would
  give a SyntaxError and getattr is compulsory to access it).

  Is there a reason why DictModel is not simply a subclass of dict with
  a __getattr__ method which would call __getitem__ on the dict? This
  would give attribute access to values "when the keys respect variable
  names", and dict keys which do not have the proper naming rules would
  still be accessible via the classical dict key access mechanism.

  example:

  >>> class DictModelExample(dict):
  >>>     def __getattr__(self, name):
  >>>         return self[name]
  >>>     def __setattr__(self, name, value):
  >>>         self[name] = value
  >>> 
  >>> d = {'a': 'foo'}
  >>> d = DictModelExample(d)
  >>> d.a
  ... 'foo'
  >>> d.b = 'bar'
  >>> d
  ... {'a': 'foo', 'b': 'bar'}

  off course, we would keep the part in the __init__ method which
  converts dict values to DictModel.

To manage notifications about this bug go to:
https://bugs.launchpad.net/neutron/+bug/1251653/+subscriptions