PyCQA / redbaron

Bottom-up approach to refactoring in python
http://redbaron.pycqa.org/
699 stars 74 forks source link

Parsing error when a class defined with metaclass keyword #192

Open sjiang1 opened 5 years ago

sjiang1 commented 5 years ago

The file, test3.py, being parsed:

from abc import ABCMeta, abstractmethod
class Employee(metaclass=ABCMeta):
    pass

The test case:

from redbaron import RedBaron
with open("test3.py", "r") as source_code:
    red = RedBaron(source_code.read())

Error message:

Traceback (most recent call last):
  File "demo.py", line 6, in <module>
    red = RedBaron(source_code.read())
  File "../redbaron/redbaron/redbaron.py", line 36, in __init__
    self.node_list = base_nodes.NodeList.from_fst(baron.parse(source_code), parent=self, on_attribute="root")
  File "../redbaron/redbaron/base_nodes.py", line 335, in from_fst
    parent=parent, on_attribute=on_attribute)
  File "../redbaron/redbaron/base_nodes.py", line 328, in __init__
    super(NodeList, self).__init__(initlist)
  File "/usr/lib/python3.6/collections/__init__.py", line 1039, in __init__
    self.data = list(initlist)
  File "../redbaron/redbaron/base_nodes.py", line 334, in <lambda>
    return klass(map(lambda x: Node.from_fst(x, parent=parent, on_attribute=on_attribute), node_list),
  File "../redbaron/redbaron/base_nodes.py", line 537, in from_fst
    return getattr(redbaron.nodes, class_name)(node, parent=parent, on_attribute=on_attribute)
  File "../redbaron/redbaron/base_nodes.py", line 526, in __init__
    setattr(self, key, NodeList.from_fst(fst[key], parent=self, on_attribute=key))
  File "../redbaron/redbaron/base_nodes.py", line 335, in from_fst
    parent=parent, on_attribute=on_attribute)
  File "../redbaron/redbaron/base_nodes.py", line 328, in __init__
    super(NodeList, self).__init__(initlist)
  File "/usr/lib/python3.6/collections/__init__.py", line 1039, in __init__
    self.data = list(initlist)
  File "../redbaron/redbaron/base_nodes.py", line 334, in <lambda>
    return klass(map(lambda x: Node.from_fst(x, parent=parent, on_attribute=on_attribute), node_list),
  File "../redbaron/redbaron/base_nodes.py", line 536, in from_fst
    class_name = baron_type_to_redbaron_classname(node["type"])
TypeError: list indices must be integers or slices, not str
olliemath commented 3 years ago

Oh wow, this is old .. yup, just hit this issue

red = RedBaron("""
class Foo(metaclass=MetaFoo): 
    pass
""")

still causes a TypeError.