3

在vscode中为Django配置专属的语法检测提醒,防止误报语法错误

 1 year ago
source link: https://v3u.cn/a_id_66
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在vscode中为Django配置专属的语法检测提醒,防止误报语法错误

首页 - Python/2019-04-16

    Python插件中默认使用pylint用来检测python代码的书写是否有错误和是否符合良好的编码习惯。

Python官网(https://www.python.org/dev/peps/pep-0008/)专门有版块介绍推荐的python代码习惯,比如类,函数等的命名规则,遵守这些规则可以使自己的代码更加简明和易读。

然而pylint在面对django框架时表现的有些不足,因为django使用了大量的元编程(metaprograming)思想以及鸭子模型,以至于在程序运行时会修改不少对象的属性和行为,但是这样给pylint这样的语法检测程序带来了比较大的困难。典型的就是集成了django.db.models.Model的模型层对象在编译时没有objects属性,但是运行时却有,造成我们在编写代码时pylint会报"has no objects attributes"之类的错误,虽然不影响程序运行,但是很影响代码的阅读性。所以有人专门开发了pylint的插件pylint-django

这里简单介绍一下如何使用pylint-django来规避一些错误的误报

首先安装pylint以及pylint-django

pip install pylint pylint_django

然后在vscode的配置文件(settings.json)中加上如下配置

"python.linting.pylintArgs": [
        "--load-plugins",
        "pylint_django",
        "--errors-only"
    ]




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK