| 1 |
from jedi import debug |
| 2 |
from jedi.inference.base_value import ValueSet, NO_VALUES, ValueWrapper |
| 3 |
from jedi.inference.gradual.base import BaseTypingValue |
| 4 |
|
| 5 |
|
| 6 |
class TypeVarClass(ValueWrapper): |
| 7 |
def py__call__(self, arguments): |
| 8 |
unpacked = arguments.unpack() |
| 9 |
|
| 10 |
key, lazy_value = next(unpacked, (None, None)) |
| 11 |
var_name = self._find_string_name(lazy_value) |
| 12 |
|
| 13 |
if var_name is None or key is not None: |
| 14 |
debug.warning('Found a variable without a name %s', arguments) |
| 15 |
return NO_VALUES |
| 16 |
|
| 17 |
return ValueSet([TypeVar.create_cached( |
| 18 |
self.inference_state, |
| 19 |
self.parent_context, |
| 20 |
tree_name=self.tree_node.name, |
| 21 |
var_name=var_name, |
| 22 |
unpacked_args=unpacked, |
| 23 |
)]) |
| 24 |
|
| 25 |
def _find_string_name(self, lazy_value): |
| 26 |
if lazy_value is None: |
| 27 |
return None |
| 28 |
|
| 29 |
value_set = lazy_value.infer() |
| 30 |
if not value_set: |
| 31 |
return None |
| 32 |
if len(value_set) > 1: |
| 33 |
debug.warning('Found multiple values for a type variable: %s', value_set) |
| 34 |
|
| 35 |
name_value = next(iter(value_set)) |
| 36 |
try: |
| 37 |
method = name_value.get_safe_value |
| 38 |
except AttributeError: |
| 39 |
return None |
| 40 |
else: |
| 41 |
safe_value = method(default=None) |
| 42 |
if isinstance(safe_value, str): |
| 43 |
return safe_value |
| 44 |
return None |
| 45 |
|
| 46 |
|
| 47 |
class TypeVar(BaseTypingValue): |
| 48 |
def __init__(self, parent_context, tree_name, var_name, unpacked_args): |
| 49 |
super().__init__(parent_context, tree_name) |
| 50 |
self._var_name = var_name |
| 51 |
|
| 52 |
self._constraints_lazy_values = [] |
| 53 |
self._bound_lazy_value = None |
| 54 |
self._covariant_lazy_value = None |
| 55 |
self._contravariant_lazy_value = None |
| 56 |
for key, lazy_value in unpacked_args: |
| 57 |
if key is None: |
| 58 |
self._constraints_lazy_values.append(lazy_value) |
| 59 |
else: |
| 60 |
if key == 'bound': |
| 61 |
self._bound_lazy_value = lazy_value |
| 62 |
elif key == 'covariant': |
| 63 |
self._covariant_lazy_value = lazy_value |
| 64 |
elif key == 'contravariant': |
| 65 |
self._contra_variant_lazy_value = lazy_value |
| 66 |
else: |
| 67 |
debug.warning('Invalid TypeVar param name %s', key) |
| 68 |
|
| 69 |
def py__name__(self): |
| 70 |
return self._var_name |
| 71 |
|
| 72 |
def get_filters(self, *args, **kwargs): |
| 73 |
return iter([]) |
| 74 |
|
| 75 |
def _get_classes(self): |
| 76 |
if self._bound_lazy_value is not None: |
| 77 |
return self._bound_lazy_value.infer() |
| 78 |
if self._constraints_lazy_values: |
| 79 |
return self.constraints |
| 80 |
debug.warning('Tried to infer the TypeVar %s without a given type', self._var_name) |
| 81 |
return NO_VALUES |
| 82 |
|
| 83 |
def is_same_class(self, other): |
| 84 |
|
| 85 |
return True |
| 86 |
|
| 87 |
@property |
| 88 |
def constraints(self): |
| 89 |
return ValueSet.from_sets( |
| 90 |
lazy.infer() for lazy in self._constraints_lazy_values |
| 91 |
) |
| 92 |
|
| 93 |
def define_generics(self, type_var_dict): |
| 94 |
try: |
| 95 |
found = type_var_dict[self.py__name__()] |
| 96 |
except KeyError: |
| 97 |
pass |
| 98 |
else: |
| 99 |
if found: |
| 100 |
return found |
| 101 |
return ValueSet({self}) |
| 102 |
|
| 103 |
def execute_annotation(self, context): |
| 104 |
return self._get_classes().execute_annotation(context) |
| 105 |
|
| 106 |
def infer_type_vars(self, value_set): |
| 107 |
def iterate(): |
| 108 |
for v in value_set: |
| 109 |
cls = v.py__class__() |
| 110 |
if v.is_function() or v.is_class(): |
| 111 |
cls = TypeWrapper(cls, v) |
| 112 |
yield cls |
| 113 |
|
| 114 |
annotation_name = self.py__name__() |
| 115 |
return {annotation_name: ValueSet(iterate())} |
| 116 |
|
| 117 |
def __repr__(self): |
| 118 |
return '<%s: %s>' % (self.__class__.__name__, self.py__name__()) |
| 119 |
|
| 120 |
|
| 121 |
class TypeWrapper(ValueWrapper): |
| 122 |
def __init__(self, wrapped_value, original_value): |
| 123 |
super().__init__(wrapped_value) |
| 124 |
self._original_value = original_value |
| 125 |
|
| 126 |
def execute_annotation(self, context): |
| 127 |
return ValueSet({self._original_value}) |