microsoft / pylance-release

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

Editor mark syntax error when trying to decorate a Python function with a decorator defined as an instance method inside the class #4912

Open JesusFragoso opened 1 year ago

JesusFragoso commented 1 year ago

Steps to Reproduce:

  1. Create this code:
from typing import Callable

class Formatter:
    def decorator(self, main_function: Callable) -> Callable:
        def wrapper(*args, **kwargs):
            print("Inside the decorator")
            return main_function(*args, **kwargs)
        return wrapper

formatter_class

Which in this case, is a class called Formatter that has an instance method that acts as decorator, called decorator.

  1. Now, create a method called foo, and decorate it using the decorator instance method, but, because in this case we need an instance of the Formatter class to use this decorator, we need to make the following code:
@Formatter().decorator
def second_foo():
    print("Inside the second_foo function")

if __name__ == "__main__":
    second_foo()

decorating_method

And it works, but the called decorator is marked in red, as if it were a mistake.

Is this an error of the editor, or do I need to define some type hints?

erictraut commented 12 months ago

This is a TextMate grammar issue.