Skip to content

Commit fb415c9

Browse files
committed
create-dummy-duck
1 parent e874d03 commit fb415c9

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

IPython/core/guarded_eval.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,22 @@ def _handle_assign(node: ast.Assign, context: EvaluationContext):
601601
)
602602

603603
key = eval_node(target.slice, context)
604-
if policy.can_call(container.__setitem__):
605-
container[key] = value
604+
attributes = (
605+
dict.fromkeys(dir(container))
606+
if policy.can_call(container.__dir__)
607+
else {}
608+
)
609+
items = {}
610+
611+
if policy.can_get_item(container, None):
612+
try:
613+
items = dict(container.items())
614+
except Exception:
615+
pass
616+
617+
items[key] = value
618+
duck_container = _Duck(attributes=attributes, items=items)
619+
transient_locals[name] = duck_container
606620
else:
607621
transient_locals[target.id] = value
608622
return None
@@ -1135,7 +1149,8 @@ def _list_methods(cls, source=None):
11351149
*_list_methods(collections.Counter, dict_non_mutating_methods),
11361150
collections.Counter.elements,
11371151
collections.Counter.most_common,
1138-
dict.__setitem__,
1152+
object.__dir__,
1153+
type.__dir__,
11391154
}
11401155

11411156
BUILTIN_GETATTR: set[MayHaveGetattr] = {

0 commit comments

Comments
 (0)