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
4from jedi.inference.compiled.value import CompiledValue, CompiledName, \
5 CompiledValueFilter, CompiledValueName, create_from_access_path
6from jedi.inference.base_value import LazyValueWrapper
7
8
9def 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 # Most of the time there is only symbol, but sometimes there are different
18 # sys.version_infos, where there are multiple ones, just use the first one.
19 return next(iter(name.infer()))
20
21
22class ExactValue(LazyValueWrapper):
23 """
24 This class represents exact values, that makes operations like additions
25 and exact boolean values possible, while still being a "normal" stub.
26 """
27 def __init__(self, compiled_value):
28 self.inference_state = compiled_value.inference_state
29 self._compiled_value = compiled_value
30
31 def __getattribute__(self, name):
32 if name in ('get_safe_value', 'execute_operation', 'access_handle',
33 'negate', 'py__bool__', 'is_compiled'):
34 return getattr(self._compiled_value, name)
35 return super().__getattribute__(name)
36
37 def _get_wrapped_value(self):
38 instance, = builtin_from_name(
39 self.inference_state, self._compiled_value.name.string_name).execute_with_values()
40 return instance
41
42 def __repr__(self):
43 return '<%s: %s>' % (self.__class__.__name__, self._compiled_value)
44
45
46def create_simple_object(inference_state, obj):
47 """
48 Only allows creations of objects that are easily picklable across Python
49 versions.
50 """
51 assert type(obj) in (int, float, str, bytes, slice, complex, bool), repr(obj)
52 compiled_value = create_from_access_path(
53 inference_state,
54 inference_state.compiled_subprocess.create_simple_object(obj)
55 )
56 return ExactValue(compiled_value)
57
58
59def get_string_value_set(inference_state):
60 return builtin_from_name(inference_state, 'str').execute_with_values()
61
62
63def load_module(inference_state, dotted_name, **kwargs):
64 # Temporary, some tensorflow builtins cannot be loaded, so it's tried again
65 # and again and it's really slow.
66 if dotted_name.startswith('tensorflow.'):
67 return None
68 access_path = inference_state.compiled_subprocess.load_module(dotted_name=dotted_name, **kwargs)
69 if access_path is None:
70 return None
71 return create_from_access_path(inference_state, access_path)