python-language-server icon indicating copy to clipboard operation
python-language-server copied to clipboard

Add support for python descriptor protocol

Open CaselIT opened this issue 5 years ago • 3 comments

As of version 0.5.30.0 the language server does not seem to support the python descriptor protocol, that allows definition of functions similar to the @property decorator. The documentation for this on the python docs is https://docs.python.org/3/howto/descriptor.html

Example:

class memoized_property:
    def __init__(self, fGet):
        self.fGet = fGet

    def __get__(self, instance, class_=None):
        if instance is None:
            return self
        instance.__dict__[self.__name__] = res = self.fGet(instance)
        return res

class Foo:
    @memoized_property
    def i_only_run_once(self) -> str:
        # long expensive stuff here
        return 'once'

foo = Foo()
value = foo.i_only_run_once

value is 'once' here, but the inferred type is the function Foo.i_only_run_once() -> str, since the language server thinks that i_only_run_once is a function. image Calling it foo.i_only_run_once() makes the inferred type be str, but it will obviously cause a runtime error. Using @property instead of @memoized_property works as expected.

I'm using vscode 1.42.1 and the language server 0.5.30.0

CaselIT avatar Mar 03 '20 09:03 CaselIT

For reference, @property is special cased in the analysis. Custom decorators like this are not supported to this extent, hence why the decorator doesn't have any effect.

jakebailey avatar Mar 03 '20 18:03 jakebailey

I figured as much, since @property is far more common. It would be nice if it could be supported though.

CaselIT avatar Mar 03 '20 20:03 CaselIT

Just wanted to mention that the same behavior happens on the cached_property in the standard lib

CaselIT avatar May 15 '20 08:05 CaselIT