Fylsk的"app.logger"的Pylint假阳性:E1101:方法"logger"没有"debug"成员(无成员) [英] Pylint false positive for Flask's "app.logger": E1101: Method 'logger' has no 'debug' member (no-member)
问题描述
使用烧瓶的 app.logger
成员函数(例如 app.logger.error
)会导致 pylint
报告 E1101 即使在运行时定义了
app.logger
的这些成员,也会出现code>(无成员
)错误.
Using flask's app.logger
member functions (such as app.logger.error
) causes pylint
to report E1101
(no-member
) errors, even though these members of app.logger
are defined at runtime.
可以使用以下文件来复制该文件:
This can be reproduced by using the following files:
app.py
import flask
app = flask.Flask(__name__)
@app.route('/')
def say_hello():
app.logger.debug('A debug message')
app.logger.error('An error message')
return 'hello'
requirements.txt
pylint==2.1.0
Flask==1.0.2
使用 virtualenv
来重现问题的命令示例:
Sample commands for reproducing the issue, using virtualenv
:
(此处使用的是Python 3.5,但问题并不特定于该版本)
(Python 3.5 is used here, but the issue is not specific to that version)
virtualenv --python=python3.5 env
source env/bin/activate
pip install pip==18.0
pip install -r requirements.txt
最后,运行 pylint
:
pylint -E app
返回以下错误:
************* Module app
app.py:9:4: E1101: Method 'logger' has no 'debug' member (no-member)
app.py:10:4: E1101: Method 'logger' has no 'error' member (no-member)
是否有避免这些误报的好方法?
Is there a good way to avoid these false positives?
推荐答案
通过pylint插件防止这些误报的解决方案:
A solution to prevent these false positives, via pylint plugins:
pylintplugins.py
import sys
from astroid import MANAGER, scoped_nodes, extract_node
from astroid.builder import AstroidBuilder
def register(_linter):
pass
def transform(f):
if f.name == 'logger':
for prop in ['debug', 'info', 'warning', 'error', 'addHandler']:
f.instance_attrs[prop] = extract_node('def {name}(arg): return'.format(name=prop))
MANAGER.register_transform(scoped_nodes.FunctionDef, transform)
此替代方法可防止在 app.logger.debug
, app.logger.info
, app.logger.warning
, app.logger.error
和 app.logger.addHandler
.
This workaround prevents linting errors on app.logger.debug
, app.logger.info
, app.logger.warning
, app.logger.error
and app.logger.addHandler
.
要使用该文件,需要使用-load-plugins
命令行选项加载 pylintplugins.py 文件:
In order to be used, the pylintplugins.py file needs to be loaded using the --load-plugins
command line option:
PYTHONPATH="." pylint -E app --load-plugins pylintplugins
或通过在 pylintrc
配置文件中包含以下行:
or by including the following line in the pylintrc
configuration file:
load-plugins=pylintplugins
这篇关于Fylsk的"app.logger"的Pylint假阳性:E1101:方法"logger"没有"debug"成员(无成员)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!