While doing a cleanup of some other IndentationError
questions, I closed a bunch of questions - including IndentationError during `ast.parse` and `ast.walk` of a function which is a method inside class - as duplicates of Does Python have a built-in function for unindenting a multiline string? . The idea is: the goal in the questions I closed is to analyze the structure of some indented code with ast.parse
, but this doesn't work because ast.parse
expects code that would be valid on its own. For example, the input could be the source code of a method in a class; while that code could just as well be interpreted as a free-standing function, each line starts with additional leading whitespace. The obvious approach to solving the problem is to unindent the code string, as in the Q&A I linked.
But these closures were wrong (mea culpa), because this does not solve the problem - and I didn't keep a good record of what I closed this way. Simply unindenting the code via textwrap.dedent
isn't sufficient, because the code can contain dedented physical lines (in a docstring, logical line continued with \
at the end of a physical line, or logical line continued implicitly within open brackets) that defeat the detection of how far to dedent.
In fact, I found at least two reports of niche bugs caused by popular libraries overlooking the same problem:
in Scrapy: Scrapy Request 'IndentationError: unexpected indent' on parse callback
in Numba/Cuda: python cuda, adding @autojit before class functions
(It's actually a bit tricky to solve the problem properly. Adding a dummy if True:
line above the code is a common workaround. It works regardless of the amount of common leading indentation, because Python's indentation processing rules will turn that into a single indent. However, that added code then has to be accounted for in the ast.parse
result.)
However, there are still many questions here which still clearly duplicate each other. I am looking for assistance in identifying any existing canonical Q&A for the issue; failing that, I'm hoping to solicit someone to write it up. (It could be that IndentationError during `ast.parse` and `ast.walk` of a function which is a method inside class is already the best version of the question, but I am interested in other opinions. At any rate, it could use a better title if it's selected - and a proper answer; I'm more than willing to place a bounty.) I could also use help to identify and move the right duplicates afterward.