根据pylint插件的参数设置多个推断类型

时间:2017-10-18 02:38:48

标签: python 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不喜欢这个。它将arg1arg2视为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)

1 个答案:

答案 0 :(得分:0)

Name个对象。这些基本上是文件中的字符串之前的任何内容。要获得可能的结果,您必须.infer()他们。这就像文件“basestring”中的单词变成了一个astroid class-ish对象basestring(好吧,实际上它返回一个生成器......但这里有广泛的笔画)。

然后,给定这些astroid class-ish对象,您必须将类“实例化”为astroid instance-ish对象。

最后(重要的部分),node.locals.items(){name: list of instance-ish objects}的字典。更新该字典可让您设置推断类型。

所以我上面的广泛代码会变成这样:

from astroid import MANAGER
from astroid import nodes, node_classes

def transform_myClass(node):
    updater = {}
    for key, value in node.locals.items():
        val = value[0]
        try:
            s = val.statement().value
            if s.func.name != 'ArgumentObj':
                continue
        except AttributeError:
            continue

        # Collect all the inferred types in this list
        typeList = []
        for child in s.get_children():
            if not isinstance(child, node_classes.Keyword):
                continue

            # What I needed to do was here:
            # Infer the child classes, and return the instantiated class
            if child.arg == 'allowedTypes':
                for tc in child.value.get_children():
                    for cls in tc.infer():
                        typeList.append(cls.instantiate_class())

        updater[key] = typeList

    # Finally, I needed to update the locals
    # which sets the inferred types of the class members
    node.locals.update(updater)

MANAGER.register_transform(nodes.ClassDef, transform_myClass)