How to apply formatting rules from PyCharm in VSCode?
Asked Answered
H

2

8

As I understand, PyCharm uses his own set of formatting rules. https://www.jetbrains.com/help/pycharm/configuring-code-style.html#editorconfig

Is there any faster way to apply same rules to the VSCode and PyCharm?

Solution could be either import formatting rules from PyCharm to VSCode OR apply same formatting rules in PyCharm as in VSCode (e.g. default formatter black). Is it possible?

Edit:

Let's say one developer using PyCharm and format document using those rules. Other developer uses VSCode with black. When developer 2 makes changes in the very long file, when he apply formatter, it also modifies formatting rules that was made by dev 1. Those changes is very annoying to review in PR, they triggered as changes, but have no impact on actual logic.

Edit2: Better example:

PyCharm defaut

def test_002_enable_service_route_security_access_denied(
        reset_test_conf, enable_service_route, can_signal, can, tf, record_property 
):

Black

def test_002_enable_service_route_security_access_denied(
    reset_test_conf, enable_service_route, can_signal, can, tf, record_property 
):

P.S. Yes, I can manually change every indent and other option. But point is to have same formatting rules using different IDEs, with ease of importing rules

Update Nov.2023: PyCharm uses own set of formatting rules, there's no 1-1 conversion between VSCode and PyCharm (like .clang-format files or similar)

Herrod answered 26/10, 2022 at 10:20 Comment(0)
F
1

The settings in vscode are stored in the settings.json file. You can also select from the gears in the lower left corner:

enter image description here

With the example you mentioned, I can query format in the search box and then customize the formatting tool:

enter image description here

You can also add the following codes into the settings.json file:

"python.formatting.provider": "black",

enter image description here

Add: The example you gave is that the line length exceeds the default value of black, so the line is automatically wrapped. You can add the following code to settings.json to modify the default value:

"python.formatting.blackArgs": [
    "max-line-length:120"
],
Fetation answered 27/10, 2022 at 1:33 Comment(6)
Thank you, but question is to use SAME formatting rules in both IDEsHerrod
@LeonidTsigrinski e.g. default formatter black I give the answer based on the example you put forward. Could you tell me in detail what kind of solution you want? Because vscode currently provides only these three formats, you can refer to the settings in github for more customized settings, such as black.Fetation
@LeonidTsigrinski I added my answer and hope it can help you.Fetation
unfortunately, that was not the case. Point of the question is to find generic standard for applying formatting rules within different IDE'sHerrod
This answer works well since there is no 1:1 conversion between pycharm and vscode. By setting the max-length to 120 you are able to achieve the same default formatting settings of pycharm on vscode.Stealth
@LeonidTsigrinski except if you found another way of solving this issue (I am facing the same issue), I think that the only way is to setup black to be an available formatter in Pycharm. Pycharm formatter is a proprietary smart tool developed by JetBrains and is not available for users with other IDEs. There's some documentation online to setup Black within Pycharm.Warmedover
E
0

https://www.jetbrains.com/help/pycharm/reformat-and-rearrange-code.html

You can reformat a part of code, the whole file, group of files, a directory, and a module. You can also exclude part of code or some files from the reformatting. PyCharm adheres to PEP8 rules and requirements for arranging and formatting Python code

This is from Pycharm doc. So you should use PEP8. In vs code you can use autopep8 extension and edit User Setting in Ctrl+Shift+p like this:

 "[python]": {
        "editor.defaultFormatter": "ms-python.autopep8",
        "editor.formatOnSave": true,
    },
    "autopep8.args": [
        "--max-line-length","120","--aggressive","--ignore","E722,E125,E126,E127,W605,E721,E731"
    ],

If you want Pycharm formatting to be exactly like VS code formatting you should ignore some other rules also. Or you can add them in Pycharm instead. For inline warning for pep8 you can use pylint or flake8 extension. The autopep8 is only for auto-formatting.

The ignored rules in Pycharm don't apply automatically.

E722 - Do not use bare except:

Using bare except: is discouraged because it catches all exceptions, including system-exiting ones. Instead, specify the exception being caught.

E125 - Continuation line with same indent as next logical line:

This occurs when a continuation line is indented at the same level as the next logical line, which can lead to readability issues.

E126 - Continuation line over-indented for hanging indent:

This error indicates that a continuation line is more indented than necessary, making the code harder to read.

E127 - Continuation line over-indented for visual indent:

This error occurs when a continuation line is visually indented more than expected, which can affect readability.

W605 - Invalid escape sequence:

This warning indicates that a string contains an invalid escape sequence. For example, using \ followed by a character that does not form a valid escape sequence.

E721 - Do not compare types, use isinstance():

Comparing types directly using type() is discouraged. Instead, use isinstance() for type checks to improve code readability and maintainability.

E731 - Do not assign a lambda expression, use a def:

Assigning a lambda expression directly to a variable or a function is discouraged. Use a def statement to define the function instead.

Edrisedrock answered 28/7 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.