def to_stub(value): if value.is_stub(): return ValueSet([value]) was_instance = value.is_instance() if was_instance: value = value.py__class__() qualified_names = value.get_qualified_names() stub_module = _load_stub_module(value.get_root_context().get_value()) if stub_module is None or qualified_names is None: return NO_VALUES was_bound_method = value.is_bound_method() if was_bound_method: # Infer the object first. We can infer the method later. method_name = qualified_names[-1] qualified_names = qualified_names[:-1] was_instance = True stub_values = ValueSet([stub_module]) for name in qualified_names: stub_values = stub_values.py__getattribute__(name) if was_instance: stub_values = ValueSet.from_sets(c.execute_with_values() for c in stub_values if c.is_class()) if was_bound_method: # Now that the instance has been properly created, we can simply get # the method. stub_values = stub_values.py__getattribute__(method_name) return stub_values
def _python_to_stub_names(names, fallback_to_python=False): for name in names: module_context = name.get_root_context() if module_context.is_stub(): yield name continue if name.is_import(): for new_name in name.goto(): # Imports don't need to be converted, because they are already # stubs if possible. if fallback_to_python or new_name.is_stub(): yield new_name continue name_list = name.get_qualified_names() stubs = NO_VALUES if name_list is not None: stub_module = _load_stub_module(module_context.get_value()) if stub_module is not None: stubs = ValueSet({stub_module}) for name in name_list[:-1]: stubs = stubs.py__getattribute__(name) if stubs and name_list: new_names = stubs.goto(name_list[-1]) for new_name in new_names: yield new_name if new_names: continue elif stubs: for c in stubs: yield c.name continue if fallback_to_python: # This is the part where if we haven't found anything, just return # the stub name. yield name