避免使用Pylint警告E1101:具有动态属性的类的“ ..实例没有..成员” [英] Avoid Pylint warning E1101: 'Instance of .. has no .. member' for class with dynamic attributes

查看:350
本文介绍了避免使用Pylint警告E1101:具有动态属性的类的“ ..实例没有..成员”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设想一个函数,该函数使用 setattr 动态地将属性添加到对象。这样做的原因是我想将某些外部结构(例如给定的参数树)映射到对象:

Imagine a function which dynamically adds 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:

my_object = SomeClass()
apply_structure(my_object, some_descriptor)
my_object.device1.enabled = True

从技术上讲这是可行的,但是当然Pylint正确地抱怨'device1'不是 SomeClass 的成员。

Technically this works but of course Pylint rightly complains about 'device1' being not a member of 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).

是否存在一种通用且合法的(防Pylint证明)方式将成员动态添加到不会导致警告的对象?

Is there a common and legal (Pylint-proof) way to dynamically add members to an object that not leads to warnings?

或者:我可以仅禁用Pylint吗?是一个对象而不是行/块/文件?

Alternatively: Can I disable Pylint for just one object rather than a line/block/file?

说明

您可能想知道为什么在pla时我应该动态地为对象配备成员属性n稍后以硬编码的方式访问这些属性。

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 specialized for a certain scenario. So I could also create a static class for this scenario but that would be overkill in a lot of situations.

下面的 specialized 代码可能允许访问可能连接到某些总线的设备的某些参数:

The following specialized code 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

因此'some / attached / device'背后的结构可能是任意的,而且非常复杂,我不愿意

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.

摆脱这种警告的一种方法是创建/访问 dict 的树:

One way to get rid of this warning would be to create/access a dict based tree:

dev['some']['subtree']['element'] = 5

但这很难写而且不好看-我只会这样做以使Pylint安静下来。

But this is harder to write and not nice to read - I would only do this to quieten Pylint.

推荐答案

只是提供现在对我有用的答案-编译器建议您可以在项目 .pylintrc

Just to provide the answer that works for me now - as The Compiler suggested you can add a rule for the problematic class in your projects .pylintrc:

[TYPECHECK]
ignored-classes=Fysom,MyClass

这篇关于避免使用Pylint警告E1101:具有动态属性的类的“ ..实例没有..成员”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆