jedi icon indicating copy to clipboard operation
jedi copied to clipboard

Dataclass construction completion does not work for cls in class methods

Open idanarye opened this issue 3 years ago • 1 comments

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.

idanarye avatar Jan 13 '22 09:01 idanarye

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!

davidhalter avatar Jan 14 '22 00:01 davidhalter