Skip to content

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

Open

Description

@KotlinIsland
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

Activity

  1. 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[/+] on Mar 21, 2022
  2. KotlinIsland commented on Mar 21, 2022

    @KotlinIsland
    ContributorAuthor

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

  3. JelleZijlstra commented on Mar 21, 2022

    @JelleZijlstra
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions