python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.5k stars 2.83k forks source link

Getting 'Untyped decorator makes function "*" untyped' when using decorator inside a function #9689

Open patrick91 opened 4 years ago

patrick91 commented 4 years ago

Sorry for the long title! I found a small issue when using disallow-untyped-decorators and while having a decorated function inside another function, here's the reproduction:

https://mypy-play.net/?mypy=latest&python=3.9&flags=disallow-untyped-decorators&gist=315f6ee827fbb1373b854cd317ee5882

from typing import Callable

def mydeco(f: Callable) -> Callable:
    return f

# Failing case

def something():
    class Query:
        @mydeco
        def search(self, input: str) -> str:
            return input

    reveal_type(Query.search)

# Failing case

def something():
    @mydeco
    def example() -> str:
        return "str"

    reveal_type(example)

# Ok case

class Query:
    @mydeco
    def search(self, input: str) -> str:
        return input

reveal_type(Query.search)

# Ok case

@mydeco
def example() -> str:
    return "str"

reveal_type(example)

I guess this is not a big deal since we don't normall define functions inside functions, but I got this issue while writing some tests and it'd be nice to get it fixed 😊

jordangarside commented 4 years ago

For this you can decorator the function manually:

def something():
    class Query:
        def search(self, input: str) -> str:
            return input
        decoratorated_search = mydeco(search)

    reveal_type(Query.decoratorated_search)