Skip to content

(🐞) TypeGuards incorrectly become the guard type when passed to a TypeVar #17114

@KotlinIsland

Description

@KotlinIsland

https://github.com/python/mypy/labels/topic-typeguard topic-type-variables

from typing import TypeGuard, TypeVar, Callable

T = TypeVar("T")

def guard(x: object) -> TypeGuard[str]: 
    return True

def f(fn: Callable[[object], T]) -> T: ...
a = f(guard)  # error: Argument 1 to "f" has incompatible type "Callable[[object], TypeGuard[str]]"; expected "Callable[[object], str]"  [arg-type]
reveal_type(a) # str

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions