Пример #1
0
def _create(inference_state, access_handle, parent_context, *args):
    compiled_object = create_cached_compiled_object(
        inference_state,
        access_handle,
        # TODO It looks like we have to use the compiled object as a parent context.
        #      Why is that?
        parent_context=None if parent_context is None else
        parent_context.compiled_object.as_context()  # noqa
    )

    # TODO accessing this is bad, but it probably doesn't matter that much,
    # because we're working with interpreteters only here.
    python_object = access_handle.access._obj
    result = _find_syntax_node_name(inference_state, python_object)
    if result is None:
        # TODO Care about generics from stuff like `[1]` and don't return like this.
        if type(python_object) in (dict, list, tuple):
            return ValueSet({compiled_object})

        tree_values = to_stub(compiled_object)
        if not tree_values:
            return ValueSet({compiled_object})
    else:
        module_node, tree_node, file_io, code_lines = result

        if parent_context is None:
            # TODO this __name__ is probably wrong.
            name = compiled_object.get_root_context().py__name__()
            string_names = tuple(name.split('.'))
            module_context = ModuleValue(
                inference_state,
                module_node,
                file_io=file_io,
                string_names=string_names,
                code_lines=code_lines,
                is_package=compiled_object.is_package(),
            ).as_context()
            if name is not None:
                inference_state.module_cache.add(string_names,
                                                 ValueSet([module_context]))
        else:
            if parent_context.tree_node.get_root_node() != module_node:
                # This happens e.g. when __module__ is wrong, or when using
                # TypeVar('foo'), where Jedi uses 'foo' as the name and
                # Python's TypeVar('foo').__module__ will be typing.
                return ValueSet({compiled_object})
            module_context = parent_context.get_root_context()

        tree_values = ValueSet({module_context.create_value(tree_node)})
        if tree_node.type == 'classdef':
            if not access_handle.is_class():
                # Is an instance, not a class.
                tree_values = tree_values.execute_with_values()

    return ValueSet(
        MixedObject(compiled_object, tree_value=tree_value)
        for tree_value in tree_values)
Пример #2
0
        def access_to_value(parent_value, access):
            if parent_value is None:
                parent_context = None
            else:
                parent_context = parent_value.as_context()

            if parent_context is None or isinstance(parent_context,
                                                    MixedContext):
                return _create(self._inference_state,
                               access,
                               parent_context=parent_context)
            else:
                return ValueSet({
                    create_cached_compiled_object(
                        parent_context.inference_state, access, parent_context)
                })