utils.py 1.1 KB

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