1
0
forked from VimPlug/jedi

More rewriting of the pep0484 logic

This commit is contained in:
Dave Halter
2018-03-14 21:34:01 +01:00
parent ae6d01abf5
commit ce0aa224f1
+17 -18
View File
@@ -42,19 +42,16 @@ def _evaluate_for_annotation(context, annotation, index=None):
If index is not None, the annotation is expected to be a tuple If index is not None, the annotation is expected to be a tuple
and we're interested in that index and we're interested in that index
""" """
if annotation is not None: context_set = context.eval_node(_fix_forward_reference(context, annotation))
context_set = context.eval_node(_fix_forward_reference(context, annotation)) if index is not None:
if index is not None: context_set = context_set.filter(
context_set = context_set.filter( lambda context: context.array_type == u'tuple'
lambda context: context.array_type == u'tuple' and len(list(context.py__iter__())) >= index
and len(list(context.py__iter__())) >= index ).py__getitem__(index)
).py__getitem__(index) return context_set.execute_evaluated()
return context_set.execute_evaluated()
else:
return NO_CONTEXTS
def _evaluate_annotation_string(context, string, index): def _evaluate_annotation_string(context, string, index=None):
node = _get_forward_reference_node(context, string) node = _get_forward_reference_node(context, string)
if node is None: if node is None:
return NO_CONTEXTS return NO_CONTEXTS
@@ -175,9 +172,10 @@ def infer_param(execution_context, param):
return NO_CONTEXTS return NO_CONTEXTS
param_comment = params_comments[index] param_comment = params_comments[index]
# Construct annotation from type comment return _evaluate_annotation_string(
annotation = tree.String(repr(param_comment), node.start_pos) execution_context.get_root_context(),
annotation.parent = node.parent param_comment
)
module_context = execution_context.get_root_context() module_context = execution_context.get_root_context()
return _evaluate_for_annotation(module_context, annotation) return _evaluate_for_annotation(module_context, annotation)
@@ -213,10 +211,11 @@ def infer_return_types(function_context):
if not match: if not match:
return NO_CONTEXTS return NO_CONTEXTS
annotation = tree.String( return _evaluate_annotation_string(
repr(str(match.group(1).strip())), function_context.get_root_context(),
node.start_pos) match.group(1).strip()
annotation.parent = node.parent )
module_context = function_context.get_root_context() module_context = function_context.get_root_context()
return _evaluate_for_annotation(module_context, annotation) return _evaluate_for_annotation(module_context, annotation)