Reformatting code breaks code compilation

I'm trying to make PyCharm format my code so that flake8 is happy.

flake8 complains about 

E501 line too long (1017 > 140 characters)

So, in PyCharm I set the Code Style "Ensure margin is not exceeded"

As a result of reformatting my code, it has broken the code in several places (does no longer compile!). Example:

 

url = f'http://{self.v2Domain}/admin/api/devices/search?installationBusinessId={ibid}{macParam}{providerParam}{typeParam}'

(with 8 leading spaces) has been reformatted to

url = f'http://{self.v2Domain}/admin/api/devices/search?installationBusinessId={ibid}{macParam}{
providerParam}{typeParam}'

which shows error "End of statement expected. Missing closing quote"

 

On another place, I'm using raw strings (`r""" bla bla """`) for testing the JSON answer I get from an API. The reformatting has just added newlines in there, which, given the raw string, results in newlines in the JSON.

I get

json.decoder.JSONDecodeError: Invalid control character at: line 11 column 119 (char 676)

 

How should I configure PyCharm so that automatic reformatting is according to flake8 rules? (and doesn't break compilation, obviously)

 

Using PyCharm community 2019.1 (Build #PC-191.7479.30 built May 29, 2019)

 

0
3 comments

Hi,

It looks like https://youtrack.jetbrains.com/issue/PY-27615

Try updating PyCharm to the latest version and see if the problem remains.

0
Avatar
Permanently deleted user

Thanks. Updated to 2019.1.4.

Updating to 2020.1 failed (see separate issue at https://intellij-support.jetbrains.com/hc/en-us/community/posts/360008078599-Updating-to-2020-1-fails-access-denied-when-downloading-jar ).

 

With version 2019.1.4 the first case (f'' string being broken) is fixed.

However, the second case still remains. Example:

import json
from unittest import TestCase
class TestFormatting(TestCase):
def test_format(self):
testJson = r"""{"comment":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\\nUt "}"""
json.loads(testJson)

Test runs fine.

After reformat with option "Ensure right margin is not exceeded"

import json
from unittest import TestCase


class TestFormatting(TestCase):
def test_format(self):
testJson = r"""{"comment":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.\\nUt "}"""
json.loads(testJson)


Test fails with

json.decoder.JSONDecodeError: Invalid control character at: line 1 column 92 (char 91)

0

Thanks for the code snippet.

I filed a bug https://youtrack.jetbrains.com/issue/PY-41819, please upvote and follow for updates.

0

Please sign in to leave a comment.