Code Completion doesn't work for (class) functions decorated with decorators that return inner functions

So, decorated functions break down when when the decorator returns an inner function. This is not necessarily the case for stand-alone functions (unless they return other functions), but class functions show that effect immediately.

As can be seen, I get code completion for this class. But when I try to use code completion for return values, there's nothing:

Even though the viewer does have several functions, none are shown, as the returning type is "Any". If I explicitly name the type for the receiving variable, I have code completion:

So PyCharm does recognize the classes.

At the same time, none of the functions in that package give me parameter hints after I select a function:

As you can see (apart from the wrong documentation style) the function takes a file_name of type str, but the parameter hint only shows *args, **kwargs.

I don't have that problem in VS Code:

This does not happen in PyCharm 2020.2.5 nor in 2020.3

 

5
10 comments

Update: So PyCharm 2021+ works with decorators, as long as those decorators don't return generic inner functions:

def simple_decorator(f: Callable[P, R]) -> Callable[P, R]:
@wraps(f)
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
try:
return f(*args, **kwargs)
except RemoteError as e:
raise RemoteError(e.errormsg) from None
return inner


def simple_decorator2(f):
try:
return f
except RemoteError as e:
raise RemoteError(e.errormsg) from None

The first decorator prevents parameter hinting (instead you just see P: ParamSpec("P")) and its return type is Any if used on a class function.

The second decorator does not prevent that. The try block is useless, but it's just to show why we need inner functions.

0

At least some feedback on this issue would be nice. I mean, this bug literally breaks THE key feature of an IDE..code completion.

0

Apparently code completion is not as important...

0

Wickermoon could you solve the problem?

One more thing, what are P and R in your first comment?

0

Still happening for decorators returning `wrapper(*args, **kwargs)`

Build #PY-231.8109.197, built on March 29, 2023
0

Ran into something similar on IntelliJ `Build #IU-231.9011.4` with Python extension `231.9011.4` and the `@serde` decorator from `pyserde`. I get no autocomplete here:

@dataclass
@serde
class NoAutocomplete:
f1: int
f2: str

instance = NoAutocomplete(<no autocomplete here!>

If I remove the `@serde` decorator, autocomplete works normally. Here's the code for the `@serde` decorator at the time of writing:

@dataclass_transform()
def serde(
    _cls=None,
    **kwargs,
):  # type: ignore
    """
    serde decorator. Keyword arguments are passed in `serialize` and `deserialize`.
    """

    def wrap(cls):
        if should_impl_dataclass(cls):
            dataclass(cls)
        serialize(cls, **kwargs)
        deserialize(cls, **kwargs)
        return cls

    if _cls is None:
        return wrap  # type: ignore

    return wrap(_cls)
0

Seems to be working fine for me:

What Python version do you use?

0

Sergey Karpov Hmm, interesting. I'm using Python 3.8.16. What about you?

0

Please sign in to leave a comment.