PyCQA / redbaron

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

find and find_all for StringNode appear to be broken #137

Closed dusktreader closed 7 years ago

dusktreader commented 7 years ago

I'm attempting to find the docstring for a module and search for text in it. However, doing find for a StringNode type fails

            with open('migration.py') as script:
                content = script.read()
            red = redbaron.RedBaron(content)
            red.find(redbaron.nodes.StringNode)

Results in an error like this

ambix/migration_script.py:49: in change_down_revision
    red.find(redbaron.nodes.StringNode)
../../.virtualenvs/ambix/lib/python3.5/site-packages/redbaron/base_nodes.py:314: in find
    candidate = i.find(identifier, *args, **kwargs)
../../.virtualenvs/ambix/lib/python3.5/site-packages/redbaron/base_nodes.py:761: in find
    return next(self.find_iter(identifier, *args, **kwargs), None)
../../.virtualenvs/ambix/lib/python3.5/site-packages/redbaron/base_nodes.py:741: in find_iter
    if self._node_match_query(self, identifier, *args, **kwargs):
../../.virtualenvs/ambix/lib/python3.5/site-packages/redbaron/base_nodes.py:781: in _node_match_query
    "re:") else identifier):
../../.virtualenvs/ambix/lib/python3.5/site-packages/redbaron/base_nodes.py:816: in _attribute_match_query
    if query(attribute):
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <[AttributeError("StringNode instance has no attribute 'type' and 'type' is not a valid identifier of another node") raised in repr()] StringNode object at 0x103230630>
fst = 'string', parent = None, on_attribute = None

    def __init__(self, fst, parent=None, on_attribute=None):
        self.init = True
        self.parent = parent
        self.on_attribute = on_attribute
        self._str_keys = ["type"]
        self._list_keys = []
        self._dict_keys = []
>       self.type = fst["type"]
E       ambix.exceptions.AmbixError: failed while changing down revision -- TypeError: string indices must be integers

../../.virtualenvs/ambix/lib/python3.5/site-packages/redbaron/base_nodes.py:479: AmbixError

The source code being processed is:

"""Some dummy migration script for alembic

Revision ID: aaaaaa
Revises: None
Create Date: 2017-06-09 11:33:51.000000

"""

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'aaaaaa'
down_revision = None

def upgrade():
    pass

def downgrade():
    pass
Psycojoker commented 7 years ago

Hello,

You need to uses red.find("string") instead of giving it the class object, same for find_all.

I'm surprise, where have you find this syntax? I don't remember having used it in any example or part of the documentation.

dusktreader commented 7 years ago

Thanks for replying. I don't know why I started using that syntax. Maybe because I was trying to use 'str' and it wasn't working.

For posterity, the working syntax is

            docstring = red.find('string', value=re.compile(
                '.*Revises.*', flags=re.MULTILINE | re.DOTALL,
            ))