forked from VimPlug/jedi
First pass at extending infer_type_vars
This mostly works for the new tests, but doesn't work for: - tuples (though this seems to be because they lack generic information anyway) - nested Type[T] handling (e.g: List[Type[T]])
This commit is contained in:
@@ -364,7 +364,7 @@ def _infer_type_vars(annotation_value, value_set, is_class_value=False):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif isinstance(annotation_value, GenericClass):
|
elif isinstance(annotation_value, GenericClass):
|
||||||
if annotation_name == 'Iterable':
|
if annotation_name == 'Iterable' and not is_class_value:
|
||||||
given = annotation_value.get_generics()
|
given = annotation_value.get_generics()
|
||||||
if given:
|
if given:
|
||||||
for nested_annotation_value in given[0]:
|
for nested_annotation_value in given[0]:
|
||||||
@@ -375,32 +375,52 @@ def _infer_type_vars(annotation_value, value_set, is_class_value=False):
|
|||||||
value_set.merge_types_of_iterate(),
|
value_set.merge_types_of_iterate(),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif annotation_name == 'Mapping':
|
else:
|
||||||
given = annotation_value.get_generics()
|
# Note: we need to handle the MRO _in order_, so we need to extract
|
||||||
if len(given) == 2:
|
# the elements from the set first, then handle them, even if we put
|
||||||
for value in value_set:
|
# them back in a set afterwards.
|
||||||
try:
|
for element in value_set:
|
||||||
method = value.get_mapping_item_values
|
if not hasattr(element, 'is_instance'):
|
||||||
except AttributeError:
|
continue
|
||||||
continue
|
|
||||||
key_values, value_values = method()
|
if element.is_instance():
|
||||||
|
py_class = element.py__class__()
|
||||||
|
else:
|
||||||
|
py_class = element
|
||||||
|
|
||||||
|
# TODO: what about things like 'str', which likely aren't
|
||||||
|
# generic, but do implement 'Iterable[str]'?
|
||||||
|
if not isinstance(py_class, DefineGenericBase):
|
||||||
|
continue
|
||||||
|
|
||||||
|
for klass in py_class.py__mro__():
|
||||||
|
class_name = klass.py__name__()
|
||||||
|
if annotation_name == class_name:
|
||||||
|
annotation_generics = annotation_value.get_generics()
|
||||||
|
actual_generics = klass.get_generics()
|
||||||
|
|
||||||
|
if len(annotation_generics) != len(actual_generics):
|
||||||
|
# TODO: might there be other matches elsewhere in the MRO?
|
||||||
|
# TODO: what happens if _some_ of the generics are realised at
|
||||||
|
# this point, but not all (e.g: class `Foo(Dict[str, T])`)?
|
||||||
|
break
|
||||||
|
|
||||||
|
for annotation_generics_set, actual_generic_set in zip(annotation_generics, actual_generics):
|
||||||
|
for nested_annotation_value in annotation_generics_set:
|
||||||
|
_merge_type_var_dicts(
|
||||||
|
type_var_dict,
|
||||||
|
_infer_type_vars(
|
||||||
|
nested_annotation_value,
|
||||||
|
actual_generic_set,
|
||||||
|
# This is a note to ourselves that we
|
||||||
|
# have already converted the instance
|
||||||
|
# representation to its class.
|
||||||
|
is_class_value=True,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
for nested_annotation_value in given[0]:
|
|
||||||
_merge_type_var_dicts(
|
|
||||||
type_var_dict,
|
|
||||||
_infer_type_vars(
|
|
||||||
nested_annotation_value,
|
|
||||||
key_values,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
for nested_annotation_value in given[1]:
|
|
||||||
_merge_type_var_dicts(
|
|
||||||
type_var_dict,
|
|
||||||
_infer_type_vars(
|
|
||||||
nested_annotation_value,
|
|
||||||
value_values,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return type_var_dict
|
return type_var_dict
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user