Skip to content

Commit e4c6700

Browse files
committed
complete-logic-and-improve-tests
1 parent e954f62 commit e4c6700

2 files changed

Lines changed: 52 additions & 18 deletions

File tree

IPython/core/guarded_eval.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -620,24 +620,37 @@ def _handle_assign(node: ast.Assign, context: EvaluationContext):
620620
raise NameError(
621621
f"{name} not found in locals, globals, nor builtins"
622622
)
623+
storage_dict = transient_locals
624+
storage_key = name
625+
elif isinstance(
626+
target.value, ast.Attribute
627+
) and _is_instance_attribute_assignment(target.value, context):
628+
attr = target.value.attr
629+
container = class_transients.get(attr, None)
630+
if container is None:
631+
raise NameError(f"{attr} not found in class transients")
632+
storage_dict = class_transients
633+
storage_key = attr
634+
else:
635+
return
623636

624-
key = eval_node(target.slice, context)
625-
attributes = (
626-
dict.fromkeys(dir(container))
627-
if policy.can_call(container.__dir__)
628-
else {}
629-
)
630-
items = {}
637+
key = eval_node(target.slice, context)
638+
attributes = (
639+
dict.fromkeys(dir(container))
640+
if policy.can_call(container.__dir__)
641+
else {}
642+
)
643+
items = {}
631644

632-
if policy.can_get_item(container, None):
633-
try:
634-
items = dict(container.items())
635-
except Exception:
636-
pass
645+
if policy.can_get_item(container, None):
646+
try:
647+
items = dict(container.items())
648+
except Exception:
649+
pass
637650

638-
items[key] = value
639-
duck_container = _Duck(attributes=attributes, items=items)
640-
transient_locals[name] = duck_container
651+
items[key] = value
652+
duck_container = _Duck(attributes=attributes, items=items)
653+
storage_dict[storage_key] = duck_container
641654
elif _is_instance_attribute_assignment(target, context):
642655
class_transients[target.attr] = value
643656
else:

tests/test_completer.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,27 @@ def _(expected):
21212121
),
21222122
"as_integer_ratio",
21232123
],
2124+
[
2125+
"\n".join(
2126+
[
2127+
"def my_test():",
2128+
" return {}",
2129+
"my_test().",
2130+
]
2131+
),
2132+
"keys",
2133+
],
2134+
[
2135+
"\n".join(
2136+
[
2137+
"l = []",
2138+
"def my_test():",
2139+
" return l",
2140+
"my_test().",
2141+
]
2142+
),
2143+
"append",
2144+
],
21242145
[
21252146
"\n".join(
21262147
[
@@ -2134,9 +2155,9 @@ def _(expected):
21342155
[
21352156
"\n".join(
21362157
[
2137-
"test = {1: 'one'}",
2138-
"test[2] = ['two']",
2139-
"test[2].",
2158+
"num = {1: 'one'}",
2159+
"num[2] = ['two']",
2160+
"num[2].",
21402161
]
21412162
),
21422163
"append",

0 commit comments

Comments
 (0)