Skip to content

autocomplete from wrong type. #651

Open
@kaddkaka

Description

@kaddkaka
class A:
    pass


class HasAnA:
    def __init__(self, a):
        self._a = a

    def get_a(self):
        return self._a


class Thing:
    def __init__(self, item: A | HasAnA):
        self._item: A
        if isinstance(item, A):
            self._item = item
        else:
            self._item = item.get_a()
        assert isinstance(item, HasAnA)

    def method(self):
        # self._item.  --> tab completes into:
        self._item.get_a()

When the member self._item is declared and assigned as above, pylsp seems to be confused about the type (Can I verify this somehow?)

When I try to tab complete after self._item. (see above) pylsp suggests the method get_a, which is not available on an instance of A.

I tried to clarify and change some types in the example, but the behavior stayed. Is there some other way to rewrite the inside of class Thing so that pylsp unserstands the type correctly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions