forked from VimPlug/jedi
Merge branch 'improve-type-annotation-inference-refactors' of https://github.com/PeterJCLaw/jedi
This commit is contained in:
@@ -184,7 +184,44 @@ class _TypingClassMixin(ClassMixin):
|
||||
|
||||
|
||||
class TypingClassValueWithIndex(_TypingClassMixin, TypingValueWithIndex):
|
||||
pass
|
||||
def infer_type_vars(self, value_set, is_class_value=False):
|
||||
# Circular
|
||||
from jedi.inference.gradual.annotation import merge_pairwise_generics, merge_type_var_dicts
|
||||
|
||||
type_var_dict = {}
|
||||
annotation_generics = self.get_generics()
|
||||
|
||||
if not annotation_generics:
|
||||
return type_var_dict
|
||||
|
||||
annotation_name = self.py__name__()
|
||||
if annotation_name == 'Type':
|
||||
if is_class_value:
|
||||
for element in value_set:
|
||||
element_name = element.py__name__()
|
||||
if annotation_name == element_name:
|
||||
merge_type_var_dicts(
|
||||
type_var_dict,
|
||||
merge_pairwise_generics(self, element),
|
||||
)
|
||||
|
||||
else:
|
||||
return annotation_generics[0].infer_type_vars(
|
||||
value_set,
|
||||
is_class_value=True,
|
||||
)
|
||||
|
||||
elif annotation_name == 'Callable':
|
||||
if len(annotation_generics) == 2:
|
||||
return annotation_generics[1].infer_type_vars(
|
||||
value_set.execute_annotation(),
|
||||
)
|
||||
|
||||
elif annotation_name == 'Tuple':
|
||||
tuple_annotation, = self.execute_annotation()
|
||||
return tuple_annotation.infer_type_vars(value_set, is_class_value)
|
||||
|
||||
return type_var_dict
|
||||
|
||||
|
||||
class ProxyTypingClassValue(_TypingClassMixin, ProxyTypingValue):
|
||||
@@ -281,6 +318,38 @@ class Tuple(BaseTypingValueWithGenerics):
|
||||
.py__getattribute__('tuple').execute_annotation()
|
||||
return tuple_
|
||||
|
||||
def infer_type_vars(self, value_set, is_class_value=False):
|
||||
# Circular
|
||||
from jedi.inference.gradual.annotation import merge_pairwise_generics, merge_type_var_dicts
|
||||
from jedi.inference.gradual.base import GenericClass
|
||||
|
||||
if self._is_homogenous():
|
||||
# The parameter annotation is of the form `Tuple[T, ...]`,
|
||||
# so we treat the incoming tuple like a iterable sequence
|
||||
# rather than a positional container of elements.
|
||||
return self.get_generics()[0].infer_type_vars(
|
||||
value_set.merge_types_of_iterate(),
|
||||
)
|
||||
|
||||
else:
|
||||
# The parameter annotation has only explicit type parameters
|
||||
# (e.g: `Tuple[T]`, `Tuple[T, U]`, `Tuple[T, U, V]`, etc.) so we
|
||||
# treat the incoming values as needing to match the annotation
|
||||
# exactly, just as we would for non-tuple annotations.
|
||||
|
||||
type_var_dict = {}
|
||||
for element in value_set:
|
||||
py_class = element.get_annotated_class_object()
|
||||
if not isinstance(py_class, GenericClass):
|
||||
py_class = element
|
||||
|
||||
merge_type_var_dicts(
|
||||
type_var_dict,
|
||||
merge_pairwise_generics(self, py_class),
|
||||
)
|
||||
|
||||
return type_var_dict
|
||||
|
||||
|
||||
class Generic(BaseTypingValueWithGenerics):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user