Subject: Re: bug#72750: 29.3; indent-region changes semantics of python code

From: kobarity <[email-masked]>
Date: Sat, 21 Sep 2024 23:51:00 +0900

Stefan Kangas wrote:
> Michael Arndt via "Bug reports for GNU Emacs, the Swiss army knife of
> text editors" <[email-masked]> writes:
> 
> > I have a problem when indenting python files. There seems to be a case
> > when using indent-region changes the semantics of the python code. When
> > there is no blank line after an if-statement, the next line becomes part
> > of the if statement.
> >
> > The problem can be reproduced by the following steps:
> >
> > 1. Start emacs -Q
> > 2. Create a python file with the following contents:
> >
> > if False:
> >       print("output1")
> > print("output2")
> >
> > 3. Use M-x mark-whole-buffer
> > 4. Use M-x indent-region
> > 5. The file contents change into:
> >
> > if False:
> >       print("output1")
> >       print("output2")
> >
> > The problem can be avoided by adding a blank line after the if statement.
> > Because I use a custom indentation function that calls (indent-region
> > (point-min) (point-max)) this can happen pretty quickly. Is this a
> > limitation of python-indent-region?
> 
> I'm not sure that I see a bug here.  I don't think one can generally
> expect `indent-region` to be able to avoid changing semantics in a
> language where indentation matters.

I agree.  Even though `python-indent-region' can be improved to some
extent, it seems to me that it is better to use an external tool like
"black" to preserve the semantics and format the entire buffer.