mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Mypy fails to deduce generic type if it has been previously deduced as `<nothing>`

Open danields761 opened this issue 3 years ago • 0 comments

Bug Report

Mypy fails to deduce generic type if it has been previously deduced as <nothing>

To Reproduce

Minimal example to reproduce

from typing import Callable, TypeVar, Generic

T = TypeVar("T")

class Foo(Generic[T]):
    pass

def decorate(foo: Foo[T] | None) -> Callable[[T], T]:
    raise NotImplementedError


# Case 1
reveal_type(decorate(None)(str()))  # Revealed type is "<nothing>"; Argument 1 has incompatible type "str"; expected <nothing>

# Case 2
reveal_type(decorate(Foo())(str()))  # Revealed type is "<nothing>"; Argument 1 has incompatible type "str"; expected <nothing>

# Case 3
reveal_type(decorate(Foo[str]())(str()))  # Revealed type is "builtins.str"

Playground link

Expected Behavior

I'am expecting mypy to work in case 1 and case 2 like in the following example, where generic variable T isn't constrainted by upper function decorate and her arguments.

def decorate() -> Callable[[T], T]:
    raise NotImplementedError

reveal_type(decorate()(str()))  # Revealed type is "str"

This intuition comes from the fact, that in some cases generic variables is not deductible at some point, but later they are defined through context of usage, like in the following example:

l = []  # it's is like `list[<nothing>]` right now
l.append(int())
reveal_type(l)  # Revealed type is `builtins.list[builtins.int]`

Actual Behavior

Mypy rejects case 1 and case 2 from the main example, but it's look counter-intuitive, therefore it looks like a bug

Your Environment

All examples were run on mypy-play.net using Python 3.10 and mypy == 0.971 without additional options

danields761 avatar Jul 27 '22 16:07 danields761