hhatto / autopep8

A tool that automatically formats Python code to conform to the PEP 8 style guide.
https://pypi.org/project/autopep8/
MIT License
4.54k stars 291 forks source link

Incorrect handling of variables ending with '_' #670

Closed tomsib2001 closed 1 year ago

tomsib2001 commented 1 year ago

x_ : str in a function argument gets reformatted as x_: str which then triggers a runtime error SyntaxError: invalid syntax

Python Code

def f(x_ : str):
    return x_

Command Line and Configuration

No specific configuration .pep8, setup.cfg, ...

[pep8]

Command Line

$ autopep8 -i file.py 

Your Environment

nbaju1 commented 1 year ago

Are you saying that running code with a function defined as

def f(x_: str):
    return x_

procude that error? Or is the actual formatting of the function argument causing the error? Cause I have no issues running a script with this syntax (on 3.10.8 though).

hhatto commented 1 year ago

I can't reproduce it specifically in my environment.

$ python3 --version
Python 3.10.6

$ autopep8 --version
autopep8 2.0.0 (pycodestyle: 2.10.0)

$ cat ff.py
def f(x_ : str):
    return x_

$ autopep8 -d ff.py
--- original/ff.py
+++ fixed/ff.py
@@ -1,2 +1,2 @@
-def f(x_ : str):
+def f(x_: str):
     return x_

$ autopep8 -i ff.py

$ python ff.py

$

Is it possible that file.py is importing another file and a syntax error is occurring there? In any case, it is not autopep8's problem for now, so I'll close it. If you have additional information, I would appreciate your comments.

@tomsib2001