Code Monkey home page Code Monkey logo

Comments (4)

pyscripter avatar pyscripter commented on June 25, 2024 1

@davidhalter

Here is my fix. In syntax_tree.py, method tree_name_to_values, I have added 4 lines after the statement found_annotation = True

def tree_name_to_values(inference_state, context, tree_name):
    value_set = NO_VALUES
    module_node = context.get_root_context().tree_node
    # First check for annotations, like: `foo: int = 3`
    if module_node is not None:
        names = module_node.get_used_names().get(tree_name.value, [])
        found_annotation = False
        for name in names:
            expr_stmt = name.parent

            if expr_stmt.type == "expr_stmt" and expr_stmt.children[1].type == "annassign":
                correct_scope = parser_utils.get_parent_scope(name) == context.tree_node
                if correct_scope:
                    found_annotation = True
                    if (expr_stmt.children[1].children[1].type == 'name') and \
                        (expr_stmt.children[1].children[1].value == tree_name.value) and \
                        context.parent_context:
                        context = context.parent_context
                    value_set |= annotation.infer_annotation(
                        context, expr_stmt.children[1].children[1]
                    ).execute_annotation()

If you agree with the change, I can submit PR. Please let me know.

from jedi.

davidhalter avatar davidhalter commented on June 25, 2024

Yes, this surprisingly does not work.

from jedi.

davidhalter avatar davidhalter commented on June 25, 2024

@pyscripter. I generally agree with the analysis of the problem. A PR would be appreciated.

However I feel like thes solution would probably be better like this:

ann_assign = expr_stmt.children[1]
if correct_scope and name.start_pos < ann_assign.start_pos:

We just need to make sure that the name is part of the annotation assignment before the annotation. That's probably easier to understand.

from jedi.

pyscripter avatar pyscripter commented on June 25, 2024

@davidhalter
I have submitted a PR. I have added the check of start_pos you suggested and makes sense. You still need to check after that, whether the variable name is identical to type name and if this is true search for the type in the parent context.

from jedi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.