Python: NoneType object has no attribute __getitem__. But its not nonetype
<p>So I have this piece of code:</p>

<pre><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']])
</code></pre>

<p>So the output I get is this:</p>

<pre><code>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
</code></pre>

<p>And then I get error:</p>

<pre><code>_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__'
</code></pre>

<p>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?</p>

<p><strong>Update</strong>.
definition of <code>_handle_object</code>:</p>

<pre><code>@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'])
</code></pre>

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

<p>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.</p>

<p>Anyway, the problem was this (in another method called <code>_set_dynamic_rows_data</code>):</p>

<pre><code>objects = self._update_objects_dict(
                        val_cfg, objects, iter_)
</code></pre>

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

python python-2.7 dictionary nonetype