Exemple #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)
Exemple #2
0
def _create(inference_state, compiled_value, module_context):
    # TODO accessing this is bad, but it probably doesn't matter that much,
    # because we're working with interpreteters only here.
    python_object = compiled_value.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_value})

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

        if module_context is None or module_context.tree_node != module_node:
            root_compiled_value = compiled_value.get_root_context().get_value()
            # TODO this __name__ might be wrong.
            name = root_compiled_value.py__name__()
            string_names = tuple(name.split('.'))
            module_value = ModuleValue(
                inference_state,
                module_node,
                file_io=file_io,
                string_names=string_names,
                code_lines=code_lines,
                is_package=root_compiled_value.is_package(),
            )
            if name is not None:
                inference_state.module_cache.add(string_names,
                                                 ValueSet([module_value]))
            module_context = module_value.as_context()

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

    return ValueSet(
        MixedObject(compiled_value, tree_value=tree_value)
        for tree_value in tree_values)