Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Pylint

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to configure Pylint". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

But one of the things that makes Pylint unique is its ability to enforce higher-level issues: for example, the number of rows in a function or the number of methods in a class. By default, Pylint is very aggressive. It will provide harsh advice on everything from checking whether declared interfaces are actually implemented to the possibility of refactoring duplicate code, which can be a lot for new users. A gentle way to introduce it into a project or team is to turn off all inspectors and then turn them on one by one. This is especially useful if you're already using flake8, black, and mypy: Pylint has quite a few inspectors and they overlap in functionality.

These numbers may vary from project to project and may depend on the preferences of the development team. However, once the team has agreed on parameters, it is useful to use automated tools to enforce them. This is where Pylint shines.

Configure Pylint

To start with an empty configuration, set.pylintrc to

[MESSAGES CONTROL]disable=all

This will disable all Pylint messages. Since many of them are redundant, this makes sense. In Pylint, a message is a specific warning.

You can confirm that all messages are closed by running pylint:

$ pylint

In general, adding parameters to the pylint command line is not a good idea: the best place to configure pylint is.pylintrc. In order for it to do something useful, we need to enable some messages.

To enable messages, add under [MESSAGES CONTROL] in.pylintrc

enable=, ...

For seemingly useful "messages"(Pylint calls them different types of warnings). My favorites include too-many-lines, too-many-arguments, and too-many-branches. All of this limits the complexity of the module or function, and code complexity measurements can be made objectively without manual intervention.

Inspectors are the source of messages: each message belongs to only one inspector. Many of the most useful messages are under the Design Inspector. Default numbers are usually good, but adjusting the maximum is easy: we can add a section called DESIGN to.pylintrc.

[DESIGN]max-args=7max-locals=15

Another useful source of information is the "refactoring" inspector. Some of my favorite messages that I have enabled are consider-using-dict-judgment, stop-iteration-return (which finds the correct way to stop iteration is return instead of iterators that use raise StopIteration), and chained-comparison, which would suggest syntax such as 1 < = x < 5 instead of the less obvious 1 5.

Finally, there is a prohibitively expensive inspector, but one that is very useful: similarities. It looks for copy and paste between different sections of code to enforce the "don't repeat yourself"(DRY principle). It enables only one message: duplicate-code. The default Minimum Similar Rows is set to 4. It can be set to different values using.pylintrc.

[SIMILARITIES]min-similarity-lines=3

Pylint makes code review easy

If you're tired of code reviews that point out that a class is too complex or that two different functions are essentially the same, add Pylint to your continuous integration configuration and argue only once about the project complexity criteria.

"How to configure Pylint" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report