Andrius
Andrius

Reputation: 21168

Python: NoneType object has no attribute __getitem__. But its not nonetype

So I have this piece of code:

    key = val_cfg['src_model']
    print "key: ", key
    print "objects dict: ", objects
    print "before Nonetype"
    print "accessing objects dict: ", objects[key]  # line 122
    print "after"
    method = self._handle_object(
        val_cfg, objects[val_cfg['src_model']])

So the output I get is this:

key:  ir.model.data
objects dict:  {'res.partner': res.partner(22,), 'ir.model.data': ir.model.data()}
before Nonetype
accessing objects dict:  ir.model.data()
after

And then I get error:

_report.tests.test_vat_report: `   File "/home/user/addons/account_vat_report/models/vat_report.py", line 122, in _handle_method
2016-10-03 11:32:44,863 31650 ERROR vat_reports openerp.addons.account_vat_report.tests.test_vat_report: `     print "accessing objects dict: ", objects[key]
2016-10-03 11:32:44,863 31650 ERROR vat_reports openerp.addons.account_vat_report.tests.test_vat_report: ` TypeError: 'NoneType' object has no attribute '__getitem__'

Well this does not make sense. I print results of line 122, but test fails, saying its NoneType object. How so? I'm probably missing something here. Does someone see whats wrong here?

Update. definition of _handle_object:

@api.model
def _handle_object(self, val_cfg, obj):
    """Method to get value from object."""
    # check if value is list or tuple of strings.
    if not isinstance(val_cfg['value'], basestring):
        for val in val_cfg['value']:
            value = self._get_attribute(obj, val_cfg['value'])
            # return first value that was retrieved.
            if value:
                return value
        # if we do not get any "True" value, just return last
        # one.
        else:
            return value
    else:
        return self._get_attribute(obj, val_cfg['value'])

Upvotes: 1

Views: 3149

Answers (1)

Andrius
Andrius

Reputation: 21168

Well I made stupid mistake, though that error got me confused, because of traceback. So I was looking in a wrong direction.

The problem was in another method, that is used to update objects dictionary. Its purpose is to update value for specific key, if some current iter item (from iteration) needs to be assigned if specified in configuration.

Anyway, the problem was this (in another method called _set_dynamic_rows_data):

objects = self._update_objects_dict(
                        val_cfg, objects, iter_)

I accidentally assigned objects dict to this method and because this method does not return anything, objects dict would be set to None. And it was disguised with that error, because first iteration, everything would be OK and during second iteration, when objects dict would be changed, it then would start failing like described in question.

Upvotes: 1

Related Questions