Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e874d03

Browse files
committed
fix-subscript-assignment
1 parent 7bcf6fe commit e874d03

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

IPython/core/guarded_eval.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ def _validate_policy_overrides(
560560
def _handle_assign(node: ast.Assign, context: EvaluationContext):
561561
value = eval_node(node.value, context)
562562
transient_locals = context.transient_locals
563+
policy = get_policy(context)
563564
for target in node.targets:
564565
if isinstance(target, (ast.Tuple, ast.List)):
565566
# Handle unpacking assignment
@@ -586,6 +587,22 @@ def _handle_assign(node: ast.Assign, context: EvaluationContext):
586587
transient_locals[targets[i].id] = values[
587588
len(values) - (len(targets) - i)
588589
]
590+
elif isinstance(target, ast.Subscript):
591+
if isinstance(target.value, ast.Name):
592+
name = target.value.id
593+
container = transient_locals.get(name)
594+
if container is None:
595+
container = context.locals.get(name)
596+
if container is None:
597+
container = context.globals.get(name)
598+
if container is None:
599+
raise NameError(
600+
f"{name} not found in locals, globals, nor builtins"
601+
)
602+
603+
key = eval_node(target.slice, context)
604+
if policy.can_call(container.__setitem__):
605+
container[key] = value
589606
else:
590607
transient_locals[target.id] = value
591608
return None
@@ -1118,6 +1135,7 @@ def _list_methods(cls, source=None):
11181135
*_list_methods(collections.Counter, dict_non_mutating_methods),
11191136
collections.Counter.elements,
11201137
collections.Counter.most_common,
1138+
dict.__setitem__,
11211139
}
11221140

11231141
BUILTIN_GETATTR: set[MayHaveGetattr] = {

tests/test_completer.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,26 @@ def _(expected):
20962096
),
20972097
"as_integer_ratio",
20982098
],
2099+
[
2100+
"\n".join(
2101+
[
2102+
"num = {1: 'one'}",
2103+
"num[2] = 'two'",
2104+
"num.",
2105+
]
2106+
),
2107+
"keys",
2108+
],
2109+
[
2110+
"\n".join(
2111+
[
2112+
"test = {1: 'one'}",
2113+
"test[2] = ['two']",
2114+
"test[2].",
2115+
]
2116+
),
2117+
"append",
2118+
],
20992119
],
21002120
)
21012121
def test_undefined_variables(use_jedi, evaluation, code, insert_text):

0 commit comments

Comments
 (0)