pylint-dev/pylint
GitHub: pylint-dev/pylint
Pylint 是一个用于 Python 的静态代码分析器,帮助在不运行代码时发现错误、强制规范并提出重构建议。
Stars: 5674 | Forks: 1240
# Pylint
.. _`Pylint`: https://pylint.readthedocs.io/
.. This is used inside the doc to recover the start of the introduction
.. image:: https://github.com/pylint-dev/pylint/actions/workflows/tests.yaml/badge.svg?branch=main
:target: https://github.com/pylint-dev/pylint/actions
.. image:: https://codecov.io/gh/pylint-dev/pylint/branch/main/graph/badge.svg?token=ZETEzayrfk
:target: https://codecov.io/gh/pylint-dev/pylint
.. image:: https://img.shields.io/pypi/v/pylint.svg
:alt: PyPI Package version
:target: https://pypi.python.org/pypi/pylint
.. image:: https://readthedocs.org/projects/pylint/badge/?version=latest
:target: https://pylint.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/ambv/black
.. image:: https://img.shields.io/badge/linting-pylint-yellowgreen
:target: https://github.com/pylint-dev/pylint
.. image:: https://results.pre-commit.ci/badge/github/pylint-dev/pylint/main.svg
:target: https://results.pre-commit.ci/latest/github/pylint-dev/pylint/main
:alt: pre-commit.ci status
.. image:: https://bestpractices.coreinfrastructure.org/projects/6328/badge
:target: https://bestpractices.coreinfrastructure.org/projects/6328
:alt: CII Best Practices
.. image:: https://img.shields.io/ossf-scorecard/github.com/PyCQA/pylint?label=openssf%20scorecard&style=flat
:target: https://api.securityscorecards.dev/projects/github.com/PyCQA/pylint
:alt: OpenSSF Scorecard
.. image:: https://img.shields.io/discord/825463413634891776.svg
:target: https://discord.gg/qYxpadCgkx
:alt: Discord
## 什么是 Pylint?
Pylint 是一个用于 Python 2 或 3 的 `静态代码分析器`_。最新版本支持 Python
3.10.0 及以上版本。
.. _`静态代码分析器`: https://en.wikipedia.org/wiki/Static_code_analysis
Pylint 在不实际运行代码的情况下分析代码。它检查错误、强制执行编码标准、查找 `代码异味`_,并可以提出关于代码
如何重构的建议。
.. _`代码异味`: https://martinfowler.com/bliki/CodeSmell.html
## 安装
.. This is used inside the doc to recover the start of the short text for installation
对于命令行使用,pylint 通过以下方式安装::
```
pip install pylint
```
或者,如果你想同时使用拼写检查功能 ``enchant``(你可能需要
`安装 enchant C 库 `_):
.. code-block:: sh
pip install pylint[spelling]
它也可以集成到大多数编辑器或 IDE 中。更多详细信息可以在
`文档`_ 中找到。
.. _`文档`: https://pylint.readthedocs.io/en/latest/user_guide/installation/index.html
.. This is used inside the doc to recover the end of the short text for installation
## 与众不同的 Pylint
Pylint 不会盲目信任你的类型,而是使用其内部代码表示(astroid)推断节点的实际值(
首先因为没有类型检查时 pylint 启动)。如果你的代码是 ``import logging as argparse``,Pylint
可以检查并知道 ``argparse.error(...)`` 实际上是一个日志调用,而不是 argparse 调用。这使得 pylint 速度较慢,但也能让 pylint 发现更多问题,
即使你的代码没有完全类型化。
```
[inference] is the killer feature that keeps us using [pylint] in our project despite how painfully slow it is.
- `Realist pylint user`_, 2022
```
.. _`Realist pylint user`: https://github.com/charliermarsh/ruff/issues/970#issuecomment-1381067064
pylint 并不害怕比现在更慢一点,而是比其他 linters 更加详尽。
有更多的检查,包括一些有主观性的检查,默认是关闭的,但可以通过配置启用。
## 如何使用 pylint
Pylint 并不比你更聪明:它可能会警告你那些你认真完成的事情,或者检查一些你并不关心的事情。
在采用阶段,特别是在从未强制使用 pylint 的遗留项目中,最好从 ``--errors-only`` 标志开始,
然后使用 ``--disable=C,R`` 禁用约定和重构消息,并随着优先级的演变逐步重新评估和重新启用消息。
Pylint 高度可配置,并允许编写插件以添加你自己的检查(例如,针对内部库或内部规则)。Pylint 还有
一个生态系统,包含针对流行框架和第三方库的现有插件。
.. note::
```
Pylint supports the Python standard library out of the box. Third-party
libraries are not always supported, so a plugin might be needed. A good place
to start is ``PyPI`` which often returns a plugin by searching for
``pylint ``. `pylint-pydantic`_, `pylint-django`_ and
`pylint-sonarjson`_ are examples of such plugins. More information about plugins
and how to load them can be found at `plugins`_.
```
.. _`plugins`: https://pylint.readthedocs.io/en/latest/development_guide/how_tos/plugins.html#plugins
.. _`pylint-pydantic`: https://pypi.org/project/pylint-pydantic
.. _`pylint-django`: https://github.com/pylint-dev/pylint-django
.. _`pylint-sonarjson`: https://github.com/cnescatlab/pylint-sonarjson-catlab
## 建议搭配使用的 linter
你可能想与 pylint 一同使用的项目包括 ruff_(**真正快速**,具有内置自动修复和大量从流行 linter 借鉴的检查,但
使用 ``rust`` 实现)或 flake8_(一个使用 ``ast`` 直接实现你自己的检查的框架),
mypy_、pyright_ / pylance 或 pyre_(类型检查),bandit_(安全检查),black_ 和
isort_(自动格式化),autoflake_(自动移除未使用的导入或变量),pyupgrade_
(自动升级到更新的 Python 语法)和 pydocstringformatter_(自动 pep257)。
.. _ruff: https://github.com/astral-sh/ruff
.. _flake8: https://github.com/PyCQA/flake8
.. _bandit: https://github.com/PyCQA/bandit
.. _mypy: https://github.com/python/mypy
.. _pyright: https://github.com/microsoft/pyright
.. _pyre: https://github.com/facebook/pyre-check
.. _black: https://github.com/psf/black
.. _autoflake: https://github.com/myint/autoflake
.. _pyupgrade: https://github.com/asottile/pyupgrade
.. _pydocstringformatter: https://github.com/DanielNoord/pydocstringformatter
.. _isort: https://pycqa.github.io/isort/
## pylint 包含的额外工具
Pylint 附带了两个额外工具:
- pyreverse_(独立工具,用于生成包和类图。)
- symilar_ (重复代码查找器,也集成在 pylint 中)
.. _pyreverse: https://pylint.readthedocs.io/en/latest/additional_tools/pyreverse/index.html
.. _symilar: https://pylint.readthedocs.io/en/latest/additional_tools/symilar/index.html
.. This is used inside the doc to recover the end of the introduction
## 展示你的使用
你可以在 README 中放置此徽章,让其他人知道你的项目使用了 pylint。
```
.. image:: https://img.shields.io/badge/linting-pylint-yellowgreen
:target: https://github.com/pylint-dev/pylint
```
了解如何将徽章添加到你的文档中,请参考 `徽章文档`_。
.. _the badge documentation: https://pylint.readthedocs.io/en/latest/user_guide/installation/badge.html
## 许可证
pylint,除以下例外情况外,`遵循 GPLv2 `_。
图标文件根据 `CC BY-SA 4.0 `_ 许可证授权:
- `doc/logo.png `_
- `doc/logo.svg `_
## 支持
请查看 `联系方式`_。
.. _`联系方式`: https://pylint.readthedocs.io/en/latest/contact.html
.. |tideliftlogo| image:: https://raw.githubusercontent.com/pylint-dev/pylint/main/doc/media/Tidelift_Logos_RGB_Tidelift_Shorthand_On-White.png
:width: 200
:alt: Tidelift
.. list-table::
:widths: 10 100
* - |tideliftlogo|
- pylint 的专业支持可通过 `Tidelift
订阅`_ 获得。Tidelift 为软件开发团队提供单一来源,用于
购买和维护软件,提供来自专家的专业级保证,
同时无缝集成到现有工具中。
.. _Tidelift Subscription: https://tidelift.com/subscription/pkg/pypi-pylint?utm_source=pypi-pylint&utm_medium=referral&utm_campaign=readme
标签:Linter, pptx, Pylint, PyPI, Python, Python 2, Python 3, 代码健康, 代码审查, 代码异味, 代码规范, 代码评分, 安全专业人员, 安全编码, 开源, 无后门, 最佳实践, 编码标准, 调试插件, 逆向工具, 错误基检测, 静态代码分析, 预提交