有
Name
之前
他们什么都做了。要得到它们可能是什么,你必须
.infer()
他们这将类似于文件“basestring”中的单词的内容转换为星形类对象
basestring
(好吧,实际上它返回一个生成器……但这里有广泛的笔划)。
然后,给定这些astroid类ish对象,您必须将该类“实例化”为astroid实例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)