First, the clearly bad options:
Should a custom moderator flag be raised explaining the problem?
Definitely do not bother moderators for something like this. There isn't, objectively, a problem that falls within their jurisdiction.
Or is it ok to leave them as it is? The OP receives their answer in the end.
In the case you cited, OP self-answered shortly after asking with the result of research; then years later, someone else came along with a new answer that was also completely wrong. So OP definitely did not "receive an answer" due to leaving the question open.
But more to the point, Stack Overflow is not about OP receiving an answer. It is about building a reference library. OP solving the problem is a nice bonus, and a great way to help keep people around - thus, part of the value of duplicate closures - but it is not at all our metric for proper handling.
Should they be closed as duplicates to some another indentation related question?
Should they be flagged as "Not reproducible or was caused by a typo"?
These options need to be considered together, because the core question is: are these questions typos?
My argument is they are usually not typos; and as such, I wish to disagree very strenuously with the previously accepted answer.
My underlying premise: a typo is when the OP either doesn't pay attention to something that would already be clearly understood, or doesn't think about a problem using information and knowledge that is already clearly demonstrated.
But it is not a typo when OP encounters a problem due to not knowing something, no matter how trivial. Nor is it "unlikely to help others" if there are a lot of "others" who don't know that same thing.
So:
In my experience reviewing hundreds (perhaps more than a thousand) of these questions, it has become abundantly clear that people often genuinely do not understand even the most basic ideas about how indentation is used in Python. (Sometimes one can't fathom how they expect Python to determine the block structure of the code; this is beside the point.) And yes, this includes people who are doing things like trying to make a website with Django, trying to snap together some pieces of an AI system with TensorFlow, etc. Python is quite probably the most popular language with the "in wayyy over their head" set.
Also in my experience reviewing these questions—as well as potential canonicals—it has become abundantly clear that there are several fundamentally distinct ways of messing things up, and that these ways can be quite subtle (to the point where the site software itself interferes with attempts to produce a proper MRE).
An IndentationError
can result from an empty block, and a correct understanding of the problem (and how to solve it) depends on whether it was intentional to have an empty block (or whether the next line of code was instead intended to be part of the block).
A try
missing the corresponding except
(or finally
) can result in IndentationError: unexpected indent
if the next part of the code is unindented past the try
(which could make perfect sense if the except
were present), but will give a generic SyntaxError
when the next part is flush with the try
. This was fixed only quite recently (somewhere between 3.9 and 3.11 inclusive) - now you can get a SyntaxError
that explicitly explains the problem.
Most non-beginners know that mixed spaces and tabs are problematic. They might not realize how easy it is to end up with such code without realizing it. Editors do all kinds of strange, idiosyncratic things. Python 3 was supposed to make the rules a bit stricter, and give a clear, distinct TabError
in cases of mixed indentation; but it doesn't consistently detect that a problem is caused by mixed indentation, and 2to3
could preserve whitespace that worked in 2.x but doesn't in 3.x. Wrongly indented code can "look right" in any number of ways (spaces before a tab are especially pernicious). In 2.x, valid code can "look wrong". Even in 3.x, there's a bizarre corner case of valid indentation that seems clearly wrong to me, that would look wrong if you set non-standard tab stops.
Relatedly: basically every "mixed spaces and tabs" question lacks a proper MRE in the post, even if OP provided one - because Stack Overflow itself converts the tabs to spaces. People who dismiss the problem as "oh, it must be a mixed-spaces-and-tabs situation" are almost always right, and having a Q&A about that for them to point to makes a ton more sense than having them get into an argument with OP about typos, or what keys were physically pressed to create the indentation. People who want to point out the specific issue, on the other hand, may have to open an edit view for the post. For older questions, they'd have to look at the original source of the post via the revisions interface - which isn't even in the site UI if there were no edits after the grace period.
Beginners often don't understand that Python is actually compiled, and thus IndentationError
can't be caught by the same code that has the indentation problem, but it can be caught in exec'd code. Beginners may find this unintuitive because the problem is reported using the same mechanism as ordinary runtime errors (because they do happen at runtime for other code, within a metacircular evaluation context). They might not find information about this easily, because it isn't immediately apparent that IndentationError
is a subtype of SyntaxError
.
Even non-beginners who have a valid reason for exec
ing code might genuinely not realize that the code can't start out indented. (Granted, in some cases it's on them for not noticing that the code was all indented; but either way the real underlying question is how to unindent it.)
Even then, one might wonder about how to dedent some copied-and-pasted code using an editor feature. And while looking through what seemed to be IndentationError
questions, I found a lot of people who had problems related to trying to paste code into a REPL and not realizing the issues related to blank lines, or who somehow ended up with an interpreter trying to evaluate each line separately.
In short, there is a ton to learn about the topic.
Declaring that such issues are universally typos, is essentially arguing for the closure and ultimate deletion (or locking) of all the previous attempts at canonicals. I think it would be truly absurd to treat, in particular, the "main" canonical I'm getting an IndentationError (or a TabError). How do I fix it? that way - it's already been through the Meta wringer and had tons of work done, and it's very clearly answering actual questions - about problems that people have, commonly and reproducibly, due to a lack of understanding rather than a lack of attention or effort.
So: please ordinarily close such questions as duplicates. Of course, if OP says it's a typo (often in the form of a self-answer), it's a typo. To preempt some objections:
"The main canonical is a roll-up question!" - I disagree. Perhaps it should be edited to remove some things, but the core of it is quite coherent.
The signposts in the question are there specifically for the purpose of helping duplicate closers find better targets when that is necessary. It is usually not necessary.
Yes, a failure to understand the basic semantics of Python indentation can cause at least four distinct error messages as well as arbitrary logical errors if one is sufficiently unlucky. But it's still the same problem, with the same solution: you need to indent to mark the beginning of an "indented block" and dedent to mark the end, and this is done with leading whitespace that is interpreted according to a particular set of rules.
Please do use other canonicals, like some of the ones I linked above, for more specific problems. And of course if the OP's question about "fixing" the indentation is at the level of how to produce the desired text (rather than what text is desired), use e.g. How to fix Python indentation. But please do not choose a question simply because it's specific to the OP's error message. That doesn't help with understanding. A person who mistakenly over-indents makes fundamentally the same mistake as one who mistakenly under-indents, and needs fundamentally the same information.
I could see an argument for separating out the "mixed tabs and spaces that look correct" issue from the general issue of not understanding how much indentation to use or why. But as far as I know, we don't really have that canonical. No, IndentationError: unindent does not match any outer indentation level, although the indentation looks correct doesn't qualify IMO, because it's too heavily 2.x-oriented, is too vague ("mixed spaces and tabs" doesn't adequately explain what is and isn't allowed) and has way too many answers.
"Dupe-hammering a low-quality question isn't signalling the quality issue" - okay, so downvote it. If it's helping search engine users to find the canonical, it's doing its job.
"Dupe-hammered questions don't get Roomba'd" - frame challenge: why should they? They can't get new answers, they highlight the importance of the canonical, and they improve SEO. They should only be deleted when they mess up SEO by having wrong titles (especially "clickbait" ones that seem to describe a different problem someone could be searching for). If they have low-quality answers, ideally the answers should be deleted, so that the question can auto-redirect logged-out users. If they somehow have non-duplicate answers, typically one would prefer to preserve that information. (Of course, this is not realistic in the current case.)
- Consider: getting the question Roomba'd as a typo requires three votes to close, dealing with answers, and then waiting. Getting the question deleted as a duplicate requires one gold-badge vote to close, waiting or downvoting, and then three votes to delete - paying no heed to any FGITW interference. Is the latter actually harder? Plus, it has the advantage of being more effective against such interference in the first place.