gpt4 book ai didi

python - 是什么导致了这个神秘的 pylint E1101 错误?

转载 作者:行者123 更新时间:2023-12-03 17:22:28 30 4
gpt4 key购买 nike

我有以下代码:

#!/usr/bin/env python                                                                 
"""pylint behavior test"""


def autodetect_method(method, data):
"""autodetect method"""
if not method:
method = 'POST' if data else 'GET'
else:
method = method.upper()

return method
pylint产生以下错误:
tt.py:10:17: E1101: Class 'method' has no 'upper' member (no-member)
重命名 method不报错其他东西的变量,例如至 method_name !
所以,我知道几种方法来摆脱这个错误信息。但我很好奇变量名 method 有什么特别之处为什么会产生错误?
以防万一这个问题是特定于版本的,我的版本是:
$ pylint --version
pylint 2.4.4
astroid 2.3.3
Python 3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0]

最佳答案

有一种方法可以满足您的好奇心 - 调试。
你可以运行 pylint来自同一个文件并跟踪其行为。

if __name__ == "__main__":
import pylint
import sys
sys.argv.append(__file__)
pylint.run_pylint()

其实说起来容易做起来难,我已经试过了,但无法理解具体发生了什么。在高级术语中, pylint尝试推断 method 的类型在表达式 method.upper() 中,自己做不到,陷入 astroid图书馆,还有 method类型推断为 ClassDef.method .明明字 methodastroid 来说意味着特别的东西,就像其他一些词一样。比如同样的 E1101如果您使用名称 function,则会检测到错误而不是 method .
看起来它的行为以某种方式与内置名称有关,但是 functionmethod不是内置函数。我猜 astroid如果 ast 的“别名”,则将它们视为某种类型类(class),但我不确定。

关于python - 是什么导致了这个神秘的 pylint E1101 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66614262/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com