I think I'm aware of the usual causes for IndentationError
like described in IndentationError: unindent does not match any outer indentation level for example. This doesn't apply here.
Also, I know about textwrap.dedent
but it doesn't feel like it's the right approach here?
If I have a "regular" function, I can do ast.parse
and ast.walk
like this:
import ast
import inspect
def a():
pass
code = inspect.getsource(a)
nodes = ast.walk(ast.parse(code))
for node in nodes:
...
However, if the function is a method inside a class like:
class B:
def c(self):
pass
code = inspect.getsource(B.c)
nodes = ast.walk(ast.parse(code))
I get:
IndentationError: unexpected indent
Which makes sense, I guess, since B.c
is indented by one level. So how do I ast.parse
and ast.walk
here instead?