Start moving some of the pep0484 comment code around

This commit is contained in:
Dave Halter
2018-03-14 21:27:29 +01:00
parent e6469f46c7
commit ae6d01abf5
+41 -24
View File
@@ -46,7 +46,7 @@ def _evaluate_for_annotation(context, annotation, index=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()
@@ -54,30 +54,51 @@ def _evaluate_for_annotation(context, annotation, index=None):
return NO_CONTEXTS return NO_CONTEXTS
def _evaluate_annotation_string(context, string, index):
node = _get_forward_reference_node(context, string)
if node is None:
return NO_CONTEXTS
context_set = context.eval_node(node)
if index is not None:
context_set = context_set.filter(
lambda context: context.array_type == u'tuple'
and len(list(context.py__iter__())) >= index
).py__getitem__(index)
return context_set.execute_evaluated()
def _fix_forward_reference(context, node): def _fix_forward_reference(context, node):
evaled_nodes = context.eval_node(node) evaled_nodes = context.eval_node(node)
if len(evaled_nodes) != 1: if len(evaled_nodes) != 1:
debug.warning("Eval'ed typing index %s should lead to 1 object, " debug.warning("Eval'ed typing index %s should lead to 1 object, "
" not %s" % (node, evaled_nodes)) " not %s" % (node, evaled_nodes))
return node return node
evaled_node = list(evaled_nodes)[0]
if is_string(evaled_node): evaled_context = list(evaled_nodes)[0]
try: if is_string(evaled_context):
new_node = context.evaluator.grammar.parse( result = _get_forward_reference_node(context, evaled_context.get_safe_value())
force_unicode(evaled_node.get_safe_value()), if result is not None:
start_symbol='eval_input', return result
error_recovery=False
) return node
except ParserSyntaxError:
debug.warning('Annotation not parsed: %s' % evaled_node)
return node def _get_forward_reference_node(context, string):
else: try:
module = node.get_root_node() new_node = context.evaluator.grammar.parse(
parser_utils.move(new_node, module.end_pos[0]) force_unicode(string),
new_node.parent = context.tree_node start_symbol='eval_input',
return new_node error_recovery=False
)
except ParserSyntaxError:
debug.warning('Annotation not parsed: %s' % string)
return None
else: else:
return node module = context.tree_node.get_root_node()
parser_utils.move(new_node, module.end_pos[0])
new_node.parent = context.tree_node
return new_node
def _split_comment_param_declaration(decl_text): def _split_comment_param_declaration(decl_text):
@@ -306,10 +327,6 @@ def _find_type_from_comment_hint(context, node, varlist, name):
if comment is None: if comment is None:
return [] return []
match = re.match(r"^#\s*type:\s*([^#]*)", comment) match = re.match(r"^#\s*type:\s*([^#]*)", comment)
if not match: if match is None:
return [] return []
annotation = tree.String( return _evaluate_annotation_string(context, match.group(1).strip(), index)
force_unicode(repr(str(match.group(1).strip()))),
node.start_pos)
annotation.parent = node.parent
return _evaluate_for_annotation(context, annotation, index)