Format: text/x-python
⬇ Download File (2.6 KB)
   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  
   4  from jedi.inference.compiled.value import CompiledValue, CompiledName, \
   5      CompiledValueFilter, CompiledValueName, create_from_access_path
   6  from jedi.inference.base_value import LazyValueWrapper
   7  
   8  
   9  def builtin_from_name(inference_state, string):
  10      typing_builtins_module = inference_state.builtins_module
  11      if string in ('None', 'True', 'False'):
  12          builtins, = typing_builtins_module.non_stub_value_set
  13          filter_ = next(builtins.get_filters())
  14      else:
  15          filter_ = next(typing_builtins_module.get_filters())
  16      name, = filter_.get(string)
  17      value, = name.infer()
  18      return value
  19  
  20  
  21  class ExactValue(LazyValueWrapper):
  22      """
  23      This class represents exact values, that makes operations like additions
  24      and exact boolean values possible, while still being a "normal" stub.
  25      """
  26      def __init__(self, compiled_value):
  27          self.inference_state = compiled_value.inference_state
  28          self._compiled_value = compiled_value
  29  
  30      def __getattribute__(self, name):
  31          if name in ('get_safe_value', 'execute_operation', 'access_handle',
  32                      'negate', 'py__bool__', 'is_compiled'):
  33              return getattr(self._compiled_value, name)
  34          return super().__getattribute__(name)
  35  
  36      def _get_wrapped_value(self):
  37          instance, = builtin_from_name(
  38              self.inference_state, self._compiled_value.name.string_name).execute_with_values()
  39          return instance
  40  
  41      def __repr__(self):
  42          return '<%s: %s>' % (self.__class__.__name__, self._compiled_value)
  43  
  44  
  45  def create_simple_object(inference_state, obj):
  46      """
  47      Only allows creations of objects that are easily picklable across Python
  48      versions.
  49      """
  50      assert type(obj) in (int, float, str, bytes, slice, complex, bool), repr(obj)
  51      compiled_value = create_from_access_path(
  52          inference_state,
  53          inference_state.compiled_subprocess.create_simple_object(obj)
  54      )
  55      return ExactValue(compiled_value)
  56  
  57  
  58  def get_string_value_set(inference_state):
  59      return builtin_from_name(inference_state, 'str').execute_with_values()
  60  
  61  
  62  def load_module(inference_state, dotted_name, **kwargs):
  63      # Temporary, some tensorflow builtins cannot be loaded, so it's tried again
  64      # and again and it's really slow.
  65      if dotted_name.startswith('tensorflow.'):
  66          return None
  67      access_path = inference_state.compiled_subprocess.load_module(dotted_name=dotted_name, **kwargs)
  68      if access_path is None:
  69          return None
  70      return create_from_access_path(inference_state, access_path)
  71