microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.7k stars 769 forks source link

Functions wrapped with functools.lru_cache are missing full prototypes and doc-strings #6094

Closed vient closed 2 months ago

vient commented 2 months ago

First of all, I am not sure that this is an issue in vscode-python and not in some other part of VSCode but I don't know how to understand which component is responsible - please point me in the right direction.

Consider this example

def is_even(x: int) -> bool:
    '''Returns True if x is even, False otherwise'''
    return x % 2 == 0

print(is_even(2))

Pointing to is_even(2) call shows a tooltip with function prototype and documentation image

Now we add lru_cache

import functools

@functools.cache
def is_even(x: int) -> bool:
    '''Returns True if x is even, False otherwise'''
    return x % 2 == 0

print(is_even(2))

and it still shows that is_even is a function but prototype changes to _lru_cache_wrapper[bool] and doc-string is gone image

Python 3.12 help() output is

Help on function is_even in module __main__:

is_even(x: int) -> bool
    Returns True if x is even, False otherwise

without lru_cache and

Help on _lru_cache_wrapper in module __main__:

is_even(x: int) -> bool
    Returns True if x is even, False otherwise

with it.

So, while it is helpful to see that the function is wrapped in lru_cache, it would also be nice to see proper argument list and documentation.

erictraut commented 2 months ago

See this issue for information about why pyright (the static type analysis engine upon which pylance is based) behaves as it does in this case. It's due to the way that functools.cache and functools.lru_cache are defined in the typeshed stubs. A change in behavior here would require a modification to the typeshed stubs. There are some tradeoffs for this change, however. Because of this, the maintainers of typeshed have been reluctant to accept a change. If you have an opinion on the matter, I recommend posting in the typeshed thread.