PyCQA / pycodestyle

Simple Python style checker in one Python file
https://pycodestyle.pycqa.org
Other
5.05k stars 751 forks source link

False positives when using white space around = in f-string debug format. #1258

Closed WarrenWeckesser closed 3 months ago

WarrenWeckesser commented 3 months ago

With this file:

x = 123
print(f'{ x = }')

pycodestyle 2.12.1 gives this output:

$ pycodestyle -v foo.py 
checking foo.py
foo.py:2:10: E201 whitespace after '{'
foo.py:2:12: E251 unexpected spaces around keyword / parameter equals
foo.py:2:14: E202 whitespace before '}'
foo.py:2:14: E251 unexpected spaces around keyword / parameter equals

Those spaces should not be reported as problems. Spaces in front of x and around = in that string are significant. The = format was designed and implemented to allow such spaces. To quote from https://github.com/python/cpython/issues/80998,

Finally, and this is the best part: what if you want whitespace around the equals sign? Well, to the left is no problem; whitespace is preserved from the original text inside the curly braces: f"{ chr(65) =}" => " chr(65) ='A'" But we also explicitly permit, and preserve, whitespace after the equals sign: f"{chr(65) = }" => "chr(65) = 'A'"

asottile commented 3 months ago

please search for duplicates!