Python 避免 Pylint 警告 E1101: 'Instance of .. has no .. member' 对于具有动态属性的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35990313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoid Pylint warning E1101: 'Instance of .. has no .. member' for class with dynamic attributes
提问by frans
Imagine a function which dynamicallyadds attributes to an object using setattr
. The reason for doing so is that I want to map some external structure (e.g. a given parameter tree) to an object:
想象一个函数,它动态地添加属性使用对象setattr
。这样做的原因是我想将一些外部结构(例如给定的参数树)映射到一个对象:
my_object = SomeClass()
apply_structure(my_object, some_descriptor)
my_object.device1.enabled = True
Technically this works but of course Pylint rightly complains about 'device1' being not a member of SomeClass
.
从技术上讲这是可行的,但当然 Pylint 正确地抱怨“device1”不是SomeClass
.
I could disable the warning but that would be bad (because I still want to get the warning in all cases when the attribute does not exist because of misspelling, etc).
我可以禁用警告,但这会很糟糕(因为当属性由于拼写错误等不存在时,我仍然希望在所有情况下都收到警告)。
Is there a common and legal (Pylint-proof) way to dynamically add members to an object that not leads to warnings?
是否有一种通用且合法的(Pylint-proof)方法可以将成员动态添加到不会导致警告的对象中?
Alternatively: Can I disable Pylint for just one objectrather than a line/block/file?
或者:我可以只为一个对象而不是行/块/文件禁用 Pylint吗?
Explanation:
说明:
You might wonder why I should equip an object with member attributes dynamically when I plan to access these attributes in a hard-coded way later.
您可能想知道,当我打算稍后以硬编码方式访问这些属性时,为什么要动态地为对象配备成员属性。
The reason is: I have a dynamic part of the program (where the decoration happens) and a static part which is specializedfor a certain scenario. So I couldalso create a static class for this scenario but that would be overkill in a lot of situations.
原因是:我有一个程序的动态部分(装饰发生的地方)和一个专门用于特定场景的静态部分。所以我也可以为这个场景创建一个静态类,但在很多情况下这会有点矫枉过正。
The following specializedcode might allow access to some parameter of a device which might be attached to some bus:
以下专用代码可能允许访问可能连接到某些总线的设备的某些参数:
class MyDeviceHandler:
on_get_some_subtree_element(self):
return _some_internal_value
on_set_some_subtree_element(self, value):
_some_internal_value = value
dev = MyDeviceHandler()
decorate_object_with_device_structure(dev, 'some/attached/device')
dev.some.subtree.element = 5 <--- will call the set-callback
x = dev.some.subtree.element <--- will call the get-callback
So the structure behind 'some/attached/device'
might be arbitrary and very complex and I don't want to reproduce it in a class structure.
所以背后的结构'some/attached/device'
可能是任意的并且非常复杂,我不想在类结构中重现它。
One way to get rid of this warning would be to create/access a dict
based tree:
摆脱此警告的一种方法是创建/访问dict
基于树:
dev['some']['subtree']['element'] = 5
But this is harder to write and not nice to read - I would only do this to quieten Pylint.
但这更难写,也不好读——我这样做只是为了让 Pylint 安静下来。
采纳答案by frans
Just to provide the answer that works for me now - as The Compilersuggested you can add a rule for the problematic class in your projects .pylintrc
:
只是为了提供现在对我有用的答案 - 正如编译器建议您可以在项目中为有问题的类添加规则.pylintrc
:
[TYPECHECK]
ignored-classes=Fysom,MyClass
回答by user2577
This pagedescribes the error and gives an easy way to address it directly in the code. tl;dr
此页面描述了错误并提供了一种直接在代码中解决它的简单方法。tl;博士
Used when an object (variable, function, …) is accessed for a non-existent member.
当为不存在的成员访问对象(变量、函数等)时使用。
False positives: This message may report object members that are created dynamically, but exist at the time they are accessed.
误报:此消息可能会报告动态创建但在访问它们时存在的对象成员。
A commentor mentions that it can be disabled on a single line at the top of the file with # pylint: disable=no-member
. I also found that you can use # pylint: disable=E1101
based on this reddit entry.
评论者提到可以在文件顶部的一行中使用# pylint: disable=no-member
. 我还发现你可以# pylint: disable=E1101
根据这个 reddit entry 使用。
回答by Ashwani
PyLint gives this type of errors on two cases Link:
PyLint 在两种情况下给出这种类型的错误链接:
Used when an object (variable, function, …) is accessed for a non-existent member.
False positives: This message may report object members that are created dynamically, but exist at the time they are accessed.
当为不存在的成员访问对象(变量、函数等)时使用。
误报:此消息可能会报告动态创建但在访问它们时存在的对象成员。
As this error is identified as E1101 error. You can solve the issue by adding the following line in your code.
由于此错误被识别为 E1101 错误。您可以通过在代码中添加以下行来解决该问题。
# pylint: disable=E1101
回答by yongtaek jun
Try this! My problem solved!
尝试这个!我的问题解决了!
Pylint doesn't understand the Django's dynamic filed. Thus, we need to teach what the Django is to Pylint
Pylint 不了解 Django 的动态文件。因此,我们需要向 Pylint 教授 Django 是什么
*for vscode in Windows 10 *
*适用于 Windows 10 中的 vscode *
$ pip install pylint-django
$ cd your_project_folder
$ code . // run vscode
Install extension for Python, Django Snippets, Django Template in vscode
在 vscode 中安装 Python、Django Snippets、Django 模板的扩展
Open .vscode/settings.json
in vscode and add:
.vscode/settings.json
在vscode中打开并添加:
{
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.pythonPath": "venv\Scripts\python.exe",
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_django"
],
}
回答by mona-mk
For me just installing pylint-django solved the issue:
对我来说,只要安装 pylint-django 就解决了这个问题:
pip install pylint-django
回答by Joshua
I was able to avoid this warning by adding the __getattr__
method to my class, which python calls when an attribute is not found on an object. Although definitely not the cleanest solution, it worked for my particular use-case as pylint considers the object valid.
我能够通过将__getattr__
方法添加到我的类中来避免此警告,当在对象上找不到属性时,python 会调用该方法。虽然绝对不是最干净的解决方案,但它适用于我的特定用例,因为 pylint 认为对象有效。
import warnings
class AppConfiguration(object):
...
def __getattr__(self, name):
''' will only get called for undefined attributes '''
warnings.warn('No member "%s" contained in settings config.' % name)
return ''
More information about the __getattr__
method can be found here.
__getattr__
可以在此处找到有关该方法的更多信息。