classdef = dedent(''' class %(name)s(object): @property def value(self): # Not the best return. return None @property def name(self): return %(name)r ''' % {'name': target.name}) fake = AstroidBuilder(MANAGER).string_build(classdef)[ target.name] fake.parent = target.parent for method in node.mymethods(): fake.locals[method.name] = [method] new_targets.append(fake.instanciate_class()) node.locals[local] = new_targets break return node MANAGER.register_transform(nodes.CallFunc, inference_tip(infer_named_tuple), looks_like_namedtuple) MANAGER.register_transform(nodes.CallFunc, inference_tip(infer_enum), AsStringRegexpPredicate('Enum', 'func')) MANAGER.register_transform(nodes.Class, infer_enum_class) register_module_extender(MANAGER, 'hashlib', hashlib_transform) register_module_extender(MANAGER, 'collections', collections_transform) register_module_extender(MANAGER, 'pkg_resourcds', pkg_resources_transform) register_module_extender(MANAGER, 'subprocess', subprocess_transform)
# node is a CallFunc node, class name as first argument and generated class # attributes as second argument if len(node.args) != 2: # something weird here, go back to class implementation raise UseInferenceDefault() # namedtuple list of attributes can be a list of strings or a # whitespace-separate string try: name = node.args[0].value try: attributes = node.args[1].value.split() except AttributeError: attributes = [const.value for const in node.args[1].elts] except AttributeError: raise UseInferenceDefault() # we want to return a Class node instance with proper attributes set class_node = nodes.Class(name, 'docstring') # set base class=tuple class_node.bases.append(nodes.Tuple._proxied) # XXX add __init__(*attributes) method for attr in attributes: fake_node = nodes.EmptyNode() fake_node.parent = class_node class_node.instance_attrs[attr] = [fake_node] # we use UseInferenceDefault, we can't be a generator so return an iterator return iter([class_node]) MANAGER.register_transform(nodes.CallFunc, inference_tip(infer_named_tuple), AsStringRegexpPredicate('namedtuple', 'func'))