Back to home

如何使用pylint统一代码风格

1. IDE配置pylint插件(自行学习)

2. 配置检查规则

默认:
pylint --generate-rcfile > pylintrc
团队自定义规则,参考:

diff

6c6
< extension-pkg-whitelist=
---
> extension-pkg-whitelist=lxml
25c25
< load-plugins=
---
> load-plugins=pylint_django
53c53
< disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call
---
> disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,C0111,E0401,R0914,r0903,C0122,W0703,w0622,W0702,C1001,W0232,W0613,R0201
78c78
< output-format=parseable
---
> output-format=text
81c81
< reports=yes
---
> reports=no
254c254
< good-names=i,j,k,ex,Run,_
---
> good-names=i,j,k,ex,Run,_,e,id,ip,pk,qs,v,vm,l,urlpatterns,fd,fp
342c342
< max-args=5
---
> max-args=8
399c399
< deprecated-modules=regsub,TERMIOS,Bastion,rexec
---
> deprecated-modules=regsub,TERMIOS,Bastion,rexec,eval
360c475
< max-public-methods=30
---
> max-public-methods=20
360c476
< generated-members=alive
---
> generated-members=

反馈

E1101 Module 'lxml.etree' has no 'Element' member
处理方式
6c6
< extension-pkg-whitelist=
---
> extension-pkg-whitelist=lxml

参考


反馈

E1101 Module 'requests.packages' has no 'urllib3' member

处理方式

pylint并非真的执行,所以链接中的兼容处理此处并不生效。
建议大家还是修改下python代码
requests.packages.urllib3.disable_warnings()

修改为
import urllib3
urllib3.disable_warnings()

更严谨地
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


反馈

Class 'xxx' has no 'objects' member (no-member)

处理方式

25c25
< load-plugins=
---
> load-plugins=pylint_django


E0611
No name %r in module %r
本地未安装第三方包报错+误报。
拒绝,具体问题具体分析
C0103
变量命名不符合规范,建议忽略
拒绝,具体问题具体分析
R0904
Too many public methods
建议忽略或调大,当前20
接受,已修改为30
R0201
Method could be a function
类方法未使用self,建议忽略
接受,添加到disable列表中
R0911
Too many return statements
当前值50,建议调大到80或忽略
拒绝
W0613
Unused argument %r
继承restframework等第三方库时候的接口要求有额外的参数,但是子类实现中未使用到。
接受,添加到disable列表中
C1001
Old-style class defined
使用metalclass 会报此错误
接受,添加到disable列表中
W0232
Class has no init method
建议忽略
接受,添加到disable列表中
W0212
Access to a protected member %s of a client class
鉴权中间件需要使用protected member.
区别对待,实在没有办法才能用


反馈

类似uwsgi.ini的非py文件出现在结果中

处理方式

1. 通过添加命令行参数,依然无效
--ignore-patterns='\S+\.ini'
实际是pylint的 bug
2. 通过升级pylint到最新版本解决
pylint 1.9.1,
astroid 1.6.4


反馈
F5101 Django is not available on the PYTHONPATH
处理方式
pip install django
反馈
E1101 Instance of 'list' has no 'alive' member
处理方式
修改pylintrc
generated-members=alive