我有一个类,它通过类变量实现了一些类型检查。 然后,当实例化类时,定义的变量将成为该类的必需参数,并具有所需的类型。图案看起来像这样:根据pylint插件的参数设置多个推断类型
class MyClass(MagicBaseClass):
arg1 = ArgumentObj(allowedTypes=(basestring,))
arg2 = ArgumentObj(allowedTypes=(list, tuple))
def myMethod(self):
print type(self.arg1) # a basestring
print type(self.arg2) # a list
mc = MyClass(arg1='test', arg2=())
mc.myMethod()
Pylint不喜欢这样。它将arg1
和arg2
视为ArgumentObj
的实例。所以我想编写一个读取传入类型的插件,并将这些对象作为我的MagicBaseClass
中这些类型的实例处理。
所以,我已经能够弄清楚如何挖掘到类转换的正确节点,并且可以访问我需要的所有数据,但我不知道该如何处理它。踢球者是多种允许的类型。我现在找不到任何例子来处理这个问题,我能找到的文档基本上是无用的。
from astroid import MANAGER
from astroid import nodes, node_classes
def transform_myClass(node):
for key, value in node.locals.items():
val = value[0]
try:
s = val.statement().value
if s.func.name != 'ArgumentObj':
continue
except AttributeError:
continue
for child in s.get_children():
if not isinstance(child, node_classes.Keyword):
continue
if child.arg == 'allowedTypes':
typeNames = child.value
#### And here is where I have no idea what to do next ####
MANAGER.register_transform(nodes.ClassDef, transform_myClass)