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 construction completion does not work for cls in class methods #1832

Open
idanarye opened this issue Jan 13, 2022 · 1 comment
Open

Comments

@idanarye
Copy link

import jedi


script = jedi.Script("""
from dataclasses import dataclass


@dataclass
class Foo:
    foo_param: int

    @classmethod
    def class_method(cls):
        cls()
        Foo()


class Bar:
    def __init__(self, bar_param: int):
        pass

    @classmethod
    def class_method(cls):
        cls()
        Bar()
""")

print('jedi version', jedi.__version__)
for line_nr in [11, 12, 21, 22]:
    print()
    line = script._code_lines[line_nr - 1]
    print('line', line_nr, repr(line.strip()))
    col = line.index('(') + 1
    print('    Context:', script.get_context(line_nr, col))
    print('    Signature:', script.get_signatures(line_nr, col))
    print('    Completion:', [c for c in script.complete(line_nr, col) if 'param' in c.name])

When I run this, I get:

jedi version 0.18.1

line 11 'cls()'
    Context: <Name full_name='__main__.Foo.class_method', description='def class_method'>
    Signature: [<Signature: index=None Foo()>]
    Completion: []

line 12 'Foo()'
    Context: <Name full_name='__main__.Foo.class_method', description='def class_method'>
    Signature: [<Signature: index=0 Foo(foo_param: int)>]
    Completion: [<Completion: foo_param=>]

line 21 'cls()'
    Context: <Name full_name='__main__.Bar.class_method', description='def class_method'>
    Signature: [<Signature: index=0 Bar(bar_param: int)>]
    Completion: [<Completion: bar_param=>]

line 22 'Bar()'
    Context: <Name full_name='__main__.Bar.class_method', description='def class_method'>
    Signature: [<Signature: index=0 Bar(bar_param: int)>]
    Completion: [<Completion: bar_param=>]

For Bar, which is not a dataclass, the signature is recognized in both when I use cls inside the class method and when I use the actual name Bar. But for Foo - which is a dataclass - the signature is only recognized when I use the class name Foo and not when I use cls, and Jedi is unable to suggest the completion of the keyword argument.

@davidhalter
Copy link
Owner

Not sure this is ever going to be fixed, because classmethods are a somewhat special heuristic, but it's definitely a bug, so thanks for reporting!

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