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.56k stars 290 forks source link

autopep8 wrongly checks the format in string #576

Closed amoskong closed 3 years ago

amoskong commented 3 years ago

Description

Hi, I touched a problem in checking coding style by autopep8, the string content (cqlsh statement) will be reported as problem.

/CC @asias

Python Code


$ cat mycode.py 

def help():
    session.execute("
                    CREATE KEYSPACE ks WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
                    ")

Command Line and Configuration

.pep8, setup.cfg, ...

[pep8]

Command Line

$ autopep8 -v -d mycode.py 
[file:mycode.py]
--->  Applying global fix for E265
--->  9 issue(s) to fix {'E501': {4}, 'E702': {4}, 'E128': {5}, 'E201': {4}, 'E203': {4}, 'E202': {4}, 'E251': {4}}
--->  8 issue(s) to fix {'E501': {4}, 'E128': {5}, 'E201': {4}, 'E203': {4}, 'E202': {4}, 'E251': {4}}
--->  7 issue(s) to fix {'E501': {4}, 'E128': {5}, 'E203': {4}, 'E202': {4}, 'E251': {4}}
--->  6 issue(s) to fix {'E501': {4}, 'E128': {5}, 'E203': {4}, 'E202': {4}, 'E251': {4}}
--->  5 issue(s) to fix {'E501': {4}, 'E128': {5}, 'E202': {4}, 'E251': {4}}
--->  4 issue(s) to fix {'E501': {4}, 'E128': {5}, 'E251': {4}}
--->  3 issue(s) to fix {'E501': {4}, 'E128': {5}, 'E251': {4}}
--->  2 issue(s) to fix {'E501': {4}, 'E128': {5}}
--- original/mycode.py
+++ fixed/mycode.py
@@ -1,5 +1,5 @@

 def help():
     session.execute("
-                    CREATE KEYSPACE ks WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
+                    CREATE KEYSPACE ks WITH REPLICATION={'class': 'SimpleStrategy', 'replication_factor': 3}
                     ")

Your Environment

No problem for string with 3 quotes

     session.execute("""
                     CREATE KEYSPACE ks WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
                     """)

No problem for string with 1 quotes in same line

    session.execute(
        "CREATE KEYSPACE ks WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };")
hhatto commented 3 years ago

First of all, this Python code is incorrect.

def help():
    session.execute("
                    CREATE KEYSPACE ks WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
                    ")
$ python mycode.py
  File "mycode.py", line 2
    session.execute("
                    ^
SyntaxError: EOL while scanning string literal

And if we pass that code to pycodestyle, it will detect the error. However, I don't think we can take care of this because it is invalid Python code.

autopep8 also uses pycodestyle in the backend, so it works the same way.

yvvt0379 commented 3 years ago

@amoskong you should write the code like this(pay attention to the triple quotes):

def help():
    session.execute("""
                    CREATE KEYSPACE ks WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
                    """)