Autoindentation and realtime formal syntax check

"Hi everyone,

Coming from a background in languages with explicit block structures (like GFA Basic or Ada), I’ve observed a recurring issue in Python when dealing with large modules (1000+ lines).

The Issue: Outdent Ambiguity
In Python, a backward tab (outdent) is functionally overloaded.

It can signify the end of the current block, or multiple blocks simultaneously.

While visually clean, this relies entirely on the programmer’s manual spacing.

In complex nested structures, a single accidental backspace can shift the logic of the entire program without triggering a syntax error. This is a silent killer of code integrity.

The Proposal
I would like to discuss the possibility of an (optional) strict mode or a linter standard where blocks must be terminated with a dedicated marker (e.g., an # end comment or a keyword).

The rule would be simple:

  1. Every indentation level must remain constant until a formal ‘end’ instruction is met.

  2. The ‘end’ instruction must match the opening statement (e.g., # end if for an if block).

  3. If an outdent occurs without the matching terminator, or the terminator doesn’t match the opening, the interpreter (or linter) raises a formal error.

Why?
We no longer have the memory constraints of the 80s. Modern development should prioritize readability and structural guarantees. Relying solely on whitespace for logic is a risk factor in large-scale software engineering that could be mitigated by making the ‘contract’ between the programmer and the structure explicit.

In the Python language self that is not an issue because of this is not influencing the interpreter:

indentation is correct

the grammar is correct

the end block statement can be ignored while running the code.

I’m curious to hear if the core team has ever considered a ‘strict-indent’ mode to prevent these silent logical errors.

Best regards,

Nicola

I personally consider python’s syntax to be quite readable as is and I did not encounter the problems you were referring to. I am quite sure that python’s syntax is not consequence of memory constraints, but deliberate stylistic choices by GvR.

But if you want to have this as a linter, feel free to go forward with this.

(Moved to Ideas from PEPs since the latter category is only for discussing actual PEPs that have been formally proposed and sponsored by a core team member.)

3 Likes

I think a strict mode is unlikely to be added to the language. Where possible, we rely on external tools instead of adding to the maintenance burden of the core team.

I have once or twice seen people use # end comments like you suggest, but it is extremely rare. This makes me think the vast majority of people don’t need it.

I’m not sure what a linter standard would be. As far as I know, we don’t have any existing linter standards, only the rules implemented in popular linters.

Certainly someone could write a linter for this style of commenting. It doesn’t need any language changes or agreement, just someone to write it and publicize it.

3 Likes

I’d thought about it a little late, too. So I decided to ask the IDE developers. It seems like a more reasonable approach. Thanks anyway.