Skip to content

Commit 82f6cb6

Browse files
committed
return-instance-attributes-instantly
1 parent f770dd0 commit 82f6cb6

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

IPython/core/guarded_eval.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -577,37 +577,40 @@ def _handle_assign(node: ast.Assign, context: EvaluationContext):
577577

578578
# Before starred
579579
for i in range(star_or_last_idx):
580-
transient_locals[targets[i].id] = values[i]
581580
# Check for self.x assignment
582581
if _is_instance_attribute_assignment(targets[i], context):
583582
class_transients[targets[i].attr] = values[i]
583+
else:
584+
transient_locals[targets[i].id] = values[i]
584585

585586
# Starred if exists
586587
if starred:
587588
end = len(values) - (len(targets) - star_or_last_idx - 1)
588-
transient_locals[targets[star_or_last_idx].value.id] = values[
589-
star_or_last_idx:end
590-
]
591589
if _is_instance_attribute_assignment(
592590
targets[star_or_last_idx], context
593591
):
594592
class_transients[targets[star_or_last_idx].attr] = values[
595593
star_or_last_idx:end
596594
]
595+
else:
596+
transient_locals[targets[star_or_last_idx].value.id] = values[
597+
star_or_last_idx:end
598+
]
597599

598600
# After starred
599601
for i in range(star_or_last_idx + 1, len(targets)):
600-
transient_locals[targets[i].id] = values[
601-
len(values) - (len(targets) - i)
602-
]
603602
if _is_instance_attribute_assignment(targets[i], context):
604603
class_transients[targets[i].attr] = values[
605604
len(values) - (len(targets) - i)
606605
]
606+
else:
607+
transient_locals[targets[i].id] = values[
608+
len(values) - (len(targets) - i)
609+
]
607610
else:
608611
if _is_instance_attribute_assignment(target, context):
609612
class_transients[target.attr] = value
610-
elif hasattr(target, "id"):
613+
else:
611614
transient_locals[target.id] = value
612615
return None
613616

@@ -698,7 +701,6 @@ def eval_node(node: Union[ast.AST, None], context: EvaluationContext):
698701
if func_context.class_transients is not None:
699702
if not is_static and not is_classmethod:
700703
func_context.instance_arg_name = node.args.args[0].arg
701-
print("setting ", func_context.instance_arg_name)
702704

703705
return_type = eval_node(node.returns, context=context)
704706

@@ -868,6 +870,12 @@ def dummy_function(*args, **kwargs):
868870
if isinstance(node, ast.Name):
869871
return _eval_node_name(node.id, context)
870872
if isinstance(node, ast.Attribute):
873+
if (
874+
context.class_transients is not None
875+
and isinstance(node.value, ast.Name)
876+
and node.value.id == context.instance_arg_name
877+
):
878+
return context.class_transients.get(node.attr)
871879
value = eval_node(node.value, context)
872880
if policy.can_get_attr(value, node.attr):
873881
return getattr(value, node.attr)
@@ -930,7 +938,7 @@ def _merge_values(values):
930938
if type(v) is not dict:
931939
continue
932940
for k, val in v.items():
933-
key_values.setdefault(k, []).append(val) # Simpler than if-check
941+
key_values.setdefault(k, []).append(val)
934942

935943
merged_items = {k: _merge_values(vals) for k, vals in key_values.items()}
936944

tests/test_completer.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,13 +2227,26 @@ def _(expected):
22272227
),
22282228
"append",
22292229
],
2230+
[
2231+
"\n".join(
2232+
[
2233+
"class NotYetDefined:",
2234+
" def test(self):",
2235+
" self.l = []",
2236+
" return self.l",
2237+
"instance = NotYetDefined()",
2238+
"instance.test().",
2239+
]
2240+
),
2241+
"append",
2242+
],
22302243
[
22312244
"\n".join(
22322245
[
22332246
"def foo():",
22342247
" if 1+1==2:",
2235-
" return {'top':{'mid':{'leaf': 2}}}",
2236-
" return {'top': {'mid':[]}}",
2248+
" return {'top':{'mid':[]}}",
2249+
" return {'top': {'mid':{'leaf': 2}}}",
22372250
"foo()['top']['mid'].",
22382251
]
22392252
),

0 commit comments

Comments
 (0)