Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute access should not search recursively #183

Open
lordmauve opened this issue Mar 27, 2019 · 1 comment
Open

Attribute access should not search recursively #183

lordmauve opened this issue Mar 27, 2019 · 1 comment

Comments

@lordmauve
Copy link

lordmauve commented Mar 27, 2019

When accessing an attribute on a node, RedBaron searches recursively for a matching node.

This is infuriating to program against because it finds matches that I don't expect, at any depth in the AST.

This makes working with blocks harder - if/elif/else, try/except/else/finally and so on.

>>> cond = RedBaron("""
... if foo:
...    if bar:
...       x
...    else:
...       y
... """)[0]
>>> cond.else_
else:
      y

Here the answer I'm looking for is None.

It is very odd that searching recursively is a convenient node.foo.bar while to avoid searching recursively is a laborious

node.find('foo', recursive=False).find('bar', recursive=False)
@lordmauve
Copy link
Author

lordmauve commented Mar 28, 2019

In case anyone wants a quick workaround for this, I wrote some monkey-patches using patchy:

import patchy

patchy.mc_patchface(Node.__getattr__, """
@@ -12,1 +12,1 @@
-    return self.find(key)
+    return self.find(key, recursive=False)
""")

patchy.mc_patchface(NodeList.__getattr__, """
@@ -7,1 +7,1 @@
-    return self.find(key)
+    return self.find(key, recursive=False)
""")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant