Skip to content

(🐞) Call to function decorated with a Callable causes inner function to fail to infer type of variable #8900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
KotlinIsland opened this issue May 27, 2020 · 2 comments
Labels
bug mypy got something wrong

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented May 27, 2020

from typing import Any, Callable, TypeVar, _T as T

def bar() -> None:
    def inner() -> None:
        it.encode()  # error: Cannot determine type of 'it'
    breaks()
    it = ""
    
deco: Callable[[T], T]

@deco
def breaks() -> None: ...

It appears that the order of the function declarations affects the result(:trollface:):

from typing import Any, Callable, TypeVar

T = TypeVar("T")

deco: Callable[[T], T]

@deco
def doesnt_break() -> None: ...

def bar() -> None:
    def inner() -> None:
        it.encode()  # no error
    doesnt_break()
    it = ""

I have determined that this only happens when the deco is a Callable, when it is a function there is no error:

from typing import Any, TypeVar, _T as T

def bar() -> None:
    def inner() -> None:
        it.encode()  # no error
    doesnt_break()
    it = ""
    
def deco(fn: T) -> T: ...

@deco
def doesnt_break() -> None: ...

playground

@97littleleaf11 97littleleaf11 added the bug mypy got something wrong label Feb 19, 2022
@KotlinIsland KotlinIsland changed the title error: Cannot determine type of 'it' with call to decorated function (🐞) Call to function decorated with a Callable causes inner function to fail to infer type of variable Mar 21, 2022
@KotlinIsland
Copy link
Contributor Author

topic-inference When to infer types or require explicit annotations ?

@JelleZijlstra
Copy link
Member

I meant "inference" to mean ways to infer types for unannotated code. This feels different though I don't know how to classify it. It's probably a bug somewhere in semanal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants