Write python code in VSCode, introduction and recommendation of code specification tools

introduction

  • In daily life, the most code written is Python. Although I am an algorithm engineer, I am not an engineer, so I still need basic coding skills.
  • Since Python is a dynamically typed language, there are no mandatory constraints. If there are no corresponding specifications, the readability of the code written by everyone will be poor, and potential problems will be difficult to find.
  • Therefore, this article aims to introduce some tools to help you write more robust code.

in conclusion

  • Let me talk about the conclusion first, to save everyone's time:
    • Pylint + Black + MyPy + isort
  • The combination of the above four tools can write more standardized and tidy code. Of course, the specific quality depends on itself, and the tools are only secondary. remember.

Pylint introduction and VSCode configuration

Introduction to Pylint

  • Pylint is a static analysis tool for Python code. It can check code quality, potential bugs, code style, complexity and many other aspects. At the same time, it has a wide range of configuration options, which can be flexibly customized according to the needs of the project.
  • It is worth mentioning that, friends may be confused about the relationship between Pylint and Flake8, and why choose Pylint?
    • This problem has also troubled me, because the Flake8 plug-in is always installed by default in my VSCode plug-in. As for its function, I have never been investigated in detail. Today, by searching online, I can understand the difference between the two:
    • the difference:
      • Functional scope : Pylint is a very powerful tool that can check multiple aspects of code quality, potential errors, code style, complexity, etc. Pylint provides a large number of inspection rules and generates detailed reports. In contrast, Flake8 focuses more on code style and syntax checking. It combines multiple independent tools, including pyflakes, pycodestyle, and mccabe, to provide consistent code style and static error checking.
      • Configuration options : Pylint provides a wide range of configuration options, which can be flexibly customized according to the needs of the project. It allows you to tweak the behavior of the rules via configuration files or command line arguments. Flake8 has relatively few configuration options and is mainly set through configuration files, but it provides some additional plugins and extensions that can add some functionality and customization options.
    • connect:
      • Static Analysis: Both Pylint and Flake8 are static analysis tools used to detect potential problems during the code writing phase. They can automatically check the code and find syntax errors, unused variables, sections that do not follow the code style guide, and more.

      • Plugin support: Both Pylint and Flake8 support the use of plugins to extend their functionality. You can choose and add suitable plugins as needed to meet the needs of specific projects.

      • Community support: Both Pylint and Flake8 have active user communities, providing documentation, examples, and support forums. This allows you to learn from the experience of other users and get feedback and help.

      • Integration with integrated development environments (IDEs): Both Pylint and Flake8 can be integrated with multiple commonly used Python integrated development environments (IDEs), such as PyCharm, Visual Studio Code, etc. This allows you to get code inspections and suggestions on the fly during development.

  • Final verdict: Choose Pylint.

VSCode configuration

  1. install Pylintplugin
  2. configurationsetting.json
        "pylint.importStrategy": "useBundled",
        "pylint.args": [
            "--disable=invalid-name,missing-module-docstring",
            "--disable=W0612,W0631,W0703,W0621,W0613,W0611,W1308,C0411,C0111,C0103,C0301,C0304,C0305,E1101,R0913,R0914,R0915,R0903" ,
        ]
    

Black introduction and VSCode configuration

Introduction

  • Black is a tool to automatically format Python code, aiming to provide a unified code style. It follows a strict set of rules and conventions to ensure that the code of different developers is consistent in format. The working principle is "no configuration required".
  • Compared with yapf , autopep8 , why choose Black?
    • Because I'm lazy, I don't want to configure too many things. Interested friends can compare the differences between the three by themselves.

VSCode configuration

  1. install Black Formatterplugin
  2. configurationsetting.json
        "[python]": {
          
          
            "editor.defaultFormatter": "ms-python.black-formatter",
            "editor.codeActionsOnSave": {
          
          
                "source.organizeImports": true
            },
            "editor.formatOnSave": true,
        },
    

MyPy introduction and VSCode configuration

Introduction to MyPy

  • MyPy is a static type checking tool. Its feature is: when type annotations are added to the code, MyPy supports type inference, type hints and type annotation functions.
  • To use this tool, you need to change your daily habits and add type annotations to function parameters, which also increases code readability. Of course, this is a painful thing at first, but after getting used to it, I benefit a lot.

VSCode configuration

  1. install pluginMypy Type Checker
  2. configurationsetting.json
        "mypy-type-checker.importStrategy": "useBundled",
        "mypy-type-checker.args": [
            "--follow-imports=skip",
            "--ignore-missing-imports",
            "--show-column-numbers",
            "--allow-untyped-defs",
            "--allow-subclassing-any",
            "--allow-untyped-calls",
            "--no-warn-no-return"
        ],
    

isort

  • This tool is used to sort the imports in the py file. It can be simply configured as a Black rule.

VSCode configuration

  1. install isortplugin
  2. configurationsetting.json
        "isort.args":["--profile", "black"],
    

Complete setting.json configuration

    "[python]": {
    
    
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.codeActionsOnSave": {
    
    
            "source.organizeImports": true
        },
        "editor.formatOnSave": true,
    },
    "isort.args":["--profile", "black"],
    "mypy-type-checker.importStrategy": "useBundled",
    "mypy-type-checker.args": [
        "--follow-imports=skip",
        "--ignore-missing-imports",
        "--show-column-numbers",
        "--allow-untyped-defs",
        "--allow-subclassing-any",
        "--allow-untyped-calls",
        "--no-warn-no-return"
    ],
    "pylint.importStrategy": "useBundled",
    "pylint.args": [
        "--disable=invalid-name,missing-module-docstring",
        "--disable=W0612,W0631,W0703,W0621,W0613,W0611,W1308,C0411,C0111,C0103,C0301,C0304,C0305,E1101,R0913,R0914,R0915,R0903" ,
    ]

Guess you like

Origin blog.csdn.net/shiwanghualuo/article/details/131750278