def __init__(self, type_constraints) -> None: """Initialize a type store with all the built-in types from the typeshed module.""" self.type_constraints = type_constraints self.classes = defaultdict(lambda: defaultdict(list)) self.functions = defaultdict(list) self.methods = defaultdict(list) builder = AstroidBuilder() module = builder.file_build(TYPE_SHED_PATH) self._parse_classes(module) self._parse_functions(module) # Add in initializers for klass_name, methods in self.classes.items(): if '__init__' in methods: self.functions[klass_name] = [class_callable(init) for init, _ in methods['__init__']]
def __init__(self, type_constraints): self.type_constraints = type_constraints with open(TYPE_SHED_PATH) as f: contents = '\n'.join(f.readlines()) module = astroid.parse(contents) self.classes = defaultdict(lambda: defaultdict(list)) self.functions = defaultdict(list) for class_def in module.nodes_of_class(astroid.ClassDef): tvars = [] for base in class_def.bases: if isinstance(base, astroid.Subscript): gen = base.value.as_string() tvars = base.slice.as_string().strip('()').replace( " ", "").split(',') if gen == 'Generic': self.classes[class_def.name]['__pyta_tvars'] = tvars for node in (nodes[0] for nodes in class_def.locals.values() if isinstance(nodes[0], astroid.AssignName) and isinstance(nodes[0].parent, astroid.AnnAssign)): self.classes[class_def.name][node.name] = [ parse_annotations(node, tvars) ] for function_def in module.nodes_of_class(astroid.FunctionDef): in_class = isinstance(function_def.parent, astroid.ClassDef) if in_class: tvars = self.classes[function_def.parent.name]['__pyta_tvars'] else: tvars = [] f_type = parse_annotations(function_def, tvars) self.functions[function_def.name].append(f_type) if in_class: self.classes[function_def.parent.name][ function_def.name].append(f_type) # Add in constructors for klass_name, methods in self.classes.items(): if '__init__' in methods: self.functions[klass_name] = [ class_callable(init) for init in methods['__init__'] ]
def __init__(self, type_constraints): self.type_constraints = type_constraints with open(TYPE_SHED_PATH) as f: contents = '\n'.join(f.readlines()) module = astroid.parse(contents) self.classes = defaultdict(lambda: defaultdict(list)) self.functions = defaultdict(list) for class_def in module.nodes_of_class(astroid.ClassDef): tvars = [] for base in class_def.bases: if isinstance(base, astroid.Subscript): gen = base.value.as_string() tvars = base.slice.as_string().strip('()').replace(" ", "").split(',') if gen == 'Generic': self.classes[class_def.name]['__pyta_tvars'] = tvars for node in (nodes[0] for nodes in class_def.locals.values() if isinstance(nodes[0], astroid.AssignName) and isinstance(nodes[0].parent, astroid.AnnAssign)): self.classes[class_def.name][node.name] = [ parse_annotations(node, tvars) ] for function_def in module.nodes_of_class(astroid.FunctionDef): in_class = isinstance(function_def.parent, astroid.ClassDef) if in_class: tvars = self.classes[function_def.parent.name]['__pyta_tvars'] else: tvars = [] f_type = parse_annotations(function_def, tvars) self.functions[function_def.name].append(f_type) if in_class: self.classes[function_def.parent.name][function_def.name].append(f_type) # Add in constructors for klass_name, methods in self.classes.items(): if '__init__' in methods: self.functions[klass_name] = [class_callable(init) for init in methods['__init__']]