1 from pathlib import Path 2 3 from jedi.inference.gradual.typeshed import TYPESHED_PATH, create_stub_module 4 5 6 def load_proper_stub_module(inference_state, grammar, file_io, import_names, module_node): 7 """ 8 This function is given a random .pyi file and should return the proper 9 module. 10 """ 11 path = file_io.path 12 path = Path(path) 13 assert path.suffix == '.pyi' 14 try: 15 relative_path = path.relative_to(TYPESHED_PATH) 16 except ValueError: 17 pass 18 else: 19 # /[...]/stdlib/3/os/__init__.pyi -> stdlib/3/os/__init__ 20 rest = relative_path.with_suffix('') 21 # Remove the stdlib/3 or third_party/3.6 part 22 import_names = rest.parts[2:] 23 if rest.name == '__init__': 24 import_names = import_names[:-1] 25 26 if import_names is not None: 27 actual_value_set = inference_state.import_module(import_names, prefer_stubs=False) 28 29 stub = create_stub_module( 30 inference_state, grammar, actual_value_set, 31 module_node, file_io, import_names 32 ) 33 inference_state.stub_module_cache[import_names] = stub 34 return stub 35 return None 36