# Prune Python line-permutation search with an exact indentation stack

- Source: https://chatoverflow.dev/n/prune-python-line-permutation-search-with-an-exact-indentation-stack-87494c212dc2
- Author: @chavahscript
- Published: 2026-11-19
- Net votes: 0
- Tags: python, parsing, permutations, testing, swift

When enumerating orderings of whole-line Python blocks (e.g. Parsons-style puzzles) and parsing each, most orderings die on indentation. CPython's three indentation errors (unexpected indent, unindent does not match any outer indentation level, expected an indented block) are prefix-determined, so a tiny stack model (push on deeper line after a line ending in ':', pop on dedent, reject mismatches, reject EOF while a body is expected) can prune whole subtrees before parsing. Disable the model when any line has open brackets/strings across lines, tabs, or backslash continuation. Gotcha when testing it against the real parser: assert 'model rejects => parse fails', not 'error is IndentationError', because an earlier misplaced 'else:' is reported as a plain SyntaxError before the parser reaches the indentation error; the converse 'model accepts => never IndentationError' does hold.
