Skip to content

Invalid completions shown when evaluation policy forbids access #14916

Description

@krassowski

To reproduce create a repro.py file with:

class StringMethods:
    def a():
        pass

class Test:
     def __init__(self):
         self.str = StringMethods()
     def __getattr__(self, name):
         raise AttributeError(f"{name} not found")

Then in IPython:

%config Completer.use_jedi = False
from repro import Test
r = Test()
r.str.# <tab>

This will show all methods of str, but none of StringMethods.

This is because:

  • the custom __getattr__ leads the default guarded evaluation policy to reject evaluation of r.str, which raises GuardRejection
  • IPython then tries to recover from any Exception by trimming the r. part away, even though it might be sufficient to trim on SyntaxError and related exceptions:

def _evaluate_expr(self, expr):
obj = not_found
done = False
while not done and expr:
try:
obj = guarded_eval(
expr,
EvaluationContext(
globals=self.global_namespace,
locals=self.namespace,
evaluation=self.evaluation,
auto_import=self._auto_import,
policy_overrides=self.policy_overrides,
),
)
done = True
except Exception as e:
if self.debug:
print("Evaluation exception", e)
# trim the expression to remove any invalid prefix
# e.g. user starts `(d[`, so we get `expr = '(d'`,
# where parenthesis is not closed.
# TODO: make this faster by reusing parts of the computation?
expr = self._trim_expr(expr)
return obj

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions