lazy_value.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from jedi.inference.base_value import ValueSet, NO_VALUES
  2. from jedi.common import monkeypatch
  3. class AbstractLazyValue:
  4. def __init__(self, data, min=1, max=1):
  5. self.data = data
  6. self.min = min
  7. self.max = max
  8. def __repr__(self):
  9. return '<%s: %s>' % (self.__class__.__name__, self.data)
  10. def infer(self):
  11. raise NotImplementedError
  12. class LazyKnownValue(AbstractLazyValue):
  13. """data is a Value."""
  14. def infer(self):
  15. return ValueSet([self.data])
  16. class LazyKnownValues(AbstractLazyValue):
  17. """data is a ValueSet."""
  18. def infer(self):
  19. return self.data
  20. class LazyUnknownValue(AbstractLazyValue):
  21. def __init__(self, min=1, max=1):
  22. super().__init__(None, min, max)
  23. def infer(self):
  24. return NO_VALUES
  25. class LazyTreeValue(AbstractLazyValue):
  26. def __init__(self, context, node, min=1, max=1):
  27. super().__init__(node, min, max)
  28. self.context = context
  29. # We need to save the predefined names. It's an unfortunate side effect
  30. # that needs to be tracked otherwise results will be wrong.
  31. self._predefined_names = dict(context.predefined_names)
  32. def infer(self):
  33. with monkeypatch(self.context, 'predefined_names', self._predefined_names):
  34. return self.context.infer_node(self.data)
  35. def get_merged_lazy_value(lazy_values):
  36. if len(lazy_values) > 1:
  37. return MergedLazyValues(lazy_values)
  38. else:
  39. return lazy_values[0]
  40. class MergedLazyValues(AbstractLazyValue):
  41. """data is a list of lazy values."""
  42. def infer(self):
  43. return ValueSet.from_sets(l.infer() for l in self.data)