diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index ed1af143..8a32919d 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -21,9 +21,9 @@ from jedi import debug from jedi import settings from jedi import common from jedi import cache -from jedi import interpret from jedi.api import keywords from jedi.api import classes +from jedi.api import interpreter from jedi.evaluate import Evaluator, filter_private_variable from jedi.evaluate import representation as er from jedi.evaluate import compiled @@ -150,8 +150,7 @@ class Script(object): if settings.case_insensitive_completion \ and n.lower().startswith(like.lower()) \ or n.startswith(like): - if not filter_private_variable(s, - user_stmt or self._parser.user_scope(), n): + if not filter_private_variable(s, user_stmt or self._parser.user_scope(), n): new = classes.Completion(self._evaluator, c, needs_dot, len(like), s) k = (new.name, new.complete) # key if k in comp_dct and settings.no_completion_duplicates: @@ -548,7 +547,6 @@ class Script(object): class Interpreter(Script): - """ Jedi API for Python REPLs. @@ -562,7 +560,6 @@ class Interpreter(Script): >>> script = Interpreter('join().up', [namespace]) >>> print(script.completions()[0].name) upper - """ def __init__(self, source, namespaces=[], **kwds): @@ -583,9 +580,7 @@ class Interpreter(Script): self.namespaces = namespaces # Here we add the namespaces to the current parser. - importer = interpret.ObjectImporter(self._parser.user_scope()) - for ns in namespaces: - importer.import_raw_namespace(ns) + interpreter.create(namespaces[0], self._parser.module()) def _simple_complete(self, path, like): user_stmt = self._parser.user_stmt(True) diff --git a/jedi/api/interpreter.py b/jedi/api/interpreter.py new file mode 100644 index 00000000..ce169bba --- /dev/null +++ b/jedi/api/interpreter.py @@ -0,0 +1,35 @@ +from jedi.evaluate import compiled +from jedi.parser import representation as pr +from jedi.evaluate import helpers + + +class InterpreterNamespace(pr.Module): + def __init__(self, namespace, parser_module): + self.namespace = namespace + self.parser_module = parser_module + + def get_defined_names(self): + for key, value in self.namespace.items(): + yield LazyName(key, value) + + def __getattr__(self, name): + return getattr(self.parser_module, name) + + +class LazyName(helpers.FakeName): + def __init__(self, name, parent_obj): + super(LazyName, self).__init__(name) + self._parent_obj = parent_obj + + @property + def parent(self): + return compiled.create(self._parent_obj) + + @parent.setter + def parent(self, value): + """Needed because of the ``representation.Simple`` super class.""" + + +def create(namespace, parser_module): + ns = InterpreterNamespace(namespace, parser_module) + parser_module.statements[0].parent = ns diff --git a/jedi/evaluate/compiled/__init__.py b/jedi/evaluate/compiled/__init__.py index b636af2a..dc81999d 100644 --- a/jedi/evaluate/compiled/__init__.py +++ b/jedi/evaluate/compiled/__init__.py @@ -290,7 +290,7 @@ def create(obj, parent=builtin, module=None): A very weird interface class to this module. The more options provided the more acurate loading compiled objects is. """ - if not inspect.ismodule(parent): + if not inspect.ismodule(obj): faked = fake.get_faked(module and module.obj, obj) if faked is not None: faked.parent = parent