惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
Jina AI
Jina AI
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
小众软件
小众软件
M
MIT News - Artificial intelligence
MyScale Blog
MyScale Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
月光博客
月光博客
L
LangChain Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
C
Check Point Blog
U
Unit 42
人人都是产品经理
人人都是产品经理

Jiajun的技术笔记

你好,2026! TiDB 源码阅读(六):TiDB Coprocessor 源码解析 性能优化的核心思想 TiDB 源码阅读(五):索引 TiDB 源码阅读(四):AST、逻辑计划、物理计划 CockroachDB Serverless Architecture podman 无故退出 Cursor Control-L (CTRL-L) Keyboard Shortcuts in Terminal Replace docker with podman Using xmonad with xfce4 A RC script for freebsd frpc 自己动手写一个k8s controller AI 会取代你的(编程)岗位吗? 自建DERP服务器提升Tailscale连接速度(使用Nginx转发) 自动升级Docker容器 再读《程序员修炼之道-从小工到专家》 让浏览器下载文件 再读《软件随想录》/《黑客与画家》/《软技能》 HTTP 压力测试中的 Coordinated Omission 2的补码 编程语言中的 context 是什么? flutter macOS 构建出错 Flatpak 使用小记 Golang CAS 操作是怎么实现的 PostgreSQL 当MQ来使用 Clash 结合 工作VPN 的网络设计 使用 PostgreSQL 搭建 JuiceFS PostgreSQL 配置优化和日志分析 有GitHub Copilot?那就可以搭建你的ChatGPT4服务 窗口函数的使用(以PG为例)
给你的代码跑个分?pylint使用教程
Jiajun Huang · 2020-03-12 · via Jiajun的技术笔记

之前我一直使用 flake8 来检查Python项目中的代码规范,工作良好,除了一个问题,由于Python是动态语言,编译器只能做最基本 最简单的错误检查,这也就意味着,很多错误,如果肉眼看不出来,那就得等执行报错了才会被发现。

因此有必要使用 pylint 这种代码检查器来减少出错的概率。

安装

各Linux发行版可以直接安装:

$ sudo apt install pylint

不过如果是使用了虚拟环境例如 pyenv, venv, virtualenv 等,还是建议在当前环境中直接安装:

$ pip install pylint

pylint 默认的输出有一些可能不适用于我们的项目,因此要有一个配置文件来进行配置,编辑 ~/.pylintrc

[FORMAT]
max-line-length=120
[MESSAGES CONTROL]
disable=C0114,C0116,C0103,W0703,C0115,R0201,R0914,R0903,R0913,R0801

比如上面的配置文件,就是说代码超过120个字符才会报代码过长,然后下面 MESSAGES CONTROL 中禁用的错误/警告类型就不会报错。 而这些错误警告码呢,就是自己来定制的,我是从 pylint <项目文件夹或代码文件> 的结果中抄出来的,比如:

hello = 1

在没有配置文件的情况下运行一下:

$ pylint hello.py 
************* Module hello
hello.py:2:5: C0326: Exactly one space required around assignment
world=2
     ^ (bad-whitespace)
hello.py:1:0: C0114: Missing module docstring (missing-module-docstring)
hello.py:1:0: C0103: Constant name "hello" doesn't conform to UPPER_CASE naming style (invalid-name)
hello.py:2:0: C0103: Constant name "world" doesn't conform to UPPER_CASE naming style (invalid-name)

----------------------------------------------------------------------
Your code has been rated at -10.00/10 (previous run: -10.00/10, +0.00)

有一些提示就不是特别适用,比如 C0114: Missing module docstring (missing-module-docstring), 于是把 C0114 加到 disable= 后面,再次运行:

$ pylint hello.py 
************* Module hello
hello.py:2:5: C0326: Exactly one space required around assignment
world=2
     ^ (bad-whitespace)

------------------------------------------------------------------
Your code has been rated at 5.00/10 (previous run: 5.00/10, +0.00)

不过我们不能每次都依靠跑一遍命令来检查错误,这种模式比较适用于CI,我们希望在编辑代码的时候就看到提示,不过这 一点也不难,比如vim,我使用 ale 这个插件,他默认如果检测到 pylint 就会使用 pylint 来检查

这个时候就好很多。注意 pylint 的输出里,最后会给你的代码评一个分,比如我维护的一个系统,在改之前是9.5分, 照着pylint给出的建议,改成了10分,你的呢?