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

Skip to content

Commit 494bdd2

Browse files
committed
check-policy-to-cal-__dir__
1 parent a0f72a0 commit 494bdd2

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

IPython/core/guarded_eval.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class EvaluationContext:
396396
policy_overrides: dict = field(default_factory=dict)
397397
#: Transient local namespace used to store mocks
398398
transient_locals: dict = field(default_factory=dict)
399-
#: Tranisents of class level
399+
#: Transients of class level
400400
class_transients: dict | None = None
401401
#: Instance variable name used in the method definition
402402
instance_arg_name: str | None = None
@@ -926,7 +926,7 @@ def dummy_function(*args, **kwargs):
926926
return None
927927

928928

929-
def _merge_values(values):
929+
def _merge_values(values, policy: EvaluationPolicy):
930930
"""Recursively merge multiple values, combining attributes and dict items."""
931931
if len(values) == 1:
932932
return values[0]
@@ -942,7 +942,9 @@ def _merge_values(values):
942942
for k, val in v.items():
943943
key_values.setdefault(k, []).append(val)
944944

945-
merged_items = {k: _merge_values(vals) for k, vals in key_values.items()}
945+
merged_items = {
946+
k: _merge_values(vals, policy) for k, vals in key_values.items()
947+
}
946948

947949
if len(types) == 1:
948950
t = next(iter(types))
@@ -953,7 +955,8 @@ def _merge_values(values):
953955

954956
attributes = set()
955957
for v in values:
956-
attributes.update(dir(v))
958+
if policy.can_call(v.__dir__):
959+
attributes.update(dir(v))
957960
return _Duck(attributes=dict.fromkeys(attributes), items=merged_items)
958961

959962

@@ -966,7 +969,8 @@ def _infer_return_value(node: ast.FunctionDef, context: EvaluationContext):
966969
if len(return_values) == 1:
967970
return return_values[0]
968971

969-
return _merge_values(return_values)
972+
policy = get_policy(context)
973+
return _merge_values(return_values, policy)
970974

971975

972976
def _collect_return_values(body, context):
@@ -1266,6 +1270,8 @@ def _list_methods(cls, source=None):
12661270
*_list_methods(collections.Counter, dict_non_mutating_methods),
12671271
collections.Counter.elements,
12681272
collections.Counter.most_common,
1273+
object.__dir__,
1274+
type.__dir__,
12691275
}
12701276

12711277
BUILTIN_GETATTR: set[MayHaveGetattr] = {

tests/test_completer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ def _(expected):
22682268
"\n".join(
22692269
[
22702270
"def foo():",
2271-
" if 1+1==2:",
2271+
" if some_condition:",
22722272
" return {'top':{'mid':{'leaf': 2}}}",
22732273
" return {'top': {'mid':[]}}",
22742274
"foo()['top']['mid']['leaf'].",

0 commit comments

Comments
 (0)