Skip to content

Problem inferring type variables with a default in Protocol #11179

@cmp0xff

Description

@cmp0xff

Describe the bug

pyright does not infer the type variable with a default properly when it comes to a Protocol.

Code or Screenshots

ttest.py:

from typing import Any, Generic, Protocol
from typing_extensions import Self, reveal_type, TypeVar

T_co = TypeVar("T_co", covariant=True)
S1 = TypeVar("S1", default=Any)


class SupportRou(Protocol[T_co]):
    def __rou__(self) -> T_co: ...


def rou(obj: SupportRou[T_co]) -> T_co:
    return obj.__rou__()


class Series(Generic[S1]):
    def __rou__(self) -> Self:
        return self


reveal_type(rou(Series()))  # pyright gives Series[Unknown], mypy gives Series[Any]

reveal_type(rou(Series[int]()))  # pyright and mypy both give Series[int]

VS Code extension or command-line

poetry run pyright ttest.py

I have pyright 1.1.407, python 3.10.11 or 3.14.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions