__init__.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # This file also re-exports symbols for wider use. We configure mypy and flake8
  2. # to be aware that this file does this.
  3. from jedi.inference.compiled.value import CompiledValue, CompiledName, \
  4. CompiledValueFilter, CompiledValueName, create_from_access_path
  5. from jedi.inference.base_value import LazyValueWrapper
  6. def builtin_from_name(inference_state, string):
  7. typing_builtins_module = inference_state.builtins_module
  8. if string in ('None', 'True', 'False'):
  9. builtins, = typing_builtins_module.non_stub_value_set
  10. filter_ = next(builtins.get_filters())
  11. else:
  12. filter_ = next(typing_builtins_module.get_filters())
  13. name, = filter_.get(string)
  14. value, = name.infer()
  15. return value
  16. class ExactValue(LazyValueWrapper):
  17. """
  18. This class represents exact values, that makes operations like additions
  19. and exact boolean values possible, while still being a "normal" stub.
  20. """
  21. def __init__(self, compiled_value):
  22. self.inference_state = compiled_value.inference_state
  23. self._compiled_value = compiled_value
  24. def __getattribute__(self, name):
  25. if name in ('get_safe_value', 'execute_operation', 'access_handle',
  26. 'negate', 'py__bool__', 'is_compiled'):
  27. return getattr(self._compiled_value, name)
  28. return super().__getattribute__(name)
  29. def _get_wrapped_value(self):
  30. instance, = builtin_from_name(
  31. self.inference_state, self._compiled_value.name.string_name).execute_with_values()
  32. return instance
  33. def __repr__(self):
  34. return '<%s: %s>' % (self.__class__.__name__, self._compiled_value)
  35. def create_simple_object(inference_state, obj):
  36. """
  37. Only allows creations of objects that are easily picklable across Python
  38. versions.
  39. """
  40. assert type(obj) in (int, float, str, bytes, slice, complex, bool), repr(obj)
  41. compiled_value = create_from_access_path(
  42. inference_state,
  43. inference_state.compiled_subprocess.create_simple_object(obj)
  44. )
  45. return ExactValue(compiled_value)
  46. def get_string_value_set(inference_state):
  47. return builtin_from_name(inference_state, 'str').execute_with_values()
  48. def load_module(inference_state, dotted_name, **kwargs):
  49. # Temporary, some tensorflow builtins cannot be loaded, so it's tried again
  50. # and again and it's really slow.
  51. if dotted_name.startswith('tensorflow.'):
  52. return None
  53. access_path = inference_state.compiled_subprocess.load_module(dotted_name=dotted_name, **kwargs)
  54. if access_path is None:
  55. return None
  56. return create_from_access_path(inference_state, access_path)