Visual studio Code Python Intellisense not detecting return types

78 views Asked by At

In past i always made functions that get a type/class as argument and create an instance of that type, returning the instance. E.g:

    def CreateWidget(self, widgetType, show = True, name = '', x = 0, y = 0, size = None, parent = None):
        ret = widgetType()
        ret.SetParent(self if not parent else parent)
        if show:
            ret.Show()
        ret.SetPosition(x,y)
        if name:
            ret.SetWindowName(name)
        ret._ApplySize(size)
        return ret

Intellisense always deduced the right type, and it always suggested the right hints while using the instance typing code.

Recently the type/class is no longer deduced, all variables that are storing the return of CreateWidget are no longer highlighted by the IDE and i can't get the hints while typing code.

i m going to show a minimal reproducible example that i m pretty sure works on most of users wanna test it:

class Foo:
    def __init__(self):
        self.value = 0
        self.value2 = 0

    def FantasticMethod(self):
        pass

    def CreateChild(self, type):
        value = type()
        return value

value = Foo()
value2 = value.CreateChild(Foo)

here's what happens when i type "value." waiting the intellisense to make the hints: enter image description here

and what happens typing "value2.": enter image description here

As you can see, there's no hints on "value2" instance, while "value" is correctly recognized as Foo. I m using "Python" and "Pylance" extensions (no other extensions related with python).

I leave here my settings.json because i guess it might be relevant:

{
    "python.pythonPath": "C:\\Users\\XXXXXX\\AppData\\Local\\Programs\\Python\\Python39\\python.exe",
    "workbench.editorAssociations": {
        "*.ipynb": "jupyter-notebook"
    },
    "notebook.cellToolbarLocation": {
        "default": "right",
        "jupyter-notebook": "left"
    },
    "python.defaultInterpreterPath": "C:\\Users\\XXXXX\\AppData\\Local\\Programs\\Python\\Python39\\python.exe",
    "files.trimTrailingWhitespace": true,
    "editor.suggest.localityBonus": true,
    "editor.renderWhitespace": "all",
    "files.encoding": "windows1252",
    "editor.hover.delay": 2000,
    "python.analysis.autoImportCompletions": false,
    "[python]": {
        "editor.formatOnType": true
    },
    "git.openRepositoryInParentFolders": "never",
    "editor.insertSpaces": false,
    "editor.autoIndent": "advanced",
    "vsintellicode.features.python.deepLearning": "enabled",
    "editor.codeActionsOnSave": {
        
    }
}

I tried to remove Python and Pylance extensions (resulting with a totally broken intellisense). I tried to revert updates on both Python and Pylance (installing old versions which i was using 1 month ago) didn't help. I tried to open a different project which also were using a similar function, and it has the same issue.

1

There are 1 answers

0
IkarusDeveloper On

I solved it reporting to PyLance Team the bug and they moved the request to PyRight team which solved the issue inside the new published PyRight version. https://github.com/microsoft/pyright/issues/7343


Actually the fix is not published on PyLance yet (it will be included in one of the next PyLance releases). I m actually using an old Release of PyLance which isn't affected by this bug (v2023.11.10).