Skip to content

generics inferred differently when assigning to variable even though the variable's type is the same #11455

@DetachHead

Description

@DetachHead
from typing import Callable, TypeVar

A = TypeVar("A")
B = TypeVar("B")


def func(a: A, b: B, fn: Callable[[A], A]) -> A | B:
    ...


inferred = func(
    1,
    None,
    lambda value: reveal_type(value),  # Revealed type is "builtins.int"
)
reveal_type(inferred)  # "Union[builtins.int, None]"

explicit: int | None
explicit = func(
    1,
    None,
    lambda value: reveal_type(value), # Revealed type is "Union[builtins.int, None]"
)
reveal_type(explicit)  # "Union[builtins.int, None]"

https://mypy-play.net/?mypy=latest&python=3.10&gist=7c2af9e77a6246ee6fb3e5052722eec1

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