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

dataclass constructor signature may be incorrect if it's created by Name.parent() #1912

Open
MrVPlusOne opened this issue Feb 11, 2023 · 2 comments

Comments

@MrVPlusOne
Copy link

Hi, I encountered this weird bug where jedi can return incorrect signature for dataclass constructors.

A minimal example to reproduce this:

import jedi, jedi.api.classes

s = jedi.Script(
"""\
from dataclasses import dataclass
@dataclass
class Foo:
    bar: int
"""
)

defs = s.goto(3, 8)  # go to Foo directly
print(f"{len(defs)=}")
n = defs[0]
assert isinstance(n, jedi.api.classes.Name)
print(n._get_docstring_signature())

This would return the correct result:

len(defs)=1
Foo(bar: int)

However, if I go to bar first and then call parent()

defs = s.goto(4, 6)  # first go to bar
print(f"{len(defs)=}")
n = defs[0].parent()  # then go to parent
assert isinstance(n, jedi.api.classes.Name)
print(n._get_docstring_signature())

jedi returns incorrect result:

len(defs)=1
Foo()

In my application, I need to analyze the definitions used by a given piece of code, and when there are class attribute or method usages, I'm using parent() to also include the class signatures. This bug is causing incorrect class signatures to be returned. Any idea what causes this or how to fix this?
Many Thanks!

@davidhalter
Copy link
Owner

I'm pretty sure that this is not going to be fixed, because parent() has a very low usage and therefore low priority. However feel free to try and fix it. The root cause is probably that at the point you are looking at the dataclass signature, Jedi thinks its just a class and looks at that __init__, which is basically empty.

@MrVPlusOne
Copy link
Author

Ok that makes sense. I'll see if I can find a way to fix it. Thanks.

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

No branches or pull requests

2 participants