ipython/ipynb

AttributeError in _filter_ast_node

alexlouden opened this issue · 1 comments

I was testing the following import:
import ipynb.fs.defs.utils.testfoo

and got the following exception:

... ipynb/fs/defs/__init__.py in <listcomp>(.0)
     39         if isinstance(node, ast.Assign):
---> 40             return all([t.id.isupper() for t in node.targets])

AttributeError: 'Subscript' object has no attribute 'id'

So I modified the _filter_ast_node to include a couple of try/excepts to see where it was failing:

        if isinstance(node, ast.Assign):
            try:
                return all([t.id.isupper() for t in node.targets])
            except AttributeError:
                print(ast.dump(node))
                for t in node.targets:
                    try:
                        t.id
                    except AttributeError:
                        print(ast.dump(t))
                raise

ast.dump(node):

Assign(targets=[Subscript(value=Name(id='df', ctx=Load()), slice=Index(value=Str(s='test')), ctx=Store())], value=Str(s='foo'))

ast.dump(t):

Subscript(value=Name(id='df', ctx=Load()), slice=Index(value=Str(s='test')), ctx=Store())

Looks like it's occurring on the following line of my notebook:

df['test'] = 'foo'

One possible solution:

    def _filter_ast_node(self, node):
        for an in ALLOWED_NODES:
            if isinstance(node, an):
                return True

        if isinstance(node, ast.Assign):
            return all(self._id_is_upper(t) for t in node.targets)

        return False

    @staticmethod
    def _id_is_upper(t):
        try:
            return t.id.isupper()
        except AttributeError:
            return False

Never mind, looks like f663f8f should fix this