Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calling parent on a node that is part of a type comment returns the (original) _ast module's FunctionDef #851

Closed
ltcmelo opened this issue Nov 11, 2020 · 4 comments · Fixed by #887
Labels

Comments

@ltcmelo
Copy link
Contributor

ltcmelo commented Nov 11, 2020

Consider this snippet:

def f(s):
    # type: (str) -> int
    return 42

Given the node from the type comment, Name.int(name='int'), if I call its parent (i.e., node.parent), I get the original _ast module's FunctionDef, instead of astroid's FunctionDef.

@PCManticore
Copy link
Contributor

Hi, thanks for the report. I tried to reproduce this but I was not able, can you paste the exact code you used to demonstrate the issue?

@PCManticore PCManticore added the Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine label Dec 28, 2020
@ltcmelo
Copy link
Contributor Author

ltcmelo commented Jan 4, 2021

@PCManticore, reproducibility of this one is straightforward to me, here's a (partial) example — but note that I had a typo in the original snippet: I missed the colon after type, which I just fixed; perhaps this is the reason you couldn't reproduce?

Assume an AST visitor with:

def visit_functiondef(self, node: nodes.FunctionDef)
   print(f'the parent of type comment return is {node.type_comment_returns.parent}')

To me, this prints:
Screen Shot 2021-01-04 at 12 06 04

@AWhetter
Copy link
Contributor

I've just hit this myself. Here's what I did to reproduce:

>>> import astroid
>>> f = astroid.extract_node("""
def f(a):
    # type: (A) -> None
    pass
""")
>>> f.type_comment_args[0].parent
<ast.FunctionDef object at 0x7f0b0fb077f0>

AWhetter added a commit that referenced this issue Jan 31, 2021
These nodes used to be parented to their original ast.FunctionDef parent
but are now correctly parented to their astroid.FunctionDef parent.

Closes #851
@AWhetter AWhetter added Bug 🪳 and removed Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine labels Jan 31, 2021
AWhetter added a commit that referenced this issue Jan 31, 2021
These nodes used to be parented to their original ast.FunctionDef parent
but are now correctly parented to their astroid.FunctionDef parent.

Closes #851
PCManticore pushed a commit that referenced this issue Feb 7, 2021
These nodes used to be parented to their original ast.FunctionDef parent
but are now correctly parented to their astroid.FunctionDef parent.

Closes #851
@PCManticore
Copy link
Contributor

Thanks @AWhetter and @ltcmelo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants