Skip to content

Not inferring generic type for variable typed as Callable with TypeVar #1555

@xhantu

Description

@xhantu

Describe the Bug

from typing import *

T = TypeVar('T')
f: Callable[[T], T] = lambda x: x
reveal_type(f)
r: int = f(1)
INFO sandbox.py:5:12-15: revealed type: (TypeVar[T]) -> TypeVar[T] [reveal-type]
ERROR sandbox.py:6:10-14: `TypeVar[T]` is not assignable to `int` [bad-assignment]
ERROR sandbox.py:6:12-13: Argument `Literal[1]` is not assignable to parameter with type `TypeVar[T]` [bad-argument-type]

Python: 3.12

The type of f should be [T](T) -> T and not (TypeVar[T]) -> TypeVar[T]

Because of this the usage of f fails with type errors.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AqADroRAFToBeOmOYwAaqkoAKAORjVAShFhEdAMKooUVNlgBtc2IC6AGhnWpdEzWyZUdfHvwjKMAG4wRgD6TMQwymDa6JR6rPzSYMoAjJogtiAArgzQcCTkiCAAxHQAqjlQEEx0YJnoAMY5uOhwIiKYMGA1vDSoDMHoma4wKl6c6AyadAC0AHx0cAyxInSrdH4MmZToNUIgAHJD2CN6wPgAvnsi6SBkfmBQpIQMtFAUJQAKpPePCxg4BDo9WakDYWz6EGahBEJQAyjAYHQABYMBjEOCIAD0mLunUehF4bExMHQmMwuHqcExwPQoPBTVJ3UodFQ-lQ0FMsCBIIgYMoEOadFwxAZeREZAYSOa00ClDgkJ20j2AGZCMkAExXdAgc4ZVCNCCBABi0BgFDQWDwRDIOqAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions